fix table sort error

This commit is contained in:
SwallowGG
2024-04-27 22:13:38 +08:00
parent 57667ccb65
commit 79bbfe66f1
3 changed files with 34 additions and 7 deletions

View File

@ -91,7 +91,7 @@ public class SqlServerMetaData extends DefaultMetaService implements MetaData {
});
}
private static String SELECT_TABLES_SQL = "SELECT t.name AS TableName, mm.value as comment FROM sys.tables t LEFT JOIN(SELECT * from sys.extended_properties ep where ep.minor_id = 0 AND ep.name = 'MS_Description') mm ON t.object_id = mm.major_id WHERE t.schema_id= SCHEMA_ID('%S') ORDER by t.name";
private static String SELECT_TABLES_SQL = "SELECT t.name AS TableName, mm.value as comment FROM sys.tables t LEFT JOIN(SELECT * from sys.extended_properties ep where ep.minor_id = 0 AND ep.name = 'MS_Description') mm ON t.object_id = mm.major_id WHERE t.schema_id= SCHEMA_ID('%S')";
@Override
public List<Table> tables(Connection connection, String databaseName, String schemaName, String tableName) {
@ -99,7 +99,10 @@ public class SqlServerMetaData extends DefaultMetaService implements MetaData {
String sql = String.format(SELECT_TABLES_SQL, schemaName);
if (StringUtils.isNotBlank(tableName)) {
sql += " AND t.name = '" + tableName + "'";
}else {
sql += " ORDER BY t.name";
}
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
while (resultSet.next()) {
Table table = new Table();
@ -153,7 +156,7 @@ public class SqlServerMetaData extends DefaultMetaService implements MetaData {
private static String OBJECT_SQL
= "SELECT name FROM sys.objects WHERE type = '%s' and SCHEMA_ID = SCHEMA_ID('%s');";
= "SELECT name FROM sys.objects WHERE type = '%s' and SCHEMA_ID = SCHEMA_ID('%s') order by name;";
@Override
public Function function(Connection connection, @NotEmpty String databaseName, String schemaName,
@ -232,7 +235,7 @@ public class SqlServerMetaData extends DefaultMetaService implements MetaData {
private static String TRIGGER_SQL_LIST
= "SELECT OBJECT_NAME(parent_obj) AS TableName, name AS triggerName, OBJECT_DEFINITION(id) AS "
+ "triggerDefinition, CASE WHEN status & 1 = 1 THEN 'Enabled' ELSE 'Disabled' END AS Status FROM sysobjects "
+ "WHERE xtype = 'TR' ";
+ "WHERE xtype = 'TR' order by name";
@Override
public List<Trigger> triggers(Connection connection, String databaseName, String schemaName) {

View File

@ -2,6 +2,7 @@ package ai.chat2db.spi.enums;
import ai.chat2db.server.tools.base.enums.BaseEnum;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
/**
* Driver class enumeration
@ -108,9 +109,9 @@ public enum DataTypeEnum implements BaseEnum<String> {
public String getSqlValue(String value) {
if (this == DataTypeEnum.BOOLEAN) {
if("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)){
if ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) {
return value;
}else {
} else {
return "'" + value + "'";
}
}
@ -118,7 +119,7 @@ public enum DataTypeEnum implements BaseEnum<String> {
return value;
}
if (this == DataTypeEnum.STRING) {
return "'" + value + "'";
return getStringValue(value);
}
if (this == DataTypeEnum.DATETIME) {
return "'" + value + "'";
@ -155,4 +156,14 @@ public enum DataTypeEnum implements BaseEnum<String> {
}
return "'" + value + "'";
}
public static String getStringValue(String value) {
if (StringUtils.isBlank(value)) {
return "'" + value + "'";
}
value = value.replace("\\", "\\\\");
value = value.replace("'", "\\'");
return "'" + value + "'";
}
}

View File

@ -124,7 +124,7 @@ public class SqlUtils {
// Iterate through each statement
for (Statement stmt : statements.getStatements()) {
if (!(stmt instanceof CreateProcedure)) {
list.add(stmt.toString());
list.add(updateNow(stmt.toString(),dbType));
}
}
if (CollectionUtils.isEmpty(list)) {
@ -136,6 +136,19 @@ public class SqlUtils {
return list;
}
private static String updateNow(String sql,DbType dbType) {
if(StringUtils.isBlank(sql) || !DbType.mysql.equals(dbType)){
return sql;
}
if(sql.contains("default now ()")){
return sql.replace("default now ()","default CURRENT_TIMESTAMP");
}
if(sql.contains("DEFAULT now ()")){
return sql.replace("DEFAULT now ()","DEFAULT CURRENT_TIMESTAMP");
}
return sql;
}
private static final String DEFAULT_VALUE = "CHAT2DB_UPDATE_TABLE_DATA_USER_FILLED_DEFAULT";
public static String getSqlValue(String value, String dataType) {