changes for issue 1362

This commit is contained in:
arjun12102019
2024-05-06 22:50:20 +05:30
parent 014a0e0455
commit c60c24b9d2
3 changed files with 144 additions and 82 deletions

View File

@ -14,60 +14,102 @@ import org.apache.commons.lang3.StringUtils;
public class ClickHouseSqlBuilder extends DefaultSqlBuilder {
@Override
public String buildCreateTableSql(Table table) {
StringBuilder script = new StringBuilder();
script.append("CREATE TABLE ");
if (StringUtils.isNotBlank(table.getDatabaseName())) {
script.append("`").append(table.getDatabaseName()).append("`").append(".");
}
// Initialize StringBuilder to build the SQL script
StringBuilder script = new StringBuilder("CREATE TABLE ");
// Append the database name, if present
appendDatabaseName(script, table.getDatabaseName());
// Append the table name
script.append("`").append(table.getName()).append("`").append(" (").append("\n");
// append column
for (TableColumn column : table.getColumnList()) {
if (StringUtils.isBlank(column.getName()) || StringUtils.isBlank(column.getColumnType())) {
continue;
}
ClickHouseColumnTypeEnum typeEnum = ClickHouseColumnTypeEnum.getByType(column.getColumnType());
script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n");
}
appendColumns(script, table.getColumnList());
// append index
for (TableIndex tableIndex : table.getIndexList()) {
if (StringUtils.isBlank(tableIndex.getName()) || StringUtils.isBlank(tableIndex.getType())) {
continue;
}
ClickHouseIndexTypeEnum mysqlIndexTypeEnum = ClickHouseIndexTypeEnum.getByType(tableIndex.getType());
if (!ClickHouseIndexTypeEnum.PRIMARY.equals(mysqlIndexTypeEnum) ) {
script.append("\t").append("").append(mysqlIndexTypeEnum.buildIndexScript(tableIndex)).append(",\n");
}
}
appendIndexes(script, table.getIndexList());
// Remove the last comma
script = new StringBuilder(script.substring(0, script.length() - 2));
script.append("\n)");
// Append the engine, if present
appendEngine(script, table.getEngine());
if (StringUtils.isNotBlank(table.getEngine())) {
script.append(" ENGINE=").append(table.getEngine()).append("\n");
}
// append primary key
for (TableIndex tableIndex : table.getIndexList()) {
if (StringUtils.isBlank(tableIndex.getName()) || StringUtils.isBlank(tableIndex.getType())) {
continue;
}
ClickHouseIndexTypeEnum mysqlIndexTypeEnum = ClickHouseIndexTypeEnum.getByType(tableIndex.getType());
if (ClickHouseIndexTypeEnum.PRIMARY.equals(mysqlIndexTypeEnum) ) {
script.append("\t").append("").append(mysqlIndexTypeEnum.buildIndexScript(tableIndex)).append("\n");
}
}
appendPrimaryKey(script, table.getIndexList());
if (StringUtils.isNotBlank(table.getComment())) {
script.append(" COMMENT '").append(table.getComment()).append("'");
}
// Append the comment, if present
appendComment(script, table.getComment());
// Append a semicolon to complete the SQL statement
script.append(";");
// Return the complete SQL script
return script.toString();
}
// Method to append the database name to the SQL script
private void appendDatabaseName(StringBuilder script, String databaseName) {
if (StringUtils.isNotBlank(databaseName)) {
script.append("`").append(databaseName).append("`.");
}
}
// Method to append columns to the SQL script
private void appendColumns(StringBuilder script, List<TableColumn> columns) {
for (TableColumn column : columns) {
// Check if column name and type are not blank
if (StringUtils.isNotBlank(column.getName()) && StringUtils.isNotBlank(column.getColumnType())) {
// Get the column type enum and append the column SQL to the script
ClickHouseColumnTypeEnum typeEnum = ClickHouseColumnTypeEnum.getByType(column.getColumnType());
script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n");
}
}
}
// Method to append indexes to the SQL script
private void appendIndexes(StringBuilder script, List<TableIndex> indexes) {
for (TableIndex index : indexes) {
// Check if index name and type are not blank
if (StringUtils.isNotBlank(index.getName()) && StringUtils.isNotBlank(index.getType())) {
// Get the index type enum and append the index script to the script
ClickHouseIndexTypeEnum indexTypeEnum = ClickHouseIndexTypeEnum.getByType(index.getType());
if (!ClickHouseIndexTypeEnum.PRIMARY.equals(indexTypeEnum)) {
script.append("\t").append(indexTypeEnum.buildIndexScript(index)).append(",\n");
}
}
}
}
// Method to append the engine to the SQL script
private void appendEngine(StringBuilder script, String engine) {
if (StringUtils.isNotBlank(engine)) {
script.append(" ENGINE=").append(engine).append("\n");
}
}
// Method to append the primary key to the SQL script
private void appendPrimaryKey(StringBuilder script, List<TableIndex> indexes) {
for (TableIndex index : indexes) {
// Check if index name and type are not blank
if (StringUtils.isNotBlank(index.getName()) && StringUtils.isNotBlank(index.getType())) {
// Get the index type enum and append the index script to the script
ClickHouseIndexTypeEnum indexTypeEnum = ClickHouseIndexTypeEnum.getByType(index.getType());
if (ClickHouseIndexTypeEnum.PRIMARY.equals(indexTypeEnum)) {
script.append("\t").append(indexTypeEnum.buildIndexScript(index)).append("\n");
}
}
}
}
// Method to append the comment to the SQL script
private void appendComment(StringBuilder script, String comment) {
if (StringUtils.isNotBlank(comment)) {
script.append(" COMMENT '").append(comment).append("'");
}
}
@Override
public String buildModifyTaleSql(Table oldTable, Table newTable) {
StringBuilder script = new StringBuilder();

View File

@ -33,10 +33,33 @@ public class H2Meta extends DefaultMetaService implements MetaData {
}
private String getDDL(Connection connection, String databaseName, String schemaName, String tableName) {
try {
// Query table structure information
ResultSet columns = connection.getMetaData().getColumns(databaseName, schemaName, tableName, null);
try{
List<String> columnDefinitions = getColumnDefinitions(connection, databaseName, schemaName, tableName);
Map<String, List<String>> indexMap = getIndexInfo(connection, databaseName, schemaName, tableName);
StringBuilder createTableDDL = new StringBuilder("CREATE TABLE ");
createTableDDL.append(tableName).append(" (\n");
createTableDDL.append(String.join(",\n", columnDefinitions));
createTableDDL.append("\n);\n");
// Output index information
for (Map.Entry<String, List<String>> entry : indexMap.entrySet()) {
String indexName = entry.getKey();
List<String> columnList = entry.getValue();
String indexColumns = String.join(", ", columnList);
String createIndexDDL = String.format("CREATE INDEX %s ON %s (%s);", indexName, tableName, indexColumns);
createTableDDL.append(createIndexDDL);
}
return createTableDDL.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
private List<String> getColumnDefinitions(Connection connection, String databaseName, String schemaName, String tableName) {
List<String> columnDefinitions = new ArrayList<>();
try (ResultSet columns = connection.getMetaData().getColumns(databaseName, schemaName, tableName, null)) {
while (columns.next()) {
String columnName = columns.getString("COLUMN_NAME");
String columnType = columns.getString("TYPE_NAME");
@ -58,40 +81,26 @@ public class H2Meta extends DefaultMetaService implements MetaData {
}
columnDefinitions.add(columnDefinition.toString());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return columnDefinitions;
}
// Query table index information
ResultSet indexes = connection.getMetaData().getIndexInfo(databaseName, schemaName, tableName, false,
false);
private Map<String, List<String>> getIndexInfo(Connection connection, String databaseName, String schemaName, String tableName) {
Map<String, List<String>> indexMap = new HashMap<>();
try (ResultSet indexes = connection.getMetaData().getIndexInfo(databaseName, schemaName, tableName, false, false)) {
while (indexes.next()) {
String indexName = indexes.getString("INDEX_NAME");
String columnName = indexes.getString("COLUMN_NAME");
if (indexName != null) {
if (!indexMap.containsKey(indexName)) {
indexMap.put(indexName, new ArrayList<>());
}
indexMap.get(indexName).add(columnName);
indexMap.computeIfAbsent(indexName, k -> new ArrayList<>()).add(columnName);
}
}
StringBuilder createTableDDL = new StringBuilder("CREATE TABLE ");
createTableDDL.append(tableName).append(" (\n");
createTableDDL.append(String.join(",\n", columnDefinitions));
createTableDDL.append("\n);\n");
// Output index information
for (Map.Entry<String, List<String>> entry : indexMap.entrySet()) {
String indexName = entry.getKey();
List<String> columnList = entry.getValue();
String indexColumns = String.join(", ", columnList);
String createIndexDDL = String.format("CREATE INDEX %s ON %s (%s);", indexName, tableName,
indexColumns);
createTableDDL.append(createIndexDDL);
} catch (SQLException e) {
throw new RuntimeException(e);
}
return createTableDDL.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
return indexMap;
}
private static String ROUTINES_SQL

View File

@ -27,27 +27,41 @@ public class HiveSqlBuilder extends DefaultSqlBuilder implements SqlBuilder {
script.append("`").append(table.getName()).append("`").append(" (").append("\n");
// append column
for (TableColumn column : table.getColumnList()) {
appendColumns(script, table.getColumnList());
// append primary key and index
appendIndexes(script, table.getIndexList());
script = new StringBuilder(script.substring(0, script.length() - 2));
script.append("\n)");
// append engine, charset, collate, auto_increment, comment, and partition
appendTableOptions(script, table);
script.append(";");
return script.toString();
}
private void appendColumns(StringBuilder script, List<TableColumn> columns) {
for (TableColumn column : columns) {
if (StringUtils.isBlank(column.getName()) || StringUtils.isBlank(column.getColumnType())) {
continue;
}
HiveColumnTypeEnum typeEnum = HiveColumnTypeEnum.getByType(column.getColumnType());
script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n");
}
}
// append primary key and index
for (TableIndex tableIndex : table.getIndexList()) {
if (StringUtils.isBlank(tableIndex.getName()) || StringUtils.isBlank(tableIndex.getType())) {
private void appendIndexes(StringBuilder script, List<TableIndex> indexes) {
for (TableIndex index : indexes) {
if (StringUtils.isBlank(index.getName()) || StringUtils.isBlank(index.getType())) {
continue;
}
HiveIndexTypeEnum hiveIndexTypeEnum = HiveIndexTypeEnum.getByType(tableIndex.getType());
script.append("\t").append("").append(hiveIndexTypeEnum.buildIndexScript(tableIndex)).append(",\n");
HiveIndexTypeEnum hiveIndexTypeEnum = HiveIndexTypeEnum.getByType(index.getType());
script.append("\t").append("").append(hiveIndexTypeEnum.buildIndexScript(index)).append(",\n");
}
}
script = new StringBuilder(script.substring(0, script.length() - 2));
script.append("\n)");
private void appendTableOptions(StringBuilder script, Table table) {
if (StringUtils.isNotBlank(table.getEngine())) {
script.append(" ENGINE=").append(table.getEngine());
}
@ -71,9 +85,6 @@ public class HiveSqlBuilder extends DefaultSqlBuilder implements SqlBuilder {
if (StringUtils.isNotBlank(table.getPartition())) {
script.append(" \n").append(table.getPartition());
}
script.append(";");
return script.toString();
}
@Override