You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by GitBox <gi...@apache.org> on 2022/05/25 01:24:48 UTC

[GitHub] [incubator-inlong] baomingyu commented on a diff in pull request #4357: [INLONG-4356][Manager] Support create postgres resource

baomingyu commented on code in PR #4357:
URL: https://github.com/apache/incubator-inlong/pull/4357#discussion_r881112573


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/postgres/PostgresJdbcUtils.java:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.inlong.manager.service.resource.postgres;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.inlong.manager.common.pojo.sink.postgres.PostgresColumnInfo;
+import org.apache.inlong.manager.common.pojo.sink.postgres.PostgresTableInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utils for Postgres JDBC.
+ */
+public class PostgresJdbcUtils {
+
+    private static final String POSTGRES_DRIVER_CLASS = "org.postgresql.Driver";
+    private static final String COLUMN_LABEL = "TABLE_NAME";
+    private static final String POSTGRES_JDBC_PREFIX = "jdbc:postgresql";
+
+    private static final Logger LOG = LoggerFactory.getLogger(PostgresJdbcUtils.class);
+
+    /**
+     * Get Postgres connection from Postgres url and user
+     */
+    public static Connection getConnection(String url, String user, String password) throws Exception {
+        if (StringUtils.isBlank(url) || !url.startsWith(POSTGRES_JDBC_PREFIX)) {
+            throw new Exception("Postgres server URL was invalid, it should start with jdbc:postgresql");
+        }
+        Connection conn;
+        try {
+            Class.forName(POSTGRES_DRIVER_CLASS);
+            conn = DriverManager.getConnection(url, user, password);
+        } catch (Exception e) {
+            LOG.error("get postgres connection error, please check postgres jdbc url, username " +
+                    "or password", e);
+            throw new Exception("get postgres connection error, please check jdbc url, username or password. "
+                    + "other error msg: " + e.getMessage());
+        }
+
+        if (conn == null) {
+            throw new Exception("get postgres connection failed, please contact administrator");
+        }
+
+        LOG.info("get postgres connection success, url={}", url);
+        return conn;
+    }
+
+    /**
+     * Execute One Postgres Sql command
+     */
+    public static boolean executeCheckSql(String sql, String url, String user, String password) throws Exception {
+        try (Connection conn = getConnection(url, user, password)) {
+            Statement stmt = conn.createStatement();
+            LOG.info("execute sql [{}] success for url: {}", sql, url);
+            return stmt.execute(sql);
+        }
+    }
+
+    /**
+     * Execute One Postgres Sql command
+     */
+    public static ResultSet executeSql(String sql, String url, String user, String password) throws Exception {

Review Comment:
   Please look at the implementation logic, which requires different return value types and different execution methods



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

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