libenchao commented on code in PR #22362:
URL: https://github.com/apache/flink/pull/22362#discussion_r1175257104


##########
flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkDatabaseMetaData.java:
##########
@@ -0,0 +1,402 @@
+/*
+ * 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.flink.table.jdbc;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.table.client.gateway.Executor;
+import org.apache.flink.table.client.gateway.StatementResult;
+
+import javax.annotation.Nullable;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static 
org.apache.flink.table.jdbc.utils.DatabaseMetaDataUtils.createCatalogsResultSet;
+import static 
org.apache.flink.table.jdbc.utils.DatabaseMetaDataUtils.createSchemasResultSet;
+
+/** Implementation of {@link java.sql.DatabaseMetaData} for flink jdbc driver. 
*/
+public class FlinkDatabaseMetaData extends BaseDatabaseMetaData {
+    private final String url;
+    private final FlinkConnection connection;
+    private final Statement statement;
+    private final Executor executor;
+
+    @VisibleForTesting
+    public FlinkDatabaseMetaData(String url, FlinkConnection connection, 
Statement statement) {
+        this.url = url;
+        this.connection = connection;
+        this.statement = statement;
+        this.executor = connection.getExecutor();
+    }
+
+    @Override
+    public ResultSet getCatalogs() throws SQLException {
+        try (StatementResult result = catalogs()) {
+            return createCatalogsResultSet(statement, result);
+        } catch (Exception e) {
+            throw new SQLException("Get catalogs fail", e);
+        }
+    }
+
+    private StatementResult catalogs() {
+        return executor.executeStatement("SHOW CATALOGS");
+    }
+
+    @Override
+    public ResultSet getSchemas() throws SQLException {
+        try {
+            String currentCatalog = connection.getCatalog();
+            String currentDatabase = connection.getSchema();
+            List<String> catalogList = new ArrayList<>();
+            Map<String, List<String>> catalogSchemaList = new HashMap<>();
+            try (StatementResult result = catalogs()) {
+                while (result.hasNext()) {
+                    String catalog = result.next().getString(0).toString();
+                    getSchemasForCatalog(catalogList, catalogSchemaList, 
catalog, null);
+                }
+            }
+            connection.setCatalog(currentCatalog);
+            connection.setSchema(currentDatabase);
+
+            return createSchemasResultSet(statement, catalogList, 
catalogSchemaList);
+        } catch (Exception e) {
+            throw new SQLException("Get schemas fail", e);
+        }
+    }
+
+    private void getSchemasForCatalog(
+            List<String> catalogList,
+            Map<String, List<String>> catalogSchemaList,
+            String catalog,
+            @Nullable String schemaPattern)
+            throws SQLException {
+        catalogList.add(catalog);
+        connection.setCatalog(catalog);
+
+        List<String> schemas = new ArrayList<>();
+        try (StatementResult schemaResult = schemas()) {
+            while (schemaResult.hasNext()) {
+                String schema = schemaResult.next().getString(0).toString();
+                if (schemaPattern == null || schema.contains(schemaPattern)) {

Review Comment:
   I'm not saying we must use that to implement this. What I meant is the 
semantic of your implementation is different from 'like xxx', e.g. we have 
schemas: "ab", "abc", "abcd", and schemaPattern is "a", your implementation 
will return all three schemas, and "show databases like 'a'" will return empty, 
if you want to match all the three schemas, you need to use "show databases 
like 'a%'"



-- 
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: issues-unsubscr...@flink.apache.org

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

Reply via email to