Skip to content

Commit

Permalink
ck创建表语句校验优化
Browse files Browse the repository at this point in the history
  • Loading branch information
coderTomato committed Dec 17, 2021
1 parent 7451281 commit f69bb4a
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.dlink.metadata.driver;

import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
Expand All @@ -22,6 +21,8 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* ClickHouseDriver
Expand Down Expand Up @@ -105,6 +106,11 @@ public List<SqlExplainResult> explain(String sql){
String type = item.getClass().getSimpleName();
if(!(item instanceof SQLSelectStatement)){
if(item instanceof Clickhouse20CreateTableStatement){
Matcher m = Pattern.compile(",\\s*\\)").matcher(sql);
if (m.find()) {
sqlExplainResults.add(SqlExplainResult.fail(sql, "No comma can be added to the last field of Table! "));
break;
}
sqlExplainResults.add(checkCreateTable((Clickhouse20CreateTableStatement)item));
} else if(item instanceof SQLDropTableStatement){
sqlExplainResults.add(checkDropTable((SQLDropTableStatement)item));
Expand All @@ -131,7 +137,12 @@ public List<SqlExplainResult> explain(String sql){

private SqlExplainResult checkCreateTable(Clickhouse20CreateTableStatement sqlStatement){
if(existTable(Table.build(sqlStatement.getTableName()))){
return SqlExplainResult.fail(sqlStatement.toString(), "Table "+sqlStatement.getSchema()+"."+sqlStatement.getTableName()+" already exists.");
if(sqlStatement.isIfNotExists()){
return SqlExplainResult.success(sqlStatement.getClass().getSimpleName(), sqlStatement.toString(), null);
}else{
String schema = null == sqlStatement.getSchema() ? "" : sqlStatement.getSchema()+".";
return SqlExplainResult.fail(sqlStatement.toString(), "Table "+schema+sqlStatement.getTableName()+" already exists.");
}
}else{
return SqlExplainResult.success(sqlStatement.getClass().getSimpleName(), sqlStatement.toString(), null);
}
Expand Down

0 comments on commit f69bb4a

Please sign in to comment.