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/07/30 03:51:18 UTC

[GitHub] [inlong] healchow commented on a diff in pull request #5263: [INLONG-5228][Manager] Support create SqlServer databases and tables

healchow commented on code in PR #5263:
URL: https://github.com/apache/inlong/pull/5263#discussion_r933726177


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sqlserver/SQLServerJdbcUtils.java:
##########
@@ -0,0 +1,289 @@
+/*
+ * 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.sqlserver;
+
+import com.google.common.collect.Lists;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.inlong.manager.common.pojo.sink.sqlserver.SQLServerColumnInfo;
+import org.apache.inlong.manager.common.pojo.sink.sqlserver.SQLServerTableInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utils for SqlServer JDBC.
+ */
+public class SQLServerJdbcUtils {
+
+    private static final String SQLSERVER_JDBC_PREFIX = "jdbc:sqlserver";
+
+    private static final String SQLSERVER_DRIVER_CLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
+
+    private static final Logger LOG = LoggerFactory.getLogger(SQLServerJdbcUtils.class);
+
+    /**
+     * Get SqlServer connection from the url and user.
+     *
+     * @param url jdbc url, such as jdbc:sqlserver://host:port
+     * @param user Username for JDBC URL
+     * @param password User password
+     * @return {@link Connection}
+     * @throws Exception on get connection error
+     */
+    public static Connection getConnection(String url, String user, String password)
+            throws Exception {
+        if (StringUtils.isBlank(url) || !url.startsWith(SQLSERVER_JDBC_PREFIX)) {
+            throw new Exception("SqlServer server URL was invalid, it should start with jdbc:sqlserver");
+        }
+        Connection conn;
+        try {
+            Class.forName(SQLSERVER_DRIVER_CLASS);
+            conn = DriverManager.getConnection(url, user, password);
+        } catch (Exception e) {
+            String errorMsg = "get SqlServer connection error, please check SqlServer JDBC url, username or password!";
+            LOG.error(errorMsg, e);
+            throw new Exception(errorMsg + " other error msg: " + e.getMessage());
+        }
+        if (conn == null) {
+            throw new Exception("get SqlServer connection failed, please contact administrator.");
+        }
+        LOG.info("get SqlServer connection success, url={}", url);
+        return conn;
+    }
+
+    /**
+     * Execute SQL command on SqlServer.
+     *
+     * @param conn JDBC Connection  {@link Connection}
+     * @param sql SQL string to be executed
+     * @throws Exception on execute SQL error
+     */
+    public static void executeSql(Connection conn, String sql) throws Exception {
+        conn.setAutoCommit(true);
+        Statement stmt = conn.createStatement();
+        LOG.info("execute sql [{}] success !", sql);
+        stmt.execute(sql);
+        stmt.close();
+    }
+
+    /**
+     * Execute query SQL on SqlServer.
+     *
+     * @param conn JDBC Connection  {@link Connection}
+     * @param sql SQL string to be executed
+     * @return {@link ResultSet}
+     * @throws Exception on execute query SQL error
+     */
+    public static ResultSet executeQuerySql(Connection conn, String sql)
+            throws Exception {
+        Statement stmt = conn.createStatement();
+        LOG.info("execute sql [{}] success !", sql);
+        return stmt.executeQuery(sql);
+    }
+
+    /**
+     * Execute batch query SQL on SqlServer.
+     *
+     * @param conn JDBC Connection  {@link Connection}
+     * @param sqls SQL string to be executed
+     * @throws Exception on get execute SQL batch error
+     */
+    public static void executeSqlBatch(Connection conn, List<String> sqls)
+            throws Exception {
+        conn.setAutoCommit(false);
+        Statement stmt = conn.createStatement();
+        for (String entry : sqls) {
+            stmt.execute(entry);
+        }
+        conn.commit();
+        stmt.close();
+        conn.setAutoCommit(true);
+        LOG.info("execute sql [{}] success! ", sqls);
+    }
+
+    /**
+     * create SqlServer schema
+     *
+     * @param conn JDBC Connection  {@link Connection}
+     * @param schemaName SqlServer schema name
+     * @throws Exception on create schema error
+     */
+    public static void createSchema(Connection conn, String schemaName) throws Exception {
+        if (!checkSchemaExist(conn, schemaName)) {
+            String createSql = SQLServerSqlBuilder.buildCreateSchemaSql(schemaName);
+            executeSql(conn, createSql);
+            LOG.info("execute sql [{}] success! ", createSql);

Review Comment:
   No need to add blank or punctuation at the end of log info.



-- 
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