Skip to content

Commit c60c24b

Browse files
committed
changes for issue 1362
1 parent 014a0e0 commit c60c24b

File tree

3 files changed

+139
-77
lines changed

3 files changed

+139
-77
lines changed

chat2db-server/chat2db-plugins/chat2db-clickhouse/src/main/java/ai/chat2db/plugin/clickhouse/builder/ClickHouseSqlBuilder.java

Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,100 @@
1414
public class ClickHouseSqlBuilder extends DefaultSqlBuilder {
1515
@Override
1616
public String buildCreateTableSql(Table table) {
17-
StringBuilder script = new StringBuilder();
18-
script.append("CREATE TABLE ");
19-
if (StringUtils.isNotBlank(table.getDatabaseName())) {
20-
script.append("`").append(table.getDatabaseName()).append("`").append(".");
21-
}
17+
// Initialize StringBuilder to build the SQL script
18+
StringBuilder script = new StringBuilder("CREATE TABLE ");
19+
20+
// Append the database name, if present
21+
appendDatabaseName(script, table.getDatabaseName());
22+
23+
// Append the table name
2224
script.append("`").append(table.getName()).append("`").append(" (").append("\n");
2325

2426
// append column
25-
for (TableColumn column : table.getColumnList()) {
26-
if (StringUtils.isBlank(column.getName()) || StringUtils.isBlank(column.getColumnType())) {
27-
continue;
28-
}
29-
ClickHouseColumnTypeEnum typeEnum = ClickHouseColumnTypeEnum.getByType(column.getColumnType());
30-
script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n");
31-
}
27+
appendColumns(script, table.getColumnList());
3228

3329
// append index
34-
for (TableIndex tableIndex : table.getIndexList()) {
35-
if (StringUtils.isBlank(tableIndex.getName()) || StringUtils.isBlank(tableIndex.getType())) {
36-
continue;
37-
}
38-
ClickHouseIndexTypeEnum mysqlIndexTypeEnum = ClickHouseIndexTypeEnum.getByType(tableIndex.getType());
39-
if (!ClickHouseIndexTypeEnum.PRIMARY.equals(mysqlIndexTypeEnum) ) {
40-
script.append("\t").append("").append(mysqlIndexTypeEnum.buildIndexScript(tableIndex)).append(",\n");
41-
}
42-
}
30+
appendIndexes(script, table.getIndexList());
4331

32+
// Remove the last comma
4433
script = new StringBuilder(script.substring(0, script.length() - 2));
4534
script.append("\n)");
4635

36+
// Append the engine, if present
37+
appendEngine(script, table.getEngine());
4738

48-
if (StringUtils.isNotBlank(table.getEngine())) {
49-
script.append(" ENGINE=").append(table.getEngine()).append("\n");
39+
// append primary key
40+
appendPrimaryKey(script, table.getIndexList());
41+
42+
// Append the comment, if present
43+
appendComment(script, table.getComment());
44+
45+
// Append a semicolon to complete the SQL statement
46+
script.append(";");
47+
48+
// Return the complete SQL script
49+
return script.toString();
50+
}
51+
52+
// Method to append the database name to the SQL script
53+
private void appendDatabaseName(StringBuilder script, String databaseName) {
54+
if (StringUtils.isNotBlank(databaseName)) {
55+
script.append("`").append(databaseName).append("`.");
5056
}
51-
// append primary key
52-
for (TableIndex tableIndex : table.getIndexList()) {
53-
if (StringUtils.isBlank(tableIndex.getName()) || StringUtils.isBlank(tableIndex.getType())) {
54-
continue;
57+
}
58+
59+
// Method to append columns to the SQL script
60+
private void appendColumns(StringBuilder script, List<TableColumn> columns) {
61+
for (TableColumn column : columns) {
62+
// Check if column name and type are not blank
63+
if (StringUtils.isNotBlank(column.getName()) && StringUtils.isNotBlank(column.getColumnType())) {
64+
// Get the column type enum and append the column SQL to the script
65+
ClickHouseColumnTypeEnum typeEnum = ClickHouseColumnTypeEnum.getByType(column.getColumnType());
66+
script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n");
5567
}
56-
ClickHouseIndexTypeEnum mysqlIndexTypeEnum = ClickHouseIndexTypeEnum.getByType(tableIndex.getType());
57-
if (ClickHouseIndexTypeEnum.PRIMARY.equals(mysqlIndexTypeEnum) ) {
58-
script.append("\t").append("").append(mysqlIndexTypeEnum.buildIndexScript(tableIndex)).append("\n");
68+
}
69+
}
70+
71+
// Method to append indexes to the SQL script
72+
private void appendIndexes(StringBuilder script, List<TableIndex> indexes) {
73+
for (TableIndex index : indexes) {
74+
// Check if index name and type are not blank
75+
if (StringUtils.isNotBlank(index.getName()) && StringUtils.isNotBlank(index.getType())) {
76+
// Get the index type enum and append the index script to the script
77+
ClickHouseIndexTypeEnum indexTypeEnum = ClickHouseIndexTypeEnum.getByType(index.getType());
78+
if (!ClickHouseIndexTypeEnum.PRIMARY.equals(indexTypeEnum)) {
79+
script.append("\t").append(indexTypeEnum.buildIndexScript(index)).append(",\n");
80+
}
5981
}
6082
}
83+
}
6184

62-
if (StringUtils.isNotBlank(table.getComment())) {
63-
script.append(" COMMENT '").append(table.getComment()).append("'");
85+
// Method to append the engine to the SQL script
86+
private void appendEngine(StringBuilder script, String engine) {
87+
if (StringUtils.isNotBlank(engine)) {
88+
script.append(" ENGINE=").append(engine).append("\n");
6489
}
90+
}
6591

66-
script.append(";");
92+
// Method to append the primary key to the SQL script
93+
private void appendPrimaryKey(StringBuilder script, List<TableIndex> indexes) {
94+
for (TableIndex index : indexes) {
95+
// Check if index name and type are not blank
96+
if (StringUtils.isNotBlank(index.getName()) && StringUtils.isNotBlank(index.getType())) {
97+
// Get the index type enum and append the index script to the script
98+
ClickHouseIndexTypeEnum indexTypeEnum = ClickHouseIndexTypeEnum.getByType(index.getType());
99+
if (ClickHouseIndexTypeEnum.PRIMARY.equals(indexTypeEnum)) {
100+
script.append("\t").append(indexTypeEnum.buildIndexScript(index)).append("\n");
101+
}
102+
}
103+
}
104+
}
67105

68-
return script.toString();
106+
// Method to append the comment to the SQL script
107+
private void appendComment(StringBuilder script, String comment) {
108+
if (StringUtils.isNotBlank(comment)) {
109+
script.append(" COMMENT '").append(comment).append("'");
110+
}
69111
}
70112

71113
@Override

chat2db-server/chat2db-plugins/chat2db-h2/src/main/java/ai/chat2db/plugin/h2/H2Meta.java

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,33 @@ public String tableDDL(Connection connection, @NotEmpty String databaseName, Str
3333
}
3434

3535
private String getDDL(Connection connection, String databaseName, String schemaName, String tableName) {
36-
try {
37-
// Query table structure information
38-
ResultSet columns = connection.getMetaData().getColumns(databaseName, schemaName, tableName, null);
39-
List<String> columnDefinitions = new ArrayList<>();
36+
37+
try{
38+
List<String> columnDefinitions = getColumnDefinitions(connection, databaseName, schemaName, tableName);
39+
Map<String, List<String>> indexMap = getIndexInfo(connection, databaseName, schemaName, tableName);
40+
StringBuilder createTableDDL = new StringBuilder("CREATE TABLE ");
41+
createTableDDL.append(tableName).append(" (\n");
42+
createTableDDL.append(String.join(",\n", columnDefinitions));
43+
createTableDDL.append("\n);\n");
44+
45+
// Output index information
46+
for (Map.Entry<String, List<String>> entry : indexMap.entrySet()) {
47+
String indexName = entry.getKey();
48+
List<String> columnList = entry.getValue();
49+
String indexColumns = String.join(", ", columnList);
50+
String createIndexDDL = String.format("CREATE INDEX %s ON %s (%s);", indexName, tableName, indexColumns);
51+
createTableDDL.append(createIndexDDL);
52+
}
53+
return createTableDDL.toString();
54+
} catch (Exception e) {
55+
e.printStackTrace();
56+
}
57+
return "";
58+
}
59+
60+
private List<String> getColumnDefinitions(Connection connection, String databaseName, String schemaName, String tableName) {
61+
List<String> columnDefinitions = new ArrayList<>();
62+
try (ResultSet columns = connection.getMetaData().getColumns(databaseName, schemaName, tableName, null)) {
4063
while (columns.next()) {
4164
String columnName = columns.getString("COLUMN_NAME");
4265
String columnType = columns.getString("TYPE_NAME");
@@ -58,40 +81,26 @@ private String getDDL(Connection connection, String databaseName, String schemaN
5881
}
5982
columnDefinitions.add(columnDefinition.toString());
6083
}
84+
} catch (Exception e) {
85+
throw new RuntimeException(e);
86+
}
87+
return columnDefinitions;
88+
}
6189

62-
// Query table index information
63-
ResultSet indexes = connection.getMetaData().getIndexInfo(databaseName, schemaName, tableName, false,
64-
false);
65-
Map<String, List<String>> indexMap = new HashMap<>();
90+
private Map<String, List<String>> getIndexInfo(Connection connection, String databaseName, String schemaName, String tableName) {
91+
Map<String, List<String>> indexMap = new HashMap<>();
92+
try (ResultSet indexes = connection.getMetaData().getIndexInfo(databaseName, schemaName, tableName, false, false)) {
6693
while (indexes.next()) {
6794
String indexName = indexes.getString("INDEX_NAME");
6895
String columnName = indexes.getString("COLUMN_NAME");
6996
if (indexName != null) {
70-
if (!indexMap.containsKey(indexName)) {
71-
indexMap.put(indexName, new ArrayList<>());
72-
}
73-
indexMap.get(indexName).add(columnName);
97+
indexMap.computeIfAbsent(indexName, k -> new ArrayList<>()).add(columnName);
7498
}
7599
}
76-
StringBuilder createTableDDL = new StringBuilder("CREATE TABLE ");
77-
createTableDDL.append(tableName).append(" (\n");
78-
createTableDDL.append(String.join(",\n", columnDefinitions));
79-
createTableDDL.append("\n);\n");
80-
// Output index information
81-
for (Map.Entry<String, List<String>> entry : indexMap.entrySet()) {
82-
String indexName = entry.getKey();
83-
List<String> columnList = entry.getValue();
84-
String indexColumns = String.join(", ", columnList);
85-
String createIndexDDL = String.format("CREATE INDEX %s ON %s (%s);", indexName, tableName,
86-
indexColumns);
87-
createTableDDL.append(createIndexDDL);
88-
}
89-
return createTableDDL.toString();
90-
91-
} catch (Exception e) {
92-
e.printStackTrace();
100+
} catch (SQLException e) {
101+
throw new RuntimeException(e);
93102
}
94-
return "";
103+
return indexMap;
95104
}
96105

97106
private static String ROUTINES_SQL

chat2db-server/chat2db-plugins/chat2db-hive/src/main/java/ai/chat2db/plugin/hive/builder/HiveSqlBuilder.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,41 @@ public String buildCreateTableSql(Table table) {
2727
script.append("`").append(table.getName()).append("`").append(" (").append("\n");
2828

2929
// append column
30-
for (TableColumn column : table.getColumnList()) {
30+
appendColumns(script, table.getColumnList());
31+
32+
// append primary key and index
33+
appendIndexes(script, table.getIndexList());
34+
script = new StringBuilder(script.substring(0, script.length() - 2));
35+
script.append("\n)");
36+
37+
// append engine, charset, collate, auto_increment, comment, and partition
38+
appendTableOptions(script, table);
39+
script.append(";");
40+
41+
return script.toString();
42+
}
43+
44+
private void appendColumns(StringBuilder script, List<TableColumn> columns) {
45+
for (TableColumn column : columns) {
3146
if (StringUtils.isBlank(column.getName()) || StringUtils.isBlank(column.getColumnType())) {
3247
continue;
3348
}
3449
HiveColumnTypeEnum typeEnum = HiveColumnTypeEnum.getByType(column.getColumnType());
3550
script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n");
3651
}
52+
}
3753

38-
// append primary key and index
39-
for (TableIndex tableIndex : table.getIndexList()) {
40-
if (StringUtils.isBlank(tableIndex.getName()) || StringUtils.isBlank(tableIndex.getType())) {
54+
private void appendIndexes(StringBuilder script, List<TableIndex> indexes) {
55+
for (TableIndex index : indexes) {
56+
if (StringUtils.isBlank(index.getName()) || StringUtils.isBlank(index.getType())) {
4157
continue;
4258
}
43-
HiveIndexTypeEnum hiveIndexTypeEnum = HiveIndexTypeEnum.getByType(tableIndex.getType());
44-
script.append("\t").append("").append(hiveIndexTypeEnum.buildIndexScript(tableIndex)).append(",\n");
59+
HiveIndexTypeEnum hiveIndexTypeEnum = HiveIndexTypeEnum.getByType(index.getType());
60+
script.append("\t").append("").append(hiveIndexTypeEnum.buildIndexScript(index)).append(",\n");
4561
}
62+
}
4663

47-
script = new StringBuilder(script.substring(0, script.length() - 2));
48-
script.append("\n)");
49-
50-
64+
private void appendTableOptions(StringBuilder script, Table table) {
5165
if (StringUtils.isNotBlank(table.getEngine())) {
5266
script.append(" ENGINE=").append(table.getEngine());
5367
}
@@ -71,9 +85,6 @@ public String buildCreateTableSql(Table table) {
7185
if (StringUtils.isNotBlank(table.getPartition())) {
7286
script.append(" \n").append(table.getPartition());
7387
}
74-
script.append(";");
75-
76-
return script.toString();
7788
}
7889

7990
@Override

0 commit comments

Comments
 (0)