You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by "Hisoka-X (via GitHub)" <gi...@apache.org> on 2023/04/24 08:46:47 UTC

[GitHub] [incubator-seatunnel] Hisoka-X commented on a diff in pull request #4632: [Feature][Catalog]Refactor catalog,put data source attributes into dialects.

Hisoka-X commented on code in PR #4632:
URL: https://github.com/apache/incubator-seatunnel/pull/4632#discussion_r1174909595


##########
seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/catalog/AbstractJdbcCatalog.java:
##########
@@ -120,6 +134,63 @@ public void close() throws CatalogException {
         LOG.info("Catalog {} closing", catalogName);
     }
 
+    @Override
+    public List<String> listDatabases() throws CatalogException {
+        try (Connection conn = DriverManager.getConnection(defaultUrl, username, pwd)) {
+
+            PreparedStatement ps = conn.prepareStatement(jdbcDialect.listDatabases());
+
+            List<String> databases = new ArrayList<>();
+            ResultSet rs = ps.executeQuery();
+
+            while (rs.next()) {
+                String databaseName = rs.getString(1);
+                if (!getSysDatabases().contains(databaseName)) {
+                    databases.add(rs.getString(1));
+                }
+            }
+
+            return databases;
+        } catch (Exception e) {
+            throw new CatalogException(
+                    String.format("Failed listing database in catalog %s", this.catalogName), e);
+        }
+    }
+
+    @Override
+    public List<String> listTables(String databaseName)
+            throws CatalogException, DatabaseNotExistException {
+        if (!databaseExists(databaseName)) {
+            throw new DatabaseNotExistException(this.catalogName, databaseName);
+        }
+
+        String dbUrl = jdbcDialect.getUrlFromDatabaseName(baseUrl, databaseName, suffix);
+        try (Connection conn = DriverManager.getConnection(dbUrl, username, pwd);
+                PreparedStatement ps =
+                        conn.prepareStatement(jdbcDialect.listTableSql(databaseName))) {
+
+            ResultSet rs = ps.executeQuery();
+
+            List<String> tables = new ArrayList<>();
+
+            StringBuilder fullTableName = new StringBuilder();
+            while (rs.next()) {
+                for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
+                    fullTableName.append(rs.getString(i)).append("\\.");
+                }
+            }
+
+            tables.add(
+                    TablePath.of(fullTableName.substring(0, fullTableName.length() - 1))
+                            .getTableName());
+
+            return tables;

Review Comment:
   I'm sure this part code can't execute normally. Please do test again. And add test case for it.



##########
seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/catalog/AbstractJdbcCatalog.java:
##########
@@ -40,29 +46,35 @@
 import java.sql.Connection;
 import java.sql.DatabaseMetaData;
 import java.sql.DriverManager;
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 public abstract class AbstractJdbcCatalog implements Catalog {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractJdbcCatalog.class);
-
     protected final String catalogName;
     protected final String defaultDatabase;
     protected final String username;
     protected final String pwd;
     protected final String baseUrl;
     protected final String suffix;
     protected final String defaultUrl;
+    protected final JdbcDialect jdbcDialect;
+    protected static final Set<String> SYS_DATABASES = new HashSet<>();

Review Comment:
   The static variable bind with `Class`, not `Object`. So in your code, all catalog implement class will share same `SYS_DATABASES`. Please do test before submit code.



##########
seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/catalog/AbstractJdbcCatalog.java:
##########
@@ -120,6 +134,63 @@ public void close() throws CatalogException {
         LOG.info("Catalog {} closing", catalogName);
     }
 
+    @Override
+    public List<String> listDatabases() throws CatalogException {
+        try (Connection conn = DriverManager.getConnection(defaultUrl, username, pwd)) {
+
+            PreparedStatement ps = conn.prepareStatement(jdbcDialect.listDatabases());
+
+            List<String> databases = new ArrayList<>();
+            ResultSet rs = ps.executeQuery();
+
+            while (rs.next()) {
+                String databaseName = rs.getString(1);
+                if (!getSysDatabases().contains(databaseName)) {
+                    databases.add(rs.getString(1));
+                }
+            }
+
+            return databases;
+        } catch (Exception e) {
+            throw new CatalogException(
+                    String.format("Failed listing database in catalog %s", this.catalogName), e);
+        }
+    }
+
+    @Override
+    public List<String> listTables(String databaseName)
+            throws CatalogException, DatabaseNotExistException {
+        if (!databaseExists(databaseName)) {
+            throw new DatabaseNotExistException(this.catalogName, databaseName);
+        }
+
+        String dbUrl = jdbcDialect.getUrlFromDatabaseName(baseUrl, databaseName, suffix);
+        try (Connection conn = DriverManager.getConnection(dbUrl, username, pwd);
+                PreparedStatement ps =
+                        conn.prepareStatement(jdbcDialect.listTableSql(databaseName))) {
+
+            ResultSet rs = ps.executeQuery();
+
+            List<String> tables = new ArrayList<>();
+
+            StringBuilder fullTableName = new StringBuilder();
+            while (rs.next()) {
+                for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
+                    fullTableName.append(rs.getString(i)).append("\\.");

Review Comment:
   `append` just use string as parameter, not regex. Why append with `\\`?



##########
seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/catalog/sqlserver/SqlServerType.java:
##########
@@ -140,7 +140,7 @@ public String getSqlTypeName(long precision, long scale) {
     public static Pair<SqlServerType, Map<String, Object>> parse(String fullTypeName) {
         Map<String, Object> params = new HashMap<>();
         String typeName = fullTypeName;
-        if (fullTypeName.indexOf("(") != -1) {
+        if (fullTypeName.contains("(")) {

Review Comment:
   @hailin0 PTAL



##########
seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/JdbcDialect.java:
##########
@@ -193,4 +195,36 @@ default ResultSetMetaData getResultSetMetaData(
         PreparedStatement ps = conn.prepareStatement(jdbcSourceConfig.getQuery());
         return ps.getMetaData();
     }
+
+    default String listDatabases() {
+        return "SHOW DATABASES;";
+    }
+
+    default String getUrlFromDatabaseName(String baseUrl, String databaseName, String suffix) {
+        return baseUrl + databaseName + suffix;
+    }
+
+    default String createDatabaseSql(String databaseName) {
+        return String.format("CREATE DATABASE IF NOT EXISTS %s;", quoteIdentifier(databaseName));
+    }
+
+    default String dropDatabaseSql(String databaseName) {
+        return String.format("DROP DATABASE IF EXISTS %s;", quoteIdentifier(databaseName));
+    }
+
+    default String listTableSql(String databaseName) {
+        return "SHOW TABLES;";
+    }
+
+    default String getDropTableSql(String tableName) {
+        return String.format("DROP TABLE %s IF EXIST;", tableName);
+    }
+
+    default String createTableSql(TablePath tablePath, CatalogTable catalogTable) {
+        return "";

Review Comment:
   `UnsupportedOperationException`



##########
seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/catalog/AbstractJdbcCatalog.java:
##########
@@ -286,5 +446,69 @@ public void dropDatabase(TablePath tablePath, boolean ignoreIfNotExists)
         }
     }
 
-    protected abstract boolean dropDatabaseInternal(String databaseName) throws CatalogException;
+    protected SeaTunnelDataType<?> fromJdbcType(ResultSetMetaData metadata, int colIndex)
+            throws SQLException {
+        return null;

Review Comment:
   Why return `null`? Just use `UnsupportedOperationException`.



##########
seatunnel-connectors-v2/connector-starrocks/src/main/java/org/apache/seatunnel/connectors/seatunnel/starrocks/catalog/StarRocksDialect.java:
##########
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.seatunnel.connectors.seatunnel.starrocks.catalog;
+
+import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.mysql.MysqlDialect;
+
+public class StarRocksDialect extends MysqlDialect {

Review Comment:
   Only override name? StarRocks have many different with MySQL. Like create table sql, row type converter so on. Maybe you should test with StarRocks.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org