Skip to content

Commit

Permalink
ck表存在验证
Browse files Browse the repository at this point in the history
  • Loading branch information
aiwenmo committed Dec 17, 2021
1 parent 279df25 commit 7451281
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
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;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.Token;
import com.dlink.assertion.Asserts;
import com.dlink.metadata.ast.Clickhouse20CreateTableStatement;
import com.dlink.metadata.convert.ClickHouseTypeConvert;
import com.dlink.metadata.convert.ITypeConvert;
import com.dlink.metadata.parser.Clickhouse20StatementParser;
Expand All @@ -14,6 +19,7 @@

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -49,6 +55,32 @@ public String getName() {
return "ClickHouse OLAP 数据库";
}

@Override
public List<Table> listTables(String schemaName) {
List<Table> tableList = new ArrayList<>();
PreparedStatement preparedStatement = null;
ResultSet results = null;
String sql = getDBQuery().tablesSql(schemaName);
try {
preparedStatement = conn.prepareStatement(sql);
results = preparedStatement.executeQuery();
while (results.next()) {
String tableName = results.getString(getDBQuery().tableName());
if (Asserts.isNotNullString(tableName)) {
Table tableInfo = new Table();
tableInfo.setName(tableName);
tableInfo.setSchema(schemaName);
tableList.add(tableInfo);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(preparedStatement, results);
}
return tableList;
}

@Override
public String getCreateTableSql(Table table) {
return null;
Expand All @@ -72,7 +104,13 @@ public List<SqlExplainResult> explain(String sql){
current = item.toString();
String type = item.getClass().getSimpleName();
if(!(item instanceof SQLSelectStatement)){
sqlExplainResults.add(SqlExplainResult.success(type, current, explain.toString()));
if(item instanceof Clickhouse20CreateTableStatement){
sqlExplainResults.add(checkCreateTable((Clickhouse20CreateTableStatement)item));
} else if(item instanceof SQLDropTableStatement){
sqlExplainResults.add(checkDropTable((SQLDropTableStatement)item));
} else {
sqlExplainResults.add(SqlExplainResult.success(type, current, explain.toString()));
}
continue;
}
preparedStatement = conn.prepareStatement("explain "+current);
Expand All @@ -90,4 +128,21 @@ public List<SqlExplainResult> explain(String sql){
return sqlExplainResults;
}
}

private SqlExplainResult checkCreateTable(Clickhouse20CreateTableStatement sqlStatement){
if(existTable(Table.build(sqlStatement.getTableName()))){
return SqlExplainResult.fail(sqlStatement.toString(), "Table "+sqlStatement.getSchema()+"."+sqlStatement.getTableName()+" already exists.");
}else{
return SqlExplainResult.success(sqlStatement.getClass().getSimpleName(), sqlStatement.toString(), null);
}
}

private SqlExplainResult checkDropTable(SQLDropTableStatement sqlStatement){
SQLExprTableSource sqlExprTableSource = sqlStatement.getTableSources().get(0);
if(!existTable(Table.build(sqlExprTableSource.getTableName()))){
return SqlExplainResult.fail(sqlStatement.toString(), "Table "+sqlExprTableSource.getSchema()+"."+sqlExprTableSource.getTableName()+" not exists.");
}else{
return SqlExplainResult.success(sqlStatement.getClass().getSimpleName(), sqlStatement.toString(), null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ protected void parseCreateTableRest(SQLCreateTableStatement stmt) {
ckStmt.setPartitionBy(expr);
}

if (lexer.identifierEquals("PRIMARY")) {
lexer.nextToken();
accept(Token.KEY);
SQLExpr expr = this.exprParser.expr();
ckStmt.setPrimaryKey(expr);
}

if (lexer.token() == Token.PRIMARY) {
lexer.nextToken();
accept(Token.KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public String schemaAllSql() {

@Override
public String tablesSql(String schemaName) {
return "show table status WHERE 1=1 ";
return "show tables";
}


Expand Down

0 comments on commit 7451281

Please sign in to comment.