|
14 | 14 | public class ClickHouseSqlBuilder extends DefaultSqlBuilder { |
15 | 15 | @Override |
16 | 16 | 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 |
22 | 24 | script.append("`").append(table.getName()).append("`").append(" (").append("\n"); |
23 | 25 |
|
24 | 26 | // 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()); |
32 | 28 |
|
33 | 29 | // 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()); |
43 | 31 |
|
| 32 | + // Remove the last comma |
44 | 33 | script = new StringBuilder(script.substring(0, script.length() - 2)); |
45 | 34 | script.append("\n)"); |
46 | 35 |
|
| 36 | + // Append the engine, if present |
| 37 | + appendEngine(script, table.getEngine()); |
47 | 38 |
|
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("`."); |
50 | 56 | } |
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"); |
55 | 67 | } |
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 | + } |
59 | 81 | } |
60 | 82 | } |
| 83 | + } |
61 | 84 |
|
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"); |
64 | 89 | } |
| 90 | + } |
65 | 91 |
|
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 | + } |
67 | 105 |
|
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 | + } |
69 | 111 | } |
70 | 112 |
|
71 | 113 | @Override |
|
0 commit comments