Skip to content

Commit 380829c

Browse files
authored
Merge pull request #1403 from arjun12102019/1402_Changes
1402 changes - Format table name
2 parents a4f96cb + f28d169 commit 380829c

File tree

13 files changed

+309
-87
lines changed

13 files changed

+309
-87
lines changed

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

Lines changed: 76 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +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-
if (typeEnum != null){
31-
continue;
32-
}
33-
script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n");
34-
}
27+
appendColumns(script, table.getColumnList());
3528

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

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

36+
// Append the engine, if present
37+
appendEngine(script, table.getEngine());
38+
39+
// append primary key
40+
appendPrimaryKey(script, table.getIndexList());
41+
42+
// Append the comment, if present
43+
appendComment(script, table.getComment());
5044

51-
if (StringUtils.isNotBlank(table.getEngine())) {
52-
script.append(" ENGINE=").append(table.getEngine()).append("\n");
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("`.");
5356
}
54-
// append primary key
55-
for (TableIndex tableIndex : table.getIndexList()) {
56-
if (StringUtils.isBlank(tableIndex.getName()) || StringUtils.isBlank(tableIndex.getType())) {
57-
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");
5867
}
59-
ClickHouseIndexTypeEnum mysqlIndexTypeEnum = ClickHouseIndexTypeEnum.getByType(tableIndex.getType());
60-
if (ClickHouseIndexTypeEnum.PRIMARY.equals(mysqlIndexTypeEnum) ) {
61-
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+
}
6281
}
6382
}
83+
}
6484

65-
if (StringUtils.isNotBlank(table.getComment())) {
66-
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");
6789
}
90+
}
6891

69-
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+
}
70105

71-
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+
}
72111
}
73112

74113
@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
@@ -35,10 +35,33 @@ public String tableDDL(Connection connection, @NotEmpty String databaseName, Str
3535
}
3636

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

64-
// Query table index information
65-
ResultSet indexes = connection.getMetaData().getIndexInfo(databaseName, schemaName, tableName, false,
66-
false);
67-
Map<String, List<String>> indexMap = new HashMap<>();
92+
private Map<String, List<String>> getIndexInfo(Connection connection, String databaseName, String schemaName, String tableName) {
93+
Map<String, List<String>> indexMap = new HashMap<>();
94+
try (ResultSet indexes = connection.getMetaData().getIndexInfo(databaseName, schemaName, tableName, false, false)) {
6895
while (indexes.next()) {
6996
String indexName = indexes.getString("INDEX_NAME");
7097
String columnName = indexes.getString("COLUMN_NAME");
7198
if (indexName != null) {
72-
if (!indexMap.containsKey(indexName)) {
73-
indexMap.put(indexName, new ArrayList<>());
74-
}
75-
indexMap.get(indexName).add(columnName);
99+
indexMap.computeIfAbsent(indexName, k -> new ArrayList<>()).add(columnName);
76100
}
77101
}
78-
StringBuilder createTableDDL = new StringBuilder("CREATE TABLE ");
79-
createTableDDL.append(tableName).append(" (\n");
80-
createTableDDL.append(String.join(",\n", columnDefinitions));
81-
createTableDDL.append("\n);\n");
82-
// Output index information
83-
for (Map.Entry<String, List<String>> entry : indexMap.entrySet()) {
84-
String indexName = entry.getKey();
85-
List<String> columnList = entry.getValue();
86-
String indexColumns = String.join(", ", columnList);
87-
String createIndexDDL = String.format("CREATE INDEX %s ON %s (%s);", indexName, tableName,
88-
indexColumns);
89-
createTableDDL.append(createIndexDDL);
90-
}
91-
return createTableDDL.toString();
92-
93-
} catch (Exception e) {
94-
log.error("Failed to get table DDL", e);
102+
} catch (SQLException e) {
103+
throw new RuntimeException(e);
95104
}
96-
return "";
105+
return indexMap;
97106
}
98107

99108
private static String ROUTINES_SQL

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,22 @@ public String buildCreateTableSql(Table table) {
2121
script.append("`").append(table.getName()).append("`").append(" (").append("\n");
2222

2323
// append column
24-
for (TableColumn column : table.getColumnList()) {
24+
appendColumns(script, table.getColumnList());
25+
26+
// append primary key and index
27+
appendIndexes(script, table.getIndexList());
28+
script = new StringBuilder(script.substring(0, script.length() - 2));
29+
script.append("\n)");
30+
31+
// append engine, charset, collate, auto_increment, comment, and partition
32+
appendTableOptions(script, table);
33+
script.append(";");
34+
35+
return script.toString();
36+
}
37+
38+
private void appendColumns(StringBuilder script, List<TableColumn> columns) {
39+
for (TableColumn column : columns) {
2540
if (StringUtils.isBlank(column.getName()) || StringUtils.isBlank(column.getColumnType())) {
2641
continue;
2742
}
@@ -31,23 +46,20 @@ public String buildCreateTableSql(Table table) {
3146
}
3247
script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n");
3348
}
49+
}
3450

35-
// append primary key and index
36-
for (TableIndex tableIndex : table.getIndexList()) {
37-
if (StringUtils.isBlank(tableIndex.getName()) || StringUtils.isBlank(tableIndex.getType())) {
51+
private void appendIndexes(StringBuilder script, List<TableIndex> indexes) {
52+
for (TableIndex index : indexes) {
53+
if (StringUtils.isBlank(index.getName()) || StringUtils.isBlank(index.getType())) {
3854
continue;
3955
}
40-
HiveIndexTypeEnum hiveIndexTypeEnum = HiveIndexTypeEnum.getByType(tableIndex.getType());
41-
if(hiveIndexTypeEnum == null){
42-
continue;
43-
}
44-
script.append("\t").append("").append(hiveIndexTypeEnum.buildIndexScript(tableIndex)).append(",\n");
45-
}
46-
47-
script = new StringBuilder(script.substring(0, script.length() - 2));
48-
script.append("\n)");
56+
HiveIndexTypeEnum hiveIndexTypeEnum = HiveIndexTypeEnum.getByType(index.getType());
57+
script.append("\t").append("").append(hiveIndexTypeEnum.buildIndexScript(index)).append(",\n");
4958

59+
}
60+
}
5061

62+
private void appendTableOptions(StringBuilder script, Table table) {
5163
if (StringUtils.isNotBlank(table.getEngine())) {
5264
script.append(" ENGINE=").append(table.getEngine());
5365
}
@@ -71,9 +83,6 @@ public String buildCreateTableSql(Table table) {
7183
if (StringUtils.isNotBlank(table.getPartition())) {
7284
script.append(" \n").append(table.getPartition());
7385
}
74-
script.append(";");
75-
76-
return script.toString();
7786
}
7887

7988
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package ai.chat2db.server.domain.api.param;
2+
3+
4+
import jakarta.validation.constraints.NotNull;
5+
import lombok.Data;
6+
7+
import java.util.List;
8+
9+
10+
@Data
11+
public class GroupByParam {
12+
13+
/**
14+
* console id
15+
*/
16+
@NotNull
17+
private Long consoleId;
18+
19+
/**
20+
* Data source id
21+
*/
22+
@NotNull
23+
private Long dataSourceId;
24+
25+
/**
26+
* databaseName
27+
*/
28+
private String databaseName;
29+
30+
31+
/**
32+
* schema name
33+
*/
34+
private String schemaName;
35+
36+
37+
/**
38+
* origin sql
39+
*/
40+
private String originSql;
41+
42+
43+
/**
44+
* sort field
45+
*/
46+
private List<String> groupByList;
47+
}

chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/DlTemplateService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import ai.chat2db.server.domain.api.param.UpdateSelectResultParam;
77
import ai.chat2db.spi.model.ExecuteResult;
88
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
9+
import ai.chat2db.server.domain.api.param.GroupByParam;
910

1011
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
1112

@@ -58,6 +59,13 @@ public interface DlTemplateService {
5859
*/
5960
DataResult<String> updateSelectResult(UpdateSelectResultParam param);
6061

62+
/**
63+
*
64+
* @param param
65+
* @return
66+
*/
67+
DataResult<String> getGroupBySql(GroupByParam param);
68+
6169
/**
6270
*
6371
* @param param

chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/DatabaseServiceImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ public DataResult<MetaSchema> queryDatabaseSchema(MetaDataQueryParam param) {
114114
ThreadUtil.execute(() -> {
115115
try {
116116
database.setSchemas(metaData.schemas(connection, database.getName()));
117-
countDownLatch.countDown();
118117
} catch (Exception e) {
119118
log.error("queryDatabaseSchema error", e);
119+
} finally{
120+
countDownLatch.countDown();
120121
}
121122
});
122123
}

0 commit comments

Comments
 (0)