hive完善表、字段comment创建修改的问题。

待完善获取表结构功能。需要做特殊解析处理。
This commit is contained in:
tmlx1990
2024-03-13 18:23:07 +08:00
parent 2b792be03f
commit e29975b933
3 changed files with 39 additions and 9 deletions

View File

@ -9,6 +9,7 @@ import ai.chat2db.spi.SqlBuilder;
import ai.chat2db.spi.jdbc.DefaultMetaService;
import ai.chat2db.spi.model.Database;
import ai.chat2db.spi.model.Schema;
import ai.chat2db.spi.model.Table;
import ai.chat2db.spi.model.TableMeta;
import ai.chat2db.spi.sql.SQLExecutor;
import jakarta.validation.constraints.NotEmpty;
@ -95,6 +96,29 @@ public class HiveMetaData extends DefaultMetaService implements MetaData {
return new HiveSqlBuilder();
}
private static String SELECT_TABLE_SQL = "DESCRIBE EXTENDED %s";
// TODO 待完善
@Override
public List<Table> tables(Connection connection, String databaseName, String schemaName, String tableName) {
String sql = String.format(SELECT_TABLE_SQL, schemaName);
if (StringUtils.isNotBlank(tableName)) {
sql = sql + " and A.TABLE_NAME = '" + tableName + "'";
}
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
List<Table> tables = new ArrayList<>();
while (resultSet.next()) {
Table table = new Table();
table.setDatabaseName(databaseName);
table.setSchemaName(schemaName);
table.setName(resultSet.getString("TABLE_NAME"));
table.setComment(resultSet.getString("COMMENTS"));
tables.add(table);
}
return tables;
});
}
public static String format(String name) {
return "`" + name + "`";
}

View File

@ -65,7 +65,7 @@ public class HiveSqlBuilder extends DefaultSqlBuilder implements SqlBuilder {
}
if (StringUtils.isNotBlank(table.getComment())) {
script.append(" COMMENT='").append(table.getComment()).append("'");
script.append(" COMMENT '").append(table.getComment()).append("'");
}
if (StringUtils.isNotBlank(table.getPartition())) {
@ -79,19 +79,25 @@ public class HiveSqlBuilder extends DefaultSqlBuilder implements SqlBuilder {
@Override
public String buildModifyTaleSql(Table oldTable, Table newTable) {
StringBuilder script = new StringBuilder();
boolean isModify = false;
script.append("ALTER TABLE ");
if (StringUtils.isNotBlank(oldTable.getDatabaseName())) {
script.append("`").append(oldTable.getDatabaseName()).append("`").append(".");
if (StringUtils.isNotBlank(newTable.getDatabaseName())) {
script.append("`").append(newTable.getDatabaseName()).append("`").append(".");
}
script.append("`").append(oldTable.getName()).append("`").append("\n");
if (!StringUtils.equalsIgnoreCase(oldTable.getName(), newTable.getName())) {
script.append("\t").append("RENAME TO ").append("`").append(newTable.getName()).append("`").append(",\n");
script.append("\t").append("RENAME TO ").append("`").append(newTable.getName()).append("`").append(";\n");
isModify = true;
}
if (!StringUtils.equalsIgnoreCase(oldTable.getComment(), newTable.getComment())) {
script.append("\t").append("COMMENT=").append("'").append(newTable.getComment()).append("'").append(",\n");
}
if (oldTable.getIncrementValue() != newTable.getIncrementValue()) {
script.append("\t").append("AUTO_INCREMENT=").append(newTable.getIncrementValue()).append(",\n");
if (isModify) {
script.append("ALTER TABLE ");
if (StringUtils.isNotBlank(newTable.getDatabaseName())) {
script.append("`").append(newTable.getDatabaseName()).append("`").append(".");
}
script.append("`").append(newTable.getName()).append("`").append("\n");
}
script.append("\t").append("SET TBLPROPERTIES ('comment' = ").append("'").append(newTable.getComment()).append("'),\n");
}
// append modify column

View File

@ -214,7 +214,7 @@ public enum HiveColumnTypeEnum implements ColumnBuilder {
return "";
}
if (column.getNullable()!=null && 1==column.getNullable()) {
return "NULL";
return "";
} else {
return "NOT NULL";
}