You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by GitBox <gi...@apache.org> on 2020/06/04 03:20:30 UTC

[GitHub] [incubator-dolphinscheduler] Rubik-W commented on a change in pull request #2879: Feature data lineage (table) #249

Rubik-W commented on a change in pull request #2879:
URL: https://github.com/apache/incubator-dolphinscheduler/pull/2879#discussion_r434969759



##########
File path: dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/SqlUtils.java
##########
@@ -0,0 +1,204 @@
+/*
+ * 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.dolphinscheduler.common.utils;
+
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.dolphinscheduler.common.enums.DbType;
+import org.datanucleus.store.rdbms.exceptions.UnsupportedDataTypeException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.alibaba.druid.sql.SQLUtils;
+import com.alibaba.druid.sql.ast.SQLStatement;
+import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
+import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
+import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
+import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor;
+import com.alibaba.druid.sql.parser.Token;
+import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
+import com.alibaba.druid.stat.TableStat;
+
+
+public class SqlUtils {
+
+    public static final Logger logger = LoggerFactory.getLogger(SqlUtils.class);
+
+    /**
+     * 解析sql语句select from表名
+     *
+     * @param dbType database type
+     * @param sql select sql
+     * @return table List
+     * @throws UnsupportedDataTypeException
+     */
+    public static List<String> resolveSqlSelectTables(DbType dbType, String sql) throws SQLException {
+        return resolveSqlTables(dbType, sql).get(Token.SELECT);
+    }
+
+    /**
+     * 解析sql语句insert表名
+     *
+     * @param dbType database type
+     * @param sql select sql
+     * @return table List
+     * @throws UnsupportedDataTypeException
+     */
+    public static List<String> resolveSqlInsertTables(DbType dbType, String sql) throws SQLException {
+        return resolveSqlTables(dbType, sql).get(Token.INSERT);
+    }
+
+    /**
+     * 解析sql语句update表名
+     *
+     * @param dbType database type
+     * @param sql select sql
+     * @return table List
+     * @throws UnsupportedDataTypeException
+     */
+    public static List<String> resolveSqlUpdateTables(DbType dbType, String sql) throws SQLException {
+        return resolveSqlTables(dbType, sql).get(Token.UPDATE);
+    }
+
+    /**
+     * 解析sql语句delete表名
+     *

Review comment:
       fixed




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

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