Supports fuzzy query of table names, not case sensitive.

Automatically add pagination parameters when users query SQL without pagination parameters.
Sort the database schema and prioritize the user-defined ones.
This commit is contained in:
SwallowGG
2023-10-22 22:46:26 +08:00
parent c0161df83b
commit 51a35f770e
28 changed files with 539 additions and 129 deletions

View File

@ -14,6 +14,7 @@ import ai.chat2db.spi.SqlBuilder;
import ai.chat2db.spi.jdbc.DefaultMetaService;
import ai.chat2db.spi.model.*;
import ai.chat2db.spi.sql.SQLExecutor;
import ai.chat2db.spi.util.SortUtils;
import com.google.common.collect.Lists;
import jakarta.validation.constraints.NotEmpty;
import org.apache.commons.lang3.StringUtils;
@ -22,6 +23,15 @@ public class OracleMetaData extends DefaultMetaService implements MetaData {
private static final String TABLE_DDL_SQL = "select dbms_metadata.get_ddl('TABLE','%s','%s') as sql from dual";
private List<String> systemSchemas = Arrays.asList("ANONYMOUS","APEX_030200","APEX_PUBLIC_USER","APPQOSSYS","BI","CTXSYS","DBSNMP","DIP","EXFSYS","FLOWS_FILES","HR","IX","MDDATA","MDSYS","MGMT_VIEW","OE","OLAPSYS","ORACLE_OCM","ORDDATA","ORDPLUGINS","ORDSYS","OUTLN","OWBSYS","OWBSYS_AUDIT","PM","SCOTT","SH","SI_INFORMTN_SCHEMA","SPATIAL_CSW_ADMIN_USR","SPATIAL_WFS_ADMIN_USR","SYS","SYSMAN","SYSTEM","WMSYS","XDB","XS$NULL");
@Override
public List<Schema> schemas(Connection connection, String databaseName) {
List<Schema> schemas = SQLExecutor.getInstance().schemas(connection, databaseName, null);
return SortUtils.sortSchema(schemas, systemSchemas);
}
@Override
public String tableDDL(Connection connection, String databaseName, String schemaName, String tableName) {
String sql = String.format(TABLE_DDL_SQL, tableName, schemaName);

View File

@ -6,6 +6,7 @@ import ai.chat2db.spi.SqlBuilder;
import ai.chat2db.spi.model.Table;
import ai.chat2db.spi.model.TableColumn;
import ai.chat2db.spi.model.TableIndex;
import ai.chat2db.spi.sql.Chat2DBContext;
import org.apache.commons.lang3.StringUtils;
public class OracleSqlBuilder implements SqlBuilder {
@ -16,7 +17,7 @@ public class OracleSqlBuilder implements SqlBuilder {
script.append("CREATE TABLE ").append("\"").append(table.getSchemaName()).append("\".\"").append(table.getName()).append("\" (").append("\n");
for (TableColumn column : table.getColumnList()) {
if(StringUtils.isBlank(column.getName())|| StringUtils.isBlank(column.getColumnType())){
if (StringUtils.isBlank(column.getName()) || StringUtils.isBlank(column.getColumnType())) {
continue;
}
OracleColumnTypeEnum typeEnum = OracleColumnTypeEnum.getByType(column.getColumnType());
@ -27,7 +28,7 @@ public class OracleSqlBuilder implements SqlBuilder {
script.append("\n);");
for (TableIndex tableIndex : table.getIndexList()) {
if(StringUtils.isBlank(tableIndex.getName())|| StringUtils.isBlank(tableIndex.getType())){
if (StringUtils.isBlank(tableIndex.getName()) || StringUtils.isBlank(tableIndex.getType())) {
continue;
}
OracleIndexTypeEnum oracleColumnTypeEnum = OracleIndexTypeEnum.getByType(tableIndex.getType());
@ -35,7 +36,7 @@ public class OracleSqlBuilder implements SqlBuilder {
}
for (TableColumn column : table.getColumnList()) {
if(StringUtils.isBlank(column.getName())|| StringUtils.isBlank(column.getColumnType()) || StringUtils.isBlank(column.getComment())){
if (StringUtils.isBlank(column.getName()) || StringUtils.isBlank(column.getColumnType()) || StringUtils.isBlank(column.getComment())) {
continue;
}
script.append("\n").append(buildComment(column)).append(";");
@ -66,7 +67,7 @@ public class OracleSqlBuilder implements SqlBuilder {
StringBuilder script = new StringBuilder();
if (!StringUtils.equalsIgnoreCase(oldTable.getName(), newTable.getName())) {
script.append("ALTER TABLE "). append("\"").append(oldTable.getSchemaName()).append("\".\"").append(oldTable.getName()).append("\"");
script.append("ALTER TABLE ").append("\"").append(oldTable.getSchemaName()).append("\".\"").append(oldTable.getName()).append("\"");
script.append(" ").append("RENAME TO ").append("\"").append(newTable.getName()).append("\"").append(";\n");
}
if (!StringUtils.equalsIgnoreCase(oldTable.getComment(), newTable.getComment())) {
@ -79,7 +80,7 @@ public class OracleSqlBuilder implements SqlBuilder {
if (StringUtils.isNotBlank(tableColumn.getEditStatus())) {
OracleColumnTypeEnum typeEnum = OracleColumnTypeEnum.getByType(tableColumn.getColumnType());
script.append("\t").append(typeEnum.buildModifyColumn(tableColumn)).append(";\n");
if(StringUtils.isNotBlank(tableColumn.getComment())){
if (StringUtils.isNotBlank(tableColumn.getComment())) {
script.append("\n").append(buildComment(tableColumn)).append(";\n");
}
}
@ -92,11 +93,38 @@ public class OracleSqlBuilder implements SqlBuilder {
script.append("\t").append(mysqlIndexTypeEnum.buildModifyIndex(tableIndex)).append(";\n");
}
}
if(script.length()>2) {
if (script.length() > 2) {
script = new StringBuilder(script.substring(0, script.length() - 2));
script.append(";");
}
return script.toString();
}
@Override
public String pageLimit(String sql, int offset, int pageNo, int pageSize) {
int startRow = offset;
int endRow = offset + pageSize;
StringBuilder sqlBuilder = new StringBuilder(sql.length() + 120);
if (startRow > 0) {
sqlBuilder.append("SELECT * FROM ( ");
}
if (endRow > 0) {
sqlBuilder.append(" SELECT TMP_PAGE.*, ROWNUM CAHT2DB_AUTO_ROW_ID FROM ( ");
}
sqlBuilder.append("\n");
sqlBuilder.append(sql);
sqlBuilder.append("\n");
if (endRow > 0) {
sqlBuilder.append(" ) TMP_PAGE WHERE ROWNUM <= ");
sqlBuilder.append(endRow);
}
if (startRow > 0) {
sqlBuilder.append(" ) WHERE CAHT2DB_AUTO_ROW_ID > ");
sqlBuilder.append(startRow);
}
return sqlBuilder.toString();
}
}