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/31 15:05:45 UTC

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

haibo-duan commented on code in PR #5263:
URL: https://github.com/apache/inlong/pull/5263#discussion_r934000078


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/sqlserver/SQLServerSqlBuilder.java:
##########
@@ -0,0 +1,255 @@
+/*
+ * 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.sink.sqlserver;
+
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.inlong.manager.pojo.sink.sqlserver.SQLServerColumnInfo;
+import org.apache.inlong.manager.pojo.sink.sqlserver.SQLServerTableInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Builder the SQL string for SQLServer.
+ */
+public class SQLServerSqlBuilder {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(SQLServerSqlBuilder.class);
+
+    /**
+     * Build SQL to check whether the table exists.
+     *
+     * @param schemaName SQLServer schema name
+     * @param tableName SQLServer table name
+     * @return the check table SQL string
+     */
+    public static String getCheckTable(String schemaName, String tableName) {
+        StringBuilder sqlBuilder = new StringBuilder();
+        sqlBuilder.append("SELECT COUNT(1) ")
+                .append(" FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '")
+                .append(schemaName)
+                .append("' AND TABLE_NAME = '")
+                .append(tableName)
+                .append("' ;");
+        LOGGER.info("check table sql: {}", sqlBuilder);
+        return sqlBuilder.toString();
+    }
+
+    /**
+     * Build SQL to check whether the column exists.
+     *
+     * @param schemaName SQLServer schema name
+     * @param tableName SQLServer table name
+     * @param columnName SQLServer column name
+     * @return the check column SQL string
+     */
+    public static String getCheckColumn(String schemaName, String tableName, String columnName) {
+        StringBuilder sqlBuilder = new StringBuilder();
+        sqlBuilder.append("SELECT COUNT(1) ")
+                .append(" FROM  INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='")
+                .append(schemaName)
+                .append("' AND TABLE_NAME = '")
+                .append(tableName)
+                .append("' AND COLUMN_NAME = '")
+                .append(columnName)
+                .append("';");
+        LOGGER.info("check table sql: {}", sqlBuilder);
+        return sqlBuilder.toString();
+    }
+
+    /**
+     * Build SQL to check whether the schema exists.
+     *
+     * @param schemaName
+     * @return
+     */
+    public static String getCheckSchema(String schemaName) {
+        StringBuilder sqlBuilder = new StringBuilder();
+        sqlBuilder.append("SELECT COUNT(1) FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME ='")
+                .append(schemaName)
+                .append("';");
+        LOGGER.info("check schema sql: {}", sqlBuilder);
+        return sqlBuilder.toString();
+    }
+
+    /**
+     * Build create schema SQL.
+     *
+     * @param schemaName SQLServer schema name
+     * @return
+     */
+    public static String buildCreateSchemaSql(String schemaName) {
+        return new StringBuilder()
+                .append("CREATE SCHEMA \"")
+                .append(schemaName)
+                .append("\" AUTHORIZATION dbo ;")
+                .toString();
+    }
+
+    /**
+     * Build create table SQL by SQLServerTableInfo.
+     *
+     * @param table SQLServer table info {@link SQLServerTableInfo}
+     * @return the create table SQL String
+     */
+    public static List<String> buildCreateTableSql(SQLServerTableInfo table) {
+        List<String> sqls = new ArrayList<>();
+        StringBuilder sql = new StringBuilder();
+        // Support _ beginning with underscore
+        sql.append("CREATE TABLE ").append(table.getSchemaName())
+                .append(".")
+                .append(table.getTableName());
+
+        // Construct columns and partition columns
+        sql.append(buildCreateColumnsSql(table));
+        sqls.add(sql.toString());
+        table.getColumns().stream().filter(column -> StringUtils.isNotEmpty(column.getComment()))

Review Comment:
   OK



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