diff --git a/dlink-metadata/dlink-metadata-clickhouse/src/main/java/com/dlink/metadata/driver/ClickHouseDriver.java b/dlink-metadata/dlink-metadata-clickhouse/src/main/java/com/dlink/metadata/driver/ClickHouseDriver.java
index 4b5e587def..629c0f64b9 100644
--- a/dlink-metadata/dlink-metadata-clickhouse/src/main/java/com/dlink/metadata/driver/ClickHouseDriver.java
+++ b/dlink-metadata/dlink-metadata-clickhouse/src/main/java/com/dlink/metadata/driver/ClickHouseDriver.java
@@ -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;
@@ -14,6 +19,7 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
+import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@@ -49,6 +55,32 @@ public String getName() {
return "ClickHouse OLAP 数据库";
}
+ @Override
+ public List
listTables(String schemaName) {
+ List 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;
@@ -72,7 +104,13 @@ public List 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);
@@ -90,4 +128,21 @@ public List 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);
+ }
+ }
}
diff --git a/dlink-metadata/dlink-metadata-clickhouse/src/main/java/com/dlink/metadata/parser/Clickhouse20CreateTableParser.java b/dlink-metadata/dlink-metadata-clickhouse/src/main/java/com/dlink/metadata/parser/Clickhouse20CreateTableParser.java
index 474f5e0757..e07b10fe1c 100644
--- a/dlink-metadata/dlink-metadata-clickhouse/src/main/java/com/dlink/metadata/parser/Clickhouse20CreateTableParser.java
+++ b/dlink-metadata/dlink-metadata-clickhouse/src/main/java/com/dlink/metadata/parser/Clickhouse20CreateTableParser.java
@@ -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);
diff --git a/dlink-metadata/dlink-metadata-clickhouse/src/main/java/com/dlink/metadata/query/ClickHouseQuery.java b/dlink-metadata/dlink-metadata-clickhouse/src/main/java/com/dlink/metadata/query/ClickHouseQuery.java
index 28fd434946..38c5b99f2a 100644
--- a/dlink-metadata/dlink-metadata-clickhouse/src/main/java/com/dlink/metadata/query/ClickHouseQuery.java
+++ b/dlink-metadata/dlink-metadata-clickhouse/src/main/java/com/dlink/metadata/query/ClickHouseQuery.java
@@ -17,7 +17,7 @@ public String schemaAllSql() {
@Override
public String tablesSql(String schemaName) {
- return "show table status WHERE 1=1 ";
+ return "show tables";
}