You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/12/10 04:41:31 UTC

[GitHub] [pulsar] tisonkun commented on a diff in pull request #2440: Issue 2313: create a JDBC sink connector

tisonkun commented on code in PR #2440:
URL: https://github.com/apache/pulsar/pull/2440#discussion_r1044984279


##########
pulsar-io/jdbc/src/main/java/org/apache/pulsar/io/jdbc/JdbcUtils.java:
##########
@@ -0,0 +1,178 @@
+/**
+ * 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.pulsar.io.jdbc;
+
+import static com.google.common.base.Preconditions.checkState;
+
+import com.google.common.collect.Lists;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.List;
+import java.util.Properties;
+import java.util.stream.IntStream;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * Jdbc Utils
+ */
+@Slf4j
+public class JdbcUtils {
+
+    @Data(staticConstructor = "of")
+    @Setter
+    @Getter
+    @EqualsAndHashCode
+    @ToString
+    public static class TableId {
+        private final String catalogName;
+        private final String schemaName;
+        private final String tableName;
+    }
+
+    @Data(staticConstructor = "of")
+    @Setter
+    @Getter
+    @EqualsAndHashCode
+    @ToString
+    public static class ColumnId {
+        private final TableId tableId;
+        private final String name;
+        // SQL type from java.sql.Types
+        private final int type;
+        private final String typeName;
+        // column position in table
+        private final int position;
+    }
+
+    @Data(staticConstructor = "of")
+    @Setter
+    @Getter
+    @EqualsAndHashCode
+    @ToString
+    public static class TableDefinition {
+        private final TableId tableId;
+        private final List<ColumnId> columns;
+    }
+
+    /**
+     * Given a driver type(such as mysql), return its jdbc driver class name.
+     * TODO: test and support more types, also add Driver in pom file.
+     */
+    public static String getDriverClassName(String driver) throws Exception {
+        if (driver.equals("mysql")) {
+            return "com.mysql.jdbc.Driver";
+        } if (driver.equals("sqlite")) {
+            return "org.sqlite.JDBC";
+        } else {
+            throw new Exception("Not tested jdbc driver type: " + driver);
+        }
+    }
+
+    /**
+     * Get the {@link Connection} for the given jdbcUrl.
+     */
+    public static Connection getConnection(String jdbcUrl, Properties properties) throws Exception {
+        String driver = jdbcUrl.split(":")[1];
+        String driverClassName = getDriverClassName(driver);
+        Class.forName(driverClassName);
+
+        return DriverManager.getConnection(jdbcUrl, properties);
+    }
+
+    /**
+     * Get the {@link TableId} for the given tableName.
+     */
+    public static TableId getTableId(Connection connection, String tableName) throws Exception {
+        DatabaseMetaData metadata = connection.getMetaData();
+        try (ResultSet rs = metadata.getTables(null, null, tableName, new String[]{"TABLE"})) {

Review Comment:
   Why do we return only "TABLE" in this case?



-- 
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@pulsar.apache.org

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