From 0fe586d263068c9477ceb9747f5d14e8238a7e2c Mon Sep 17 00:00:00 2001 From: SwallowGG <1558143046@qq.com> Date: Fri, 15 Mar 2024 14:07:51 +0800 Subject: [PATCH] feat:support Serializable --- .../ConnectionEdit/config/dataSource.ts | 4 + .../builder/ClickHouseSqlBuilder.java | 2 +- .../plugin/h2/builder/H2SqlBuilder.java | 2 +- .../kingbase/builder/KingBaseSqlBuilder.java | 2 +- .../mongodb/MongodbCommandExecutor.java | 17 +++ .../plugin/mongodb/MongodbMetaData.java | 7 +- .../plugin/mysql/builder/MysqlSqlBuilder.java | 2 +- .../oracle/builder/OracleSqlBuilder.java | 3 +- .../builder/PostgreSQLSqlBuilder.java | 3 +- .../chat2db-plugins/chat2db-redis/pom.xml | 34 ------ .../chat2db/plugin/redis/RedisDBManage.java | 8 -- .../chat2db/plugin/redis/RedisMetaData.java | 57 ---------- .../ai/chat2db/plugin/redis/RedisPlugin.java | 27 ----- .../java/ai/chat2db/plugin/redis/redis.json | 18 --- .../META-INF/services/ai.chat2db.spi.Plugin | 1 - .../plugin/sqlite/builder/SqliteBuilder.java | 2 +- .../builder/SqlServerSqlBuilder.java | 2 +- chat2db-server/chat2db-plugins/pom.xml | 1 - .../domain/api/param/DlExecuteParam.java | 3 + .../domain/api/service/DlTemplateService.java | 8 ++ .../chat2db-server-domain-core/pom.xml | 5 - .../core/impl/DlTemplateServiceImpl.java | 25 +++-- .../api/controller/rdb/RdbDmlController.java | 13 +-- chat2db-server/chat2db-spi/pom.xml | 17 +++ .../java/ai/chat2db/spi/CommandExecutor.java | 6 + .../main/java/ai/chat2db/spi/MetaData.java | 15 +++ .../main/java/ai/chat2db/spi/SqlBuilder.java | 10 +- .../chat2db/spi/jdbc/DefaultMetaService.java | 12 +- .../chat2db/spi/jdbc/DefaultSqlBuilder.java | 8 +- .../java/ai/chat2db/spi/model/Command.java | 5 + .../ai/chat2db/spi/model/ExecuteResult.java | 8 +- .../ai/chat2db/spi/model/QueryResult.java | 16 +++ .../java/ai/chat2db/spi/sql/SQLExecutor.java | 103 ++++++++++-------- 33 files changed, 207 insertions(+), 239 deletions(-) create mode 100644 chat2db-server/chat2db-plugins/chat2db-mongodb/src/main/java/ai/chat2db/plugin/mongodb/MongodbCommandExecutor.java delete mode 100644 chat2db-server/chat2db-plugins/chat2db-redis/pom.xml delete mode 100644 chat2db-server/chat2db-plugins/chat2db-redis/src/main/java/ai/chat2db/plugin/redis/RedisDBManage.java delete mode 100644 chat2db-server/chat2db-plugins/chat2db-redis/src/main/java/ai/chat2db/plugin/redis/RedisMetaData.java delete mode 100644 chat2db-server/chat2db-plugins/chat2db-redis/src/main/java/ai/chat2db/plugin/redis/RedisPlugin.java delete mode 100644 chat2db-server/chat2db-plugins/chat2db-redis/src/main/java/ai/chat2db/plugin/redis/redis.json delete mode 100644 chat2db-server/chat2db-plugins/chat2db-redis/src/main/resources/META-INF/services/ai.chat2db.spi.Plugin create mode 100644 chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/QueryResult.java diff --git a/chat2db-client/src/components/ConnectionEdit/config/dataSource.ts b/chat2db-client/src/components/ConnectionEdit/config/dataSource.ts index a866d2da6..f14c86510 100644 --- a/chat2db-client/src/components/ConnectionEdit/config/dataSource.ts +++ b/chat2db-client/src/components/ConnectionEdit/config/dataSource.ts @@ -250,6 +250,10 @@ export const dataSourceFormConfigs: IConnectionConfig[] = [ { "key": "zeroDateTimeBehavior", "value": "convertToNull" + }, + { + "key": "useInformationSchema", + "value": "true" } ], type: DatabaseTypeCode.MYSQL, diff --git a/chat2db-server/chat2db-plugins/chat2db-clickhouse/src/main/java/ai/chat2db/plugin/clickhouse/builder/ClickHouseSqlBuilder.java b/chat2db-server/chat2db-plugins/chat2db-clickhouse/src/main/java/ai/chat2db/plugin/clickhouse/builder/ClickHouseSqlBuilder.java index 41140ef58..f093c198a 100644 --- a/chat2db-server/chat2db-plugins/chat2db-clickhouse/src/main/java/ai/chat2db/plugin/clickhouse/builder/ClickHouseSqlBuilder.java +++ b/chat2db-server/chat2db-plugins/chat2db-clickhouse/src/main/java/ai/chat2db/plugin/clickhouse/builder/ClickHouseSqlBuilder.java @@ -11,7 +11,7 @@ import org.apache.commons.lang3.StringUtils; -public class ClickHouseSqlBuilder extends DefaultSqlBuilder implements SqlBuilder { +public class ClickHouseSqlBuilder extends DefaultSqlBuilder { @Override public String buildCreateTableSql(Table table) { StringBuilder script = new StringBuilder(); diff --git a/chat2db-server/chat2db-plugins/chat2db-h2/src/main/java/ai/chat2db/plugin/h2/builder/H2SqlBuilder.java b/chat2db-server/chat2db-plugins/chat2db-h2/src/main/java/ai/chat2db/plugin/h2/builder/H2SqlBuilder.java index ed41f653d..325456ae2 100644 --- a/chat2db-server/chat2db-plugins/chat2db-h2/src/main/java/ai/chat2db/plugin/h2/builder/H2SqlBuilder.java +++ b/chat2db-server/chat2db-plugins/chat2db-h2/src/main/java/ai/chat2db/plugin/h2/builder/H2SqlBuilder.java @@ -5,7 +5,7 @@ import ai.chat2db.spi.model.Schema; import org.apache.commons.lang3.StringUtils; -public class H2SqlBuilder extends DefaultSqlBuilder implements SqlBuilder { +public class H2SqlBuilder extends DefaultSqlBuilder { @Override public String buildCreateSchemaSql(Schema schema) { diff --git a/chat2db-server/chat2db-plugins/chat2db-kingbase/src/main/java/ai/chat2db/plugin/kingbase/builder/KingBaseSqlBuilder.java b/chat2db-server/chat2db-plugins/chat2db-kingbase/src/main/java/ai/chat2db/plugin/kingbase/builder/KingBaseSqlBuilder.java index bccae8957..9dcbf920d 100644 --- a/chat2db-server/chat2db-plugins/chat2db-kingbase/src/main/java/ai/chat2db/plugin/kingbase/builder/KingBaseSqlBuilder.java +++ b/chat2db-server/chat2db-plugins/chat2db-kingbase/src/main/java/ai/chat2db/plugin/kingbase/builder/KingBaseSqlBuilder.java @@ -14,7 +14,7 @@ import java.util.stream.Collectors; -public class KingBaseSqlBuilder extends DefaultSqlBuilder implements SqlBuilder { +public class KingBaseSqlBuilder extends DefaultSqlBuilder { @Override public String buildCreateTableSql(Table table) { StringBuilder script = new StringBuilder(); diff --git a/chat2db-server/chat2db-plugins/chat2db-mongodb/src/main/java/ai/chat2db/plugin/mongodb/MongodbCommandExecutor.java b/chat2db-server/chat2db-plugins/chat2db-mongodb/src/main/java/ai/chat2db/plugin/mongodb/MongodbCommandExecutor.java new file mode 100644 index 000000000..222518b37 --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-mongodb/src/main/java/ai/chat2db/plugin/mongodb/MongodbCommandExecutor.java @@ -0,0 +1,17 @@ +package ai.chat2db.plugin.mongodb; + +import ai.chat2db.spi.model.Command; +import ai.chat2db.spi.model.ExecuteResult; +import ai.chat2db.spi.sql.SQLExecutor; + +import java.util.List; + +public class MongodbCommandExecutor extends SQLExecutor { + + @Override + public List executeSelectTable(Command command) { + String sql = "db." + command.getTableName() + ".find()"; + command.setScript(sql); + return execute(command); + } +} diff --git a/chat2db-server/chat2db-plugins/chat2db-mongodb/src/main/java/ai/chat2db/plugin/mongodb/MongodbMetaData.java b/chat2db-server/chat2db-plugins/chat2db-mongodb/src/main/java/ai/chat2db/plugin/mongodb/MongodbMetaData.java index 4716d88e7..01047a760 100644 --- a/chat2db-server/chat2db-plugins/chat2db-mongodb/src/main/java/ai/chat2db/plugin/mongodb/MongodbMetaData.java +++ b/chat2db-server/chat2db-plugins/chat2db-mongodb/src/main/java/ai/chat2db/plugin/mongodb/MongodbMetaData.java @@ -1,9 +1,9 @@ package ai.chat2db.plugin.mongodb; +import ai.chat2db.spi.CommandExecutor; import ai.chat2db.spi.MetaData; import ai.chat2db.spi.jdbc.DefaultMetaService; import ai.chat2db.spi.model.Database; -import ai.chat2db.spi.sql.SQLExecutor; import com.google.common.collect.Lists; import java.sql.Connection; @@ -17,4 +17,9 @@ public class MongodbMetaData extends DefaultMetaService implements MetaData { public List databases(Connection connection) { return Lists.newArrayList(); } + + @Override + public CommandExecutor getCommandExecutor() { + return new MongodbCommandExecutor(); + } } diff --git a/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/builder/MysqlSqlBuilder.java b/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/builder/MysqlSqlBuilder.java index 2d816769f..1b84c41bc 100644 --- a/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/builder/MysqlSqlBuilder.java +++ b/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/builder/MysqlSqlBuilder.java @@ -14,7 +14,7 @@ import java.util.*; -public class MysqlSqlBuilder extends DefaultSqlBuilder implements SqlBuilder { +public class MysqlSqlBuilder extends DefaultSqlBuilder { @Override public String buildCreateTableSql(Table table) { StringBuilder script = new StringBuilder(); diff --git a/chat2db-server/chat2db-plugins/chat2db-oracle/src/main/java/ai/chat2db/plugin/oracle/builder/OracleSqlBuilder.java b/chat2db-server/chat2db-plugins/chat2db-oracle/src/main/java/ai/chat2db/plugin/oracle/builder/OracleSqlBuilder.java index 468ac2027..57d9b4a3f 100644 --- a/chat2db-server/chat2db-plugins/chat2db-oracle/src/main/java/ai/chat2db/plugin/oracle/builder/OracleSqlBuilder.java +++ b/chat2db-server/chat2db-plugins/chat2db-oracle/src/main/java/ai/chat2db/plugin/oracle/builder/OracleSqlBuilder.java @@ -2,14 +2,13 @@ import ai.chat2db.plugin.oracle.type.OracleColumnTypeEnum; import ai.chat2db.plugin.oracle.type.OracleIndexTypeEnum; -import ai.chat2db.spi.SqlBuilder; import ai.chat2db.spi.jdbc.DefaultSqlBuilder; import ai.chat2db.spi.model.Table; import ai.chat2db.spi.model.TableColumn; import ai.chat2db.spi.model.TableIndex; import org.apache.commons.lang3.StringUtils; -public class OracleSqlBuilder extends DefaultSqlBuilder implements SqlBuilder { +public class OracleSqlBuilder extends DefaultSqlBuilder { @Override public String buildCreateTableSql(Table table) { StringBuilder script = new StringBuilder(); diff --git a/chat2db-server/chat2db-plugins/chat2db-postgresql/src/main/java/ai/chat2db/plugin/postgresql/builder/PostgreSQLSqlBuilder.java b/chat2db-server/chat2db-plugins/chat2db-postgresql/src/main/java/ai/chat2db/plugin/postgresql/builder/PostgreSQLSqlBuilder.java index e0d90a517..34a856cb7 100644 --- a/chat2db-server/chat2db-plugins/chat2db-postgresql/src/main/java/ai/chat2db/plugin/postgresql/builder/PostgreSQLSqlBuilder.java +++ b/chat2db-server/chat2db-plugins/chat2db-postgresql/src/main/java/ai/chat2db/plugin/postgresql/builder/PostgreSQLSqlBuilder.java @@ -2,7 +2,6 @@ import ai.chat2db.plugin.postgresql.type.PostgreSQLColumnTypeEnum; import ai.chat2db.plugin.postgresql.type.PostgreSQLIndexTypeEnum; -import ai.chat2db.spi.SqlBuilder; import ai.chat2db.spi.jdbc.DefaultSqlBuilder; import ai.chat2db.spi.model.*; import org.apache.commons.collections4.CollectionUtils; @@ -14,7 +13,7 @@ import java.util.stream.Collectors; -public class PostgreSQLSqlBuilder extends DefaultSqlBuilder implements SqlBuilder { +public class PostgreSQLSqlBuilder extends DefaultSqlBuilder { @Override public String buildCreateTableSql(Table table) { StringBuilder script = new StringBuilder(); diff --git a/chat2db-server/chat2db-plugins/chat2db-redis/pom.xml b/chat2db-server/chat2db-plugins/chat2db-redis/pom.xml deleted file mode 100644 index ddd3c4f29..000000000 --- a/chat2db-server/chat2db-plugins/chat2db-redis/pom.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - 4.0.0 - - ai.chat2db - chat2db-plugins - ${revision} - ../pom.xml - - - - ai.chat2db - chat2db-spi - - - - chat2db-redis - - - - src/main/java - - - **/*.json - - - - src/main/resources - - - - \ No newline at end of file diff --git a/chat2db-server/chat2db-plugins/chat2db-redis/src/main/java/ai/chat2db/plugin/redis/RedisDBManage.java b/chat2db-server/chat2db-plugins/chat2db-redis/src/main/java/ai/chat2db/plugin/redis/RedisDBManage.java deleted file mode 100644 index b9761c5d4..000000000 --- a/chat2db-server/chat2db-plugins/chat2db-redis/src/main/java/ai/chat2db/plugin/redis/RedisDBManage.java +++ /dev/null @@ -1,8 +0,0 @@ -package ai.chat2db.plugin.redis; - -import ai.chat2db.spi.DBManage; -import ai.chat2db.spi.jdbc.DefaultDBManage; - -public class RedisDBManage extends DefaultDBManage implements DBManage { - -} diff --git a/chat2db-server/chat2db-plugins/chat2db-redis/src/main/java/ai/chat2db/plugin/redis/RedisMetaData.java b/chat2db-server/chat2db-plugins/chat2db-redis/src/main/java/ai/chat2db/plugin/redis/RedisMetaData.java deleted file mode 100644 index ac85570d2..000000000 --- a/chat2db-server/chat2db-plugins/chat2db-redis/src/main/java/ai/chat2db/plugin/redis/RedisMetaData.java +++ /dev/null @@ -1,57 +0,0 @@ -package ai.chat2db.plugin.redis; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; - -import ai.chat2db.spi.MetaData; -import ai.chat2db.spi.jdbc.DefaultMetaService; -import ai.chat2db.spi.model.Database; -import ai.chat2db.spi.model.Table; -import ai.chat2db.spi.sql.SQLExecutor; -import org.apache.commons.lang3.StringUtils; - -public class RedisMetaData extends DefaultMetaService implements MetaData { - - @Override - public List databases(Connection connection) { - List databases = new ArrayList<>(); - return SQLExecutor.getInstance().execute(connection,"config get databases", resultSet -> { - try { - if (resultSet.next()) { - Object count = resultSet.getObject(2); - if(StringUtils.isNotBlank(count.toString())) { - for (int i = 0; i < Integer.parseInt(count.toString()); i++) { - Database database = Database.builder().name(String.valueOf(i)).build(); - databases.add(database); - } - } - } - } catch (SQLException e) { - throw new RuntimeException(e); - } - return databases; - }); - } - - @Override - public List tables(Connection connection, String databaseName, String schemaName, String tableName) { - return SQLExecutor.getInstance().execute(connection,"scan 0 MATCH * COUNT 1000", resultSet -> { - List
tables = new ArrayList<>(); - try { - while (resultSet.next()) { - ArrayList list = (ArrayList)resultSet.getObject(2); - for (Object object : list) { - Table table = new Table(); - table.setName(object.toString()); - tables.add(table); - } - } - } catch (SQLException e) { - throw new RuntimeException(e); - } - return tables; - }); - } -} diff --git a/chat2db-server/chat2db-plugins/chat2db-redis/src/main/java/ai/chat2db/plugin/redis/RedisPlugin.java b/chat2db-server/chat2db-plugins/chat2db-redis/src/main/java/ai/chat2db/plugin/redis/RedisPlugin.java deleted file mode 100644 index 53c988db4..000000000 --- a/chat2db-server/chat2db-plugins/chat2db-redis/src/main/java/ai/chat2db/plugin/redis/RedisPlugin.java +++ /dev/null @@ -1,27 +0,0 @@ -package ai.chat2db.plugin.redis; - - -import ai.chat2db.spi.DBManage; -import ai.chat2db.spi.MetaData; -import ai.chat2db.spi.Plugin; -import ai.chat2db.spi.config.DBConfig; -import ai.chat2db.spi.util.FileUtils; - -public class RedisPlugin implements Plugin { - - @Override - public DBConfig getDBConfig() { - return FileUtils.readJsonValue(this.getClass(),"redis.json", DBConfig.class); - - } - - @Override - public MetaData getMetaData() { - return new RedisMetaData(); - } - - @Override - public DBManage getDBManage() { - return new RedisDBManage(); - } -} diff --git a/chat2db-server/chat2db-plugins/chat2db-redis/src/main/java/ai/chat2db/plugin/redis/redis.json b/chat2db-server/chat2db-plugins/chat2db-redis/src/main/java/ai/chat2db/plugin/redis/redis.json deleted file mode 100644 index a10b33ae5..000000000 --- a/chat2db-server/chat2db-plugins/chat2db-redis/src/main/java/ai/chat2db/plugin/redis/redis.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "dbType": "REDIS", - "supportDatabase": false, - "supportSchema": false, - "driverConfigList": [ - { - "url": "jdbc:redis://127.0.0.1:6379/0", - "custom": false, - "defaultDriver": true, - "downloadJdbcDriverUrls": [ - "https://oss.sqlgpt.cn/lib/redis-jdbc-driver-1.3.jar" - ], - "jdbcDriver": "redis-jdbc-driver-1.3.jar", - "jdbcDriverClass": "jdbc.RedisDriver" - } - ], - "name": "Redis" -} diff --git a/chat2db-server/chat2db-plugins/chat2db-redis/src/main/resources/META-INF/services/ai.chat2db.spi.Plugin b/chat2db-server/chat2db-plugins/chat2db-redis/src/main/resources/META-INF/services/ai.chat2db.spi.Plugin deleted file mode 100644 index 3ccb61291..000000000 --- a/chat2db-server/chat2db-plugins/chat2db-redis/src/main/resources/META-INF/services/ai.chat2db.spi.Plugin +++ /dev/null @@ -1 +0,0 @@ -ai.chat2db.plugin.redis.RedisPlugin \ No newline at end of file diff --git a/chat2db-server/chat2db-plugins/chat2db-sqlite/src/main/java/ai/chat2db/plugin/sqlite/builder/SqliteBuilder.java b/chat2db-server/chat2db-plugins/chat2db-sqlite/src/main/java/ai/chat2db/plugin/sqlite/builder/SqliteBuilder.java index 8c28e9e60..5878d6675 100644 --- a/chat2db-server/chat2db-plugins/chat2db-sqlite/src/main/java/ai/chat2db/plugin/sqlite/builder/SqliteBuilder.java +++ b/chat2db-server/chat2db-plugins/chat2db-sqlite/src/main/java/ai/chat2db/plugin/sqlite/builder/SqliteBuilder.java @@ -10,7 +10,7 @@ import org.apache.commons.lang3.StringUtils; -public class SqliteBuilder extends DefaultSqlBuilder implements SqlBuilder { +public class SqliteBuilder extends DefaultSqlBuilder { @Override public String buildCreateTableSql(Table table) { StringBuilder script = new StringBuilder(); diff --git a/chat2db-server/chat2db-plugins/chat2db-sqlserver/src/main/java/ai/chat2db/plugin/sqlserver/builder/SqlServerSqlBuilder.java b/chat2db-server/chat2db-plugins/chat2db-sqlserver/src/main/java/ai/chat2db/plugin/sqlserver/builder/SqlServerSqlBuilder.java index 77e8d524f..85a0baee8 100644 --- a/chat2db-server/chat2db-plugins/chat2db-sqlserver/src/main/java/ai/chat2db/plugin/sqlserver/builder/SqlServerSqlBuilder.java +++ b/chat2db-server/chat2db-plugins/chat2db-sqlserver/src/main/java/ai/chat2db/plugin/sqlserver/builder/SqlServerSqlBuilder.java @@ -8,7 +8,7 @@ import ai.chat2db.spi.sql.Chat2DBContext; import org.apache.commons.lang3.StringUtils; -public class SqlServerSqlBuilder extends DefaultSqlBuilder implements SqlBuilder { +public class SqlServerSqlBuilder extends DefaultSqlBuilder { @Override public String buildCreateTableSql(Table table) { StringBuilder script = new StringBuilder(); diff --git a/chat2db-server/chat2db-plugins/pom.xml b/chat2db-server/chat2db-plugins/pom.xml index d058b04e7..17be8e0e8 100644 --- a/chat2db-server/chat2db-plugins/pom.xml +++ b/chat2db-server/chat2db-plugins/pom.xml @@ -28,7 +28,6 @@ chat2db-mongodbchat2db-prestochat2db-hive - chat2db-redischat2db-kingbase diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/param/DlExecuteParam.java b/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/param/DlExecuteParam.java index 92c607cd6..63b12a260 100644 --- a/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/param/DlExecuteParam.java +++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/param/DlExecuteParam.java @@ -37,6 +37,9 @@ public class DlExecuteParam { private String databaseName; + private String tableName; + + /** * schema name */ diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/DlTemplateService.java b/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/DlTemplateService.java index 33b21aaed..b4a745c80 100644 --- a/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/DlTemplateService.java +++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/DlTemplateService.java @@ -27,6 +27,14 @@ public interface DlTemplateService { ListResult execute(DlExecuteParam param); + /** + * + * @param param + * @return + */ + ListResult executeSelectTable(DlExecuteParam param); + + /** * Data source execution update * diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/pom.xml b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/pom.xml index ce19bc340..47a86c68a 100644 --- a/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/pom.xml +++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/pom.xml @@ -111,11 +111,6 @@ chat2db-presto ${revision} - - ai.chat2db - chat2db-redis - ${revision} - ai.chat2db chat2db-sqlite diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/DlTemplateServiceImpl.java b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/DlTemplateServiceImpl.java index f2f0eb4fc..56dc646ac 100644 --- a/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/DlTemplateServiceImpl.java +++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/DlTemplateServiceImpl.java @@ -17,7 +17,6 @@ import ai.chat2db.spi.model.*; import ai.chat2db.spi.sql.Chat2DBContext; import ai.chat2db.spi.sql.ConnectInfo; -import ai.chat2db.spi.sql.SQLExecutor; import ai.chat2db.spi.util.JdbcUtils; import ai.chat2db.spi.util.SqlUtils; import com.alibaba.druid.DbType; @@ -28,6 +27,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -61,12 +61,15 @@ public ListResult execute(DlExecuteParam param) { CommandExecutor executor = Chat2DBContext.getMetaData().getCommandExecutor(); Command command = commandConverter.param2model(param); List results = executor.execute(command); + return reBuildHeader(results,param.getSchemaName(),param.getDatabaseName()); + } + + private ListResult reBuildHeader(List results,String schemaName,String databaseName){ ListResult listResult = ListResult.of(results); for (ExecuteResult executeResult : results) { List
headers = executeResult.getHeaderList(); if (executeResult.getSuccess() && executeResult.isCanEdit() && CollectionUtils.isNotEmpty(headers)) { - headers = setColumnInfo(headers, executeResult.getTableName(), param.getSchemaName(), - param.getDatabaseName()); + headers = setColumnInfo(headers, executeResult.getTableName(), schemaName, databaseName); executeResult.setHeaderList(headers); } if (!executeResult.getSuccess()) { @@ -77,12 +80,13 @@ public ListResult execute(DlExecuteParam param) { addOperationLog(executeResult); } return listResult; + } -// if ("SQLSERVER".equalsIgnoreCase(type)) { -// RemoveSpecialGO(param); -// } - - + @Override + public ListResult executeSelectTable(DlExecuteParam param) { + Command command = commandConverter.param2model(param); + List results = Chat2DBContext.getMetaData().getCommandExecutor().executeSelectTable(command); + return reBuildHeader(results,param.getSchemaName(),param.getDatabaseName()); } @Override @@ -159,8 +163,9 @@ public DataResult count(DlCountParam param) { @Override public DataResult updateSelectResult(UpdateSelectResultParam param) { SqlBuilder sqlBuilder = Chat2DBContext.getSqlBuilder(); - String sql = sqlBuilder.generateSqlBasedOnResults(param.getTableName(), param.getHeaderList(), - param.getOperations()); + QueryResult queryResult = new QueryResult(); + BeanUtils.copyProperties(param, queryResult); + String sql = sqlBuilder.buildSqlByQuery(queryResult); return DataResult.of(sql); } diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/RdbDmlController.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/RdbDmlController.java index 85f474305..1abc17144 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/RdbDmlController.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/RdbDmlController.java @@ -97,18 +97,7 @@ private String getClientId() { @RequestMapping(value = "/execute_table", method = {RequestMethod.POST, RequestMethod.PUT}) public ListResult executeTable(@RequestBody DmlTableRequest request) { DlExecuteParam param = rdbWebConverter.request2param(request); - // parse sql - String type = Chat2DBContext.getConnectInfo().getDbType(); - MetaData metaData = Chat2DBContext.getMetaData(); - if (DataSourceTypeEnum.MONGODB.getCode().equals(type)) { - param.setSql("db." + request.getTableName() + ".find()"); - } else if (DataSourceTypeEnum.SQLSERVER.getCode().equals(type)){ - param.setSql("select * from" + metaData.getMetaDataName(request.getSchemaName()) + "." + metaData.getMetaDataName(request.getTableName())); - }else { - // Splice `tableName` to avoid the problem of keywords being occupied - param.setSql("select * from " + metaData.getMetaDataName(request.getTableName())); - } - return dlTemplateService.execute(param) + return dlTemplateService.executeSelectTable(param) .map(rdbWebConverter::dto2vo); } diff --git a/chat2db-server/chat2db-spi/pom.xml b/chat2db-server/chat2db-spi/pom.xml index 0a691f53f..de441bc9f 100644 --- a/chat2db-server/chat2db-spi/pom.xml +++ b/chat2db-server/chat2db-spi/pom.xml @@ -86,4 +86,21 @@ bson + + + + org.apache.maven.plugins + maven-source-plugin + 3.2.1 + + + attach-sources + + jar + + + + + + \ No newline at end of file diff --git a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/CommandExecutor.java b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/CommandExecutor.java index ee5a9c942..35b878956 100644 --- a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/CommandExecutor.java +++ b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/CommandExecutor.java @@ -27,6 +27,12 @@ public interface CommandExecutor { ExecuteResult executeUpdate(String sql, Connection connection, int n)throws SQLException; + /** + * Execute command + */ + List executeSelectTable(Command command); + + /** * * diff --git a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/MetaData.java b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/MetaData.java index f7e377002..771bfbe89 100644 --- a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/MetaData.java +++ b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/MetaData.java @@ -3,7 +3,9 @@ import java.sql.Connection; import java.util.List; +import ai.chat2db.server.tools.base.wrapper.result.PageResult; import ai.chat2db.spi.model.*; +import cn.hutool.db.Page; import jakarta.validation.constraints.NotEmpty; /** @@ -52,6 +54,19 @@ String tableDDL(Connection connection, @NotEmpty String databaseName, String sch */ List
tables(Connection connection, @NotEmpty String databaseName, String schemaName, String tableName); + + /** + * Querying all table under a schema. + * + * @param connection + * @param databaseName + * @param schemaName + * @param tableNamePattern + * @param pageNo + * @param pageSize + * @return + */ + PageResult
tables(Connection connection, String databaseName, String schemaName, String tableNamePattern, int pageNo, int pageSize); /** * Querying view information. * diff --git a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/SqlBuilder.java b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/SqlBuilder.java index 589db131c..9089634cb 100644 --- a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/SqlBuilder.java +++ b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/SqlBuilder.java @@ -4,7 +4,7 @@ import java.util.List; -public interface SqlBuilder { +public interface SqlBuilder { /** * Generate create table sql @@ -12,7 +12,7 @@ public interface SqlBuilder { * @param table * @return */ - String buildCreateTableSql(Table table); + String buildCreateTableSql(T table); /** @@ -22,7 +22,7 @@ public interface SqlBuilder { * @param oldTable * @return */ - String buildModifyTaleSql(Table oldTable, Table newTable); + String buildModifyTaleSql(T oldTable, T newTable); /** @@ -79,7 +79,7 @@ public interface SqlBuilder { /** * generate sql based on results */ - String generateSqlBasedOnResults(String tableName, List
headerList, List operations); + String buildSqlByQuery(QueryResult queryResult); /** * DML SQL @@ -87,5 +87,5 @@ public interface SqlBuilder { * @param type * @return */ - String getTableDmlSql(Table table,String type); + String getTableDmlSql(T table,String type); } diff --git a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/jdbc/DefaultMetaService.java b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/jdbc/DefaultMetaService.java index a3ba280bb..a05313551 100644 --- a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/jdbc/DefaultMetaService.java +++ b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/jdbc/DefaultMetaService.java @@ -3,9 +3,9 @@ import java.sql.Connection; import java.util.Arrays; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; +import ai.chat2db.server.tools.base.wrapper.result.PageResult; import ai.chat2db.spi.CommandExecutor; import ai.chat2db.spi.MetaData; import ai.chat2db.spi.SqlBuilder; @@ -48,6 +48,16 @@ public List
tables(Connection connection, String databaseName, String sch return SQLExecutor.getInstance().tables(connection, StringUtils.isEmpty(databaseName) ? null : databaseName, StringUtils.isEmpty(schemaName) ? null : schemaName, tableName, new String[]{"TABLE","SYSTEM TABLE"}); } + @Override + public PageResult
tables(Connection connection, String databaseName, String schemaName, String tableNamePattern, int pageNo, int pageSize) { + List
tables = tables(connection, databaseName, schemaName, tableNamePattern); + if(CollectionUtils.isEmpty(tables)){ + return PageResult.of(tables,0L,pageNo, pageSize); + } + List result = tables.stream().skip((pageNo - 1) * pageSize).limit(pageSize).collect(Collectors.toList()); + return PageResult.of(result, (long) tables.size(), pageNo, pageSize); + } + @Override public Table view(Connection connection, String databaseName, String schemaName, String viewName) { return null; diff --git a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/jdbc/DefaultSqlBuilder.java b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/jdbc/DefaultSqlBuilder.java index 722f6b5f5..d63d1603c 100644 --- a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/jdbc/DefaultSqlBuilder.java +++ b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/jdbc/DefaultSqlBuilder.java @@ -19,7 +19,7 @@ import java.util.ArrayList; import java.util.List; -public class DefaultSqlBuilder implements SqlBuilder { +public class DefaultSqlBuilder implements SqlBuilder
{ @Override @@ -90,8 +90,10 @@ public String buildOrderBySql(String originSql, List orderByList) { } @Override - public String generateSqlBasedOnResults(String tableName, List
headerList, List operations) { - + public String buildSqlByQuery(QueryResult queryResult) { + List
headerList = queryResult.getHeaderList(); + List operations = queryResult.getOperations(); + String tableName = queryResult.getTableName(); StringBuilder stringBuilder = new StringBuilder(); MetaData metaSchema = Chat2DBContext.getMetaData(); List keyColumns = getPrimaryColumns(headerList); diff --git a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/Command.java b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/Command.java index fb6246032..67a5e732b 100644 --- a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/Command.java +++ b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/Command.java @@ -38,6 +38,11 @@ public class Command implements Serializable { */ private String schemaName; + /** + * + */ + private String tableName; + /** *Page coding * Only available for select statements diff --git a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/ExecuteResult.java b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/ExecuteResult.java index d602f9834..e7bba8841 100644 --- a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/ExecuteResult.java +++ b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/ExecuteResult.java @@ -2,6 +2,7 @@ import java.io.Serializable; import java.util.List; +import java.util.Map; import lombok.AllArgsConstructor; import lombok.Data; @@ -104,7 +105,12 @@ public class ExecuteResult implements Serializable { private boolean canEdit; /** - * Table Name + * Table Name for the result */ private String tableName; + + /** + * Extra information that can be used by the plugin + */ + private Map extra; } diff --git a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/QueryResult.java b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/QueryResult.java new file mode 100644 index 000000000..e125060e9 --- /dev/null +++ b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/QueryResult.java @@ -0,0 +1,16 @@ +package ai.chat2db.spi.model; + +import lombok.Data; + +import java.io.Serializable; +import java.util.List; +import java.util.Map; + +@Data +public class QueryResult implements Serializable { + + private String tableName; + private List
headerList; + private List operations; + private Map extra; +} diff --git a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/SQLExecutor.java b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/SQLExecutor.java index 11ea0c4c2..cb5881b25 100644 --- a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/SQLExecutor.java +++ b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/SQLExecutor.java @@ -16,6 +16,7 @@ import ai.chat2db.server.tools.common.util.EasyCollectionUtils; import ai.chat2db.server.tools.common.util.I18nUtils; import ai.chat2db.spi.CommandExecutor; +import ai.chat2db.spi.MetaData; import ai.chat2db.spi.ValueHandler; import ai.chat2db.spi.enums.DataTypeEnum; import ai.chat2db.spi.enums.SqlTypeEnum; @@ -39,7 +40,7 @@ import org.springframework.util.Assert; /** - * Dbhub unified database connection management + * Dbhub unified database connection management * * @author jipengfei */ @@ -74,6 +75,7 @@ public R execute(Connection connection, String sql, ResultSetFunction fun } return null; } + public void execute(Connection connection, String sql, ResultSetConsumer consumer) { log.info("execute:{}", sql); try (Statement stmt = connection.createStatement()) { @@ -113,10 +115,10 @@ public void execute(Connection connection, String sql, Consumer> he List
headerList = Lists.newArrayListWithExpectedSize(col); for (int i = 1; i <= col; i++) { headerList.add(Header.builder() - .dataType(JdbcUtils.resolveDataType( - resultSetMetaData.getColumnTypeName(i), resultSetMetaData.getColumnType(i)).getCode()) - .name(ResultSetUtils.getColumnName(resultSetMetaData, i)) - .build()); + .dataType(JdbcUtils.resolveDataType( + resultSetMetaData.getColumnTypeName(i), resultSetMetaData.getColumnType(i)).getCode()) + .name(ResultSetUtils.getColumnName(resultSetMetaData, i)) + .build()); } headerConsumer.accept(headerList); @@ -144,13 +146,13 @@ public void execute(Connection connection, String sql, Consumer> he * @throws SQLException */ public ExecuteResult execute(final String sql, Connection connection, ValueHandler valueHandler) - throws SQLException { + throws SQLException { return execute(sql, connection, true, null, null, valueHandler); } @Override public ExecuteResult executeUpdate(String sql, Connection connection, int n) - throws SQLException { + throws SQLException { Assert.notNull(sql, "SQL must not be null"); log.info("execute:{}", sql); // connection.setAutoCommit(false); @@ -160,29 +162,40 @@ public ExecuteResult executeUpdate(String sql, Connection connection, int n) if (affectedRows != n) { executeResult.setSuccess(false); executeResult.setMessage("Update error " + sql + " update affectedRows = " + affectedRows - + ", Each SQL statement should update no more than one record. Please use a unique key for " - + "updates."); + + ", Each SQL statement should update no more than one record. Please use a unique key for " + + "updates."); // connection.rollback(); } } return executeResult; } + @Override + public List executeSelectTable(Command command) { + MetaData metaData = Chat2DBContext.getMetaData(); + String tableName = metaData.getMetaDataName(command.getDatabaseName(), command.getSchemaName(), + command.getTableName()); + String sql = "select * from " + tableName; + command.setScript(sql); + return execute(command); + } + /** * Executes the given SQL query using the provided connection. - * @param sql The SQL query to be executed. - * @param connection The database connection to use for the query. + * + * @param sql The SQL query to be executed. + * @param connection The database connection to use for the query. * @param limitRowSize Flag to indicate if row size should be limited. - * @param offset The starting point of rows to fetch in the result set. - * @param count The number of rows to fetch from the result set. + * @param offset The starting point of rows to fetch in the result set. + * @param count The number of rows to fetch from the result set. * @param valueHandler Handles the processing of the result set values. * @return ExecuteResult containing the result of the execution. * @throws SQLException If there is any SQL related error. */ public ExecuteResult execute(final String sql, Connection connection, boolean limitRowSize, Integer offset, - Integer count, ValueHandler valueHandler) - throws SQLException { + Integer count, ValueHandler valueHandler) + throws SQLException { Assert.notNull(sql, "SQL must not be null"); log.info("execute:{}", sql); @@ -227,11 +240,11 @@ public ExecuteResult execute(final String sql, Connection connection, boolean li continue; } String dataType = JdbcUtils.resolveDataType( - resultSetMetaData.getColumnTypeName(i), resultSetMetaData.getColumnType(i)).getCode(); + resultSetMetaData.getColumnTypeName(i), resultSetMetaData.getColumnType(i)).getCode(); headerList.add(Header.builder() - .dataType(dataType) - .name(name) - .build()); + .dataType(dataType) + .name(name) + .build()); } // Get data information @@ -271,16 +284,16 @@ public ExecuteResult execute(final String sql, Connection connection, boolean li if (o instanceof Document document) { for (String string : document.keySet()) { headerListMap.computeIfAbsent(string, k -> Header.builder() - .dataType("string") - .name(string) - .build()); + .dataType("string") + .name(string) + .build()); row.put(string, Objects.toString(document.get(string))); } } else { headerListMap.computeIfAbsent("_unknown", k -> Header.builder() - .dataType("string") - .name("_unknown") - .build()); + .dataType("string") + .name("_unknown") + .build()); row.put("_unknown", Objects.toString(o)); } } @@ -393,12 +406,12 @@ public List schemas(Connection connection, String databaseName, String s * @return */ public List
tables(Connection connection, String databaseName, String schemaName, String tableName, - String types[]) { + String types[]) { try { DatabaseMetaData metadata = connection.getMetaData(); ResultSet resultSet = metadata.getTables(databaseName, schemaName, tableName, - types); + types); // If connection is mysql if ("MySQL".equalsIgnoreCase(metadata.getDatabaseProductName())) { // Get the comment of mysql table @@ -438,10 +451,10 @@ public List
tables(Connection connection, String databaseName, String sch * @return */ public List columns(Connection connection, String databaseName, String schemaName, String - tableName, - String columnName) { + tableName, + String columnName) { try (ResultSet resultSet = connection.getMetaData().getColumns(databaseName, schemaName, tableName, - columnName)) { + columnName)) { return ResultSetUtils.toObjectList(resultSet, TableColumn.class); } catch (Exception e) { throw new RuntimeException(e); @@ -460,22 +473,22 @@ public List columns(Connection connection, String databaseName, Str public List indexes(Connection connection, String databaseName, String schemaName, String tableName) { List tableIndices = Lists.newArrayList(); try (ResultSet resultSet = connection.getMetaData().getIndexInfo(databaseName, schemaName, tableName, - false, - false)) { + false, + false)) { List tableIndexColumns = ResultSetUtils.toObjectList(resultSet, TableIndexColumn.class); tableIndexColumns.stream().filter(c -> c.getIndexName() != null).collect( - Collectors.groupingBy(TableIndexColumn::getIndexName)).entrySet() - .stream().forEach(entry -> { - TableIndex tableIndex = new TableIndex(); - TableIndexColumn column = entry.getValue().get(0); - tableIndex.setName(entry.getKey()); - tableIndex.setTableName(column.getTableName()); - tableIndex.setSchemaName(column.getSchemaName()); - tableIndex.setDatabaseName(column.getDatabaseName()); - tableIndex.setUnique(!column.getNonUnique()); - tableIndex.setColumnList(entry.getValue()); - tableIndices.add(tableIndex); - }); + Collectors.groupingBy(TableIndexColumn::getIndexName)).entrySet() + .stream().forEach(entry -> { + TableIndex tableIndex = new TableIndex(); + TableIndexColumn column = entry.getValue().get(0); + tableIndex.setName(entry.getKey()); + tableIndex.setTableName(column.getTableName()); + tableIndex.setSchemaName(column.getSchemaName()); + tableIndex.setDatabaseName(column.getDatabaseName()); + tableIndex.setUnique(!column.getNonUnique()); + tableIndex.setColumnList(entry.getValue()); + tableIndices.add(tableIndex); + }); } catch (SQLException e) { throw new RuntimeException(e); } @@ -491,7 +504,7 @@ public List indexes(Connection connection, String databaseName, Stri * @return List */ public List functions(Connection connection, String databaseName, - String schemaName) { + String schemaName) { try (ResultSet resultSet = connection.getMetaData().getFunctions(databaseName, schemaName, null);) { return ResultSetUtils.toObjectList(resultSet, ai.chat2db.spi.model.Function.class); } catch (Exception e) {