From 288b8cc71112416d32264145140d67252db18b34 Mon Sep 17 00:00:00 2001 From: jipengfei-jpf <1558143046@qq.com> Date: Sun, 16 Jul 2023 20:48:10 +0800 Subject: [PATCH] support function trigger view --- .../domain/api/service/FunctionService.java | 22 ++ .../domain/api/service/ProcedureService.java | 16 ++ .../domain/api/service/TriggerService.java | 17 ++ .../domain/api/service/ViewService.java | 22 ++ .../domain/core/impl/FunctionServiceImpl.java | 15 ++ .../core/impl/ProcedureServiceImpl.java | 16 ++ .../domain/core/impl/TriggerServiceImpl.java | 15 ++ .../domain/core/impl/ViewServiceImpl.java | 16 ++ .../controller/rdb/DatabaseController.java | 93 +++++++++ .../controller/rdb/FunctionController.java | 27 +++ .../controller/rdb/ProcedureController.java | 24 +++ .../api/controller/rdb/RdbDdlController.java | 3 + .../api/controller/rdb/SchemaController.java | 90 ++++++++ .../api/controller/rdb/TableController.java | 197 ++++++++++++++++++ .../api/controller/rdb/TriggerController.java | 27 +++ .../api/controller/rdb/ViewController.java | 35 ++++ .../main/java/ai/chat2db/spi/MetaData.java | 2 +- .../chat2db/spi/jdbc/DefaultMetaService.java | 2 +- 18 files changed, 637 insertions(+), 2 deletions(-) create mode 100644 chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/FunctionService.java create mode 100644 chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/ProcedureService.java create mode 100644 chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/TriggerService.java create mode 100644 chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/ViewService.java create mode 100644 chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/FunctionServiceImpl.java create mode 100644 chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/ProcedureServiceImpl.java create mode 100644 chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/TriggerServiceImpl.java create mode 100644 chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/ViewServiceImpl.java create mode 100644 chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/DatabaseController.java create mode 100644 chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/FunctionController.java create mode 100644 chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/ProcedureController.java create mode 100644 chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/SchemaController.java create mode 100644 chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/TableController.java create mode 100644 chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/TriggerController.java create mode 100644 chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/ViewController.java diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/FunctionService.java b/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/FunctionService.java new file mode 100644 index 000000000..779cc4a56 --- /dev/null +++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/FunctionService.java @@ -0,0 +1,22 @@ +package ai.chat2db.server.domain.api.service; + +import ai.chat2db.server.tools.base.wrapper.result.ListResult; +import ai.chat2db.spi.model.Function; +import jakarta.validation.constraints.NotEmpty; + +/** + * author jipengfei + * date 2021/9/23 15:22 + */ +public interface FunctionService { + + /** + * Querying all functions under a schema. + * + * @param databaseName + * @return + */ + ListResult functions(@NotEmpty String databaseName, String schemaName); + + +} diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/ProcedureService.java b/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/ProcedureService.java new file mode 100644 index 000000000..3e7004e3c --- /dev/null +++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/ProcedureService.java @@ -0,0 +1,16 @@ +package ai.chat2db.server.domain.api.service; + +import ai.chat2db.server.tools.base.wrapper.result.ListResult; +import ai.chat2db.spi.model.Procedure; +import jakarta.validation.constraints.NotEmpty; + +public interface ProcedureService { + + /** + * Querying all procedures under a schema. + * + * @param databaseName + * @return + */ + ListResult procedures(@NotEmpty String databaseName, String schemaName); +} diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/TriggerService.java b/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/TriggerService.java new file mode 100644 index 000000000..e051b93b5 --- /dev/null +++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/TriggerService.java @@ -0,0 +1,17 @@ +package ai.chat2db.server.domain.api.service; + +import ai.chat2db.server.tools.base.wrapper.result.ListResult; +import ai.chat2db.spi.model.Trigger; +import jakarta.validation.constraints.NotEmpty; + +public interface TriggerService { + + /** + * Querying all triggers under a schema. + * + * @param databaseName + * @return + */ + ListResult triggers(@NotEmpty String databaseName, String schemaName); + +} diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/ViewService.java b/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/ViewService.java new file mode 100644 index 000000000..b79fe9c3e --- /dev/null +++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/ViewService.java @@ -0,0 +1,22 @@ +package ai.chat2db.server.domain.api.service; + +import ai.chat2db.server.tools.base.wrapper.result.ListResult; +import ai.chat2db.spi.model.Table; +import jakarta.validation.constraints.NotEmpty; + +/** + * author jipengfei + * date 2021/9/23 15:22 + */ +public interface ViewService { + + /** + * Querying all views under a schema. + * + * @param databaseName + * @return + */ + ListResult views(@NotEmpty String databaseName, String schemaName); + + +} diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/FunctionServiceImpl.java b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/FunctionServiceImpl.java new file mode 100644 index 000000000..2844055bb --- /dev/null +++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/FunctionServiceImpl.java @@ -0,0 +1,15 @@ +package ai.chat2db.server.domain.core.impl; + +import ai.chat2db.server.domain.api.service.FunctionService; +import ai.chat2db.server.tools.base.wrapper.result.ListResult; +import ai.chat2db.spi.model.Function; +import ai.chat2db.spi.sql.Chat2DBContext; +import org.springframework.stereotype.Service; + +@Service +public class FunctionServiceImpl implements FunctionService { + @Override + public ListResult functions(String databaseName, String schemaName) { + return ListResult.of(Chat2DBContext.getMetaData().functions(databaseName, schemaName)); + } +} diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/ProcedureServiceImpl.java b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/ProcedureServiceImpl.java new file mode 100644 index 000000000..8b17720c8 --- /dev/null +++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/ProcedureServiceImpl.java @@ -0,0 +1,16 @@ +package ai.chat2db.server.domain.core.impl; + +import ai.chat2db.server.domain.api.service.ProcedureService; +import ai.chat2db.server.tools.base.wrapper.result.ListResult; +import ai.chat2db.spi.model.Procedure; +import ai.chat2db.spi.sql.Chat2DBContext; +import org.springframework.stereotype.Service; + +@Service +public class ProcedureServiceImpl implements ProcedureService { + + @Override + public ListResult procedures(String databaseName, String schemaName) { + return ListResult.of(Chat2DBContext.getMetaData().procedures(databaseName, schemaName)); + } +} diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/TriggerServiceImpl.java b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/TriggerServiceImpl.java new file mode 100644 index 000000000..d72174b70 --- /dev/null +++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/TriggerServiceImpl.java @@ -0,0 +1,15 @@ +package ai.chat2db.server.domain.core.impl; + +import ai.chat2db.server.domain.api.service.TriggerService; +import ai.chat2db.server.tools.base.wrapper.result.ListResult; +import ai.chat2db.spi.model.Trigger; +import ai.chat2db.spi.sql.Chat2DBContext; +import org.springframework.stereotype.Service; + +@Service +public class TriggerServiceImpl implements TriggerService { + @Override + public ListResult triggers(String databaseName, String schemaName) { + return ListResult.of(Chat2DBContext.getMetaData().triggers(databaseName, schemaName)); + } +} diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/ViewServiceImpl.java b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/ViewServiceImpl.java new file mode 100644 index 000000000..150791fbb --- /dev/null +++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/ViewServiceImpl.java @@ -0,0 +1,16 @@ +package ai.chat2db.server.domain.core.impl; + +import ai.chat2db.server.domain.api.service.ViewService; +import ai.chat2db.server.tools.base.wrapper.result.ListResult; +import ai.chat2db.spi.model.Table; +import ai.chat2db.spi.sql.Chat2DBContext; +import org.springframework.stereotype.Service; + +@Service +public class ViewServiceImpl implements ViewService { + + @Override + public ListResult
views(String databaseName, String schemaName) { + return ListResult.of(Chat2DBContext.getMetaData().views(databaseName, schemaName)); + } +} diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/DatabaseController.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/DatabaseController.java new file mode 100644 index 000000000..e8a4e46f5 --- /dev/null +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/DatabaseController.java @@ -0,0 +1,93 @@ +package ai.chat2db.server.web.api.controller.rdb; + +import ai.chat2db.server.domain.api.param.DatabaseOperationParam; +import ai.chat2db.server.domain.api.param.MetaDataQueryParam; +import ai.chat2db.server.domain.api.service.DatabaseService; +import ai.chat2db.server.domain.api.service.DlTemplateService; +import ai.chat2db.server.domain.api.service.TableService; +import ai.chat2db.server.tools.base.wrapper.result.ActionResult; +import ai.chat2db.server.tools.base.wrapper.result.DataResult; +import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect; +import ai.chat2db.server.web.api.controller.data.source.request.DataSourceBaseRequest; +import ai.chat2db.server.web.api.controller.rdb.converter.RdbWebConverter; +import ai.chat2db.server.web.api.controller.rdb.request.UpdateDatabaseRequest; +import ai.chat2db.server.web.api.controller.rdb.vo.MetaSchemaVO; +import ai.chat2db.spi.model.MetaSchema; +import jakarta.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@ConnectionInfoAspect +@RequestMapping("/api/rdb/datbase") +@RestController +public class DatabaseController { + + @Autowired + private TableService tableService; + + @Autowired + private DlTemplateService dlTemplateService; + + @Autowired + private RdbWebConverter rdbWebConverter; + + @Autowired + private DatabaseService databaseService; + /** + * 查询数据库里包含的database_schema_list + * + * @param request + * @return + */ + @GetMapping("/database_schema_list") + public DataResult databaseSchemaList(@Valid DataSourceBaseRequest request) { + MetaDataQueryParam queryParam = MetaDataQueryParam.builder().dataSourceId(request.getDataSourceId()).refresh( + request.isRefresh()).build(); + DataResult result = databaseService.queryDatabaseSchema(queryParam); + MetaSchemaVO schemaDto2vo = rdbWebConverter.metaSchemaDto2vo(result.getData()); + return DataResult.of(schemaDto2vo); + } + + + /** + * 删除数据库 + * + * @param request + * @return + */ + @PostMapping("/delete_database") + public ActionResult deleteDatabase(@Valid @RequestBody DataSourceBaseRequest request) { + DatabaseOperationParam param = DatabaseOperationParam.builder().databaseName(request.getDatabaseName()).build(); + return databaseService.deleteDatabase(param); + } + + /** + * 创建database + * + * @param request + * @return + */ + @PostMapping("/create_database") + public ActionResult createDatabase(@Valid @RequestBody DataSourceBaseRequest request) { + DatabaseOperationParam param = DatabaseOperationParam.builder().databaseName(request.getDatabaseName()).build(); + return databaseService.createDatabase(param); + } + + + /** + * 修改database + * + * @param request + * @return + */ + @PostMapping("/modify_database") + public ActionResult modifyDatabase(@Valid @RequestBody UpdateDatabaseRequest request) { + DatabaseOperationParam param = DatabaseOperationParam.builder().databaseName(request.getDatabaseName()) + .newDatabaseName(request.getNewDatabaseName()).build(); + return databaseService.modifyDatabase(param); + } +} diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/FunctionController.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/FunctionController.java new file mode 100644 index 000000000..aaad10328 --- /dev/null +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/FunctionController.java @@ -0,0 +1,27 @@ +package ai.chat2db.server.web.api.controller.rdb; + +import ai.chat2db.server.domain.api.service.FunctionService; +import ai.chat2db.server.tools.base.wrapper.result.ListResult; +import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect; +import ai.chat2db.server.web.api.controller.rdb.request.TableBriefQueryRequest; +import ai.chat2db.spi.model.Function; +import jakarta.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@ConnectionInfoAspect +@RequestMapping("/api/rdb/function") +@RestController +public class FunctionController { + + @Autowired + private FunctionService functionService; + + + @GetMapping("/list") + public ListResult list(@Valid TableBriefQueryRequest request) { + return functionService.functions(request.getDatabaseName(), request.getSchemaName()); + } +} diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/ProcedureController.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/ProcedureController.java new file mode 100644 index 000000000..3dd2e28bc --- /dev/null +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/ProcedureController.java @@ -0,0 +1,24 @@ +package ai.chat2db.server.web.api.controller.rdb; + +import ai.chat2db.server.domain.api.service.ProcedureService; +import ai.chat2db.server.tools.base.wrapper.result.ListResult; +import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect; +import ai.chat2db.server.web.api.controller.rdb.request.TableBriefQueryRequest; +import ai.chat2db.spi.model.Procedure; +import jakarta.validation.Valid; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@ConnectionInfoAspect +@RequestMapping("/api/rdb/procedure") +@RestController +public class ProcedureController { + + private ProcedureService procedureService; + + @GetMapping("/list") + public ListResult list(@Valid TableBriefQueryRequest request) { + return procedureService.procedures(request.getDatabaseName(), request.getSchemaName()); + } +} diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/RdbDdlController.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/RdbDdlController.java index 0c0eb29ee..f76ae22e2 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/RdbDdlController.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/RdbDdlController.java @@ -45,6 +45,7 @@ @ConnectionInfoAspect @RequestMapping("/api/rdb/ddl") @RestController +@Deprecated public class RdbDdlController { @Autowired @@ -183,6 +184,8 @@ public ActionResult modifySchema(@Valid @RequestBody UpdateSchemaRequest request return databaseService.modifySchema(param); } + + /** * 查询当前DB下的表columns * d diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/SchemaController.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/SchemaController.java new file mode 100644 index 000000000..3ffd95312 --- /dev/null +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/SchemaController.java @@ -0,0 +1,90 @@ +package ai.chat2db.server.web.api.controller.rdb; + +import java.util.List; + +import ai.chat2db.server.domain.api.param.SchemaOperationParam; +import ai.chat2db.server.domain.api.param.SchemaQueryParam; +import ai.chat2db.server.domain.api.service.DatabaseService; +import ai.chat2db.server.domain.api.service.DlTemplateService; +import ai.chat2db.server.domain.api.service.TableService; +import ai.chat2db.server.tools.base.wrapper.result.ActionResult; +import ai.chat2db.server.tools.base.wrapper.result.ListResult; +import ai.chat2db.server.web.api.controller.data.source.request.DataSourceBaseRequest; +import ai.chat2db.server.web.api.controller.rdb.converter.RdbWebConverter; +import ai.chat2db.server.web.api.controller.rdb.request.UpdateSchemaRequest; +import ai.chat2db.server.web.api.controller.rdb.vo.SchemaVO; +import ai.chat2db.spi.model.Schema; +import jakarta.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +public class SchemaController { + + + @Autowired + private TableService tableService; + + @Autowired + private DlTemplateService dlTemplateService; + + @Autowired + private RdbWebConverter rdbWebConverter; + + @Autowired + private DatabaseService databaseService; + + /** + * 查询数据库里包含的schema_list + * + * @param request + * @return + */ + @GetMapping("/list") + public ListResult list(@Valid DataSourceBaseRequest request) { + SchemaQueryParam queryParam = SchemaQueryParam.builder().dataBaseName(request.getDatabaseName()).build(); + ListResult tableColumns = databaseService.querySchema(queryParam); + List tableVOS = rdbWebConverter.schemaDto2vo(tableColumns.getData()); + return ListResult.of(tableVOS); + } + + /** + * 删除schema + * + * @param request + * @return + */ + @PostMapping("/delete_schema") + public ActionResult deleteSchema(@Valid @RequestBody DataSourceBaseRequest request) { + SchemaOperationParam param = SchemaOperationParam.builder().databaseName(request.getDatabaseName()) + .schemaName(request.getSchemaName()).build(); + return databaseService.deleteSchema(param); + } + + /** + * 创建schema + * + * @param request + * @return + */ + @PostMapping("/create_schema") + public ActionResult createSchema(@Valid @RequestBody DataSourceBaseRequest request) { + SchemaOperationParam param = SchemaOperationParam.builder().databaseName(request.getDatabaseName()) + .schemaName(request.getSchemaName()).build(); + return databaseService.createSchema(param); + } + + /** + * 创建database + * + * @param request + * @return + */ + @PostMapping("/modify_schema") + public ActionResult modifySchema(@Valid @RequestBody UpdateSchemaRequest request) { + SchemaOperationParam param = SchemaOperationParam.builder().databaseName(request.getDatabaseName()) + .schemaName(request.getSchemaName()).newSchemaName(request.getNewSchemaName()).build(); + return databaseService.modifySchema(param); + } +} diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/TableController.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/TableController.java new file mode 100644 index 000000000..2be6f0623 --- /dev/null +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/TableController.java @@ -0,0 +1,197 @@ +package ai.chat2db.server.web.api.controller.rdb; + +import java.util.List; + +import ai.chat2db.server.domain.api.param.DropParam; +import ai.chat2db.server.domain.api.param.ShowCreateTableParam; +import ai.chat2db.server.domain.api.param.TablePageQueryParam; +import ai.chat2db.server.domain.api.param.TableQueryParam; +import ai.chat2db.server.domain.api.param.TableSelector; +import ai.chat2db.server.domain.api.service.DatabaseService; +import ai.chat2db.server.domain.api.service.DlTemplateService; +import ai.chat2db.server.domain.api.service.TableService; +import ai.chat2db.server.tools.base.wrapper.result.ActionResult; +import ai.chat2db.server.tools.base.wrapper.result.DataResult; +import ai.chat2db.server.tools.base.wrapper.result.ListResult; +import ai.chat2db.server.tools.base.wrapper.result.PageResult; +import ai.chat2db.server.tools.base.wrapper.result.web.WebPageResult; +import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect; +import ai.chat2db.server.web.api.controller.rdb.converter.RdbWebConverter; +import ai.chat2db.server.web.api.controller.rdb.request.DdlExportRequest; +import ai.chat2db.server.web.api.controller.rdb.request.TableBriefQueryRequest; +import ai.chat2db.server.web.api.controller.rdb.request.TableCreateDdlQueryRequest; +import ai.chat2db.server.web.api.controller.rdb.request.TableDeleteRequest; +import ai.chat2db.server.web.api.controller.rdb.request.TableDetailQueryRequest; +import ai.chat2db.server.web.api.controller.rdb.request.TableModifySqlRequest; +import ai.chat2db.server.web.api.controller.rdb.request.TableUpdateDdlQueryRequest; +import ai.chat2db.server.web.api.controller.rdb.vo.ColumnVO; +import ai.chat2db.server.web.api.controller.rdb.vo.IndexVO; +import ai.chat2db.server.web.api.controller.rdb.vo.SqlVO; +import ai.chat2db.server.web.api.controller.rdb.vo.TableVO; +import ai.chat2db.spi.model.Table; +import ai.chat2db.spi.model.TableColumn; +import ai.chat2db.spi.model.TableIndex; +import com.google.common.collect.Lists; +import jakarta.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@ConnectionInfoAspect +@RequestMapping("/api/rdb/table") +@RestController +public class TableController { + + @Autowired + private TableService tableService; + + @Autowired + private DlTemplateService dlTemplateService; + + @Autowired + private RdbWebConverter rdbWebConverter; + + @Autowired + private DatabaseService databaseService; + + + /** + * 查询当前DB下的表列表 + * + * @param request + * @return + */ + @GetMapping("/list") + public WebPageResult list(@Valid TableBriefQueryRequest request) { + TablePageQueryParam queryParam = rdbWebConverter.tablePageRequest2param(request); + TableSelector tableSelector = new TableSelector(); + tableSelector.setColumnList(false); + tableSelector.setIndexList(false); + PageResult
tableDTOPageResult = tableService.pageQuery(queryParam, tableSelector); + List tableVOS = rdbWebConverter.tableDto2vo(tableDTOPageResult.getData()); + return WebPageResult.of(tableVOS, tableDTOPageResult.getTotal(), request.getPageNo(), + request.getPageSize()); + } + + + /** + * 查询当前DB下的表columns + * d + * + * @param request + * @return + */ + @GetMapping("/column_list") + public ListResult columnList(@Valid TableDetailQueryRequest request) { + TableQueryParam queryParam = rdbWebConverter.tableRequest2param(request); + List tableColumns = tableService.queryColumns(queryParam); + List tableVOS = rdbWebConverter.columnDto2vo(tableColumns); + return ListResult.of(tableVOS); + } + + /** + * 查询当前DB下的表index + * + * @param request + * @return + */ + @GetMapping("/index_list") + public ListResult indexList(@Valid TableDetailQueryRequest request) { + TableQueryParam queryParam = rdbWebConverter.tableRequest2param(request); + List tableIndices = tableService.queryIndexes(queryParam); + List indexVOS = rdbWebConverter.indexDto2vo(tableIndices); + return ListResult.of(indexVOS); + } + + /** + * 查询当前DB下的表key + * + * @param request + * @return + */ + @GetMapping("/key_list") + public ListResult keyList(@Valid TableDetailQueryRequest request) { + // TODO 增加查询key实现 + return ListResult.of(Lists.newArrayList()); + } + + /** + * 导出建表语句 + * + * @param request + * @return + */ + @GetMapping("/export") + public DataResult export(@Valid DdlExportRequest request) { + ShowCreateTableParam param = rdbWebConverter.ddlExport2showCreate(request); + return tableService.showCreateTable(param); + } + + /** + * 建表语句样例 + * + * @param request + * @return + */ + @GetMapping("/create/example") + public DataResult createExample(@Valid TableCreateDdlQueryRequest request) { + return tableService.createTableExample(request.getDbType()); + } + + /** + * 更新表语句样例 + * + * @param request + * @return + */ + @GetMapping("/update/example") + public DataResult updateExample(@Valid TableUpdateDdlQueryRequest request) { + return tableService.alterTableExample(request.getDbType()); + } + + /** + * 获取表下列和索引等信息 + * + * @param request + * @return + */ + @GetMapping("/query") + public DataResult query(@Valid TableDetailQueryRequest request) { + TableQueryParam queryParam = rdbWebConverter.tableRequest2param(request); + TableSelector tableSelector = new TableSelector(); + tableSelector.setColumnList(true); + tableSelector.setIndexList(true); + DataResult
tableDTODataResult = tableService.query(queryParam, tableSelector); + TableVO tableVO = rdbWebConverter.tableDto2vo(tableDTODataResult.getData()); + return DataResult.of(tableVO); + } + + /** + * 获取修改表的sql语句 + * + * @param request + * @return + */ + @GetMapping("/modify/sql") + public ListResult modifySql(@Valid TableModifySqlRequest request) { + return tableService.buildSql( + rdbWebConverter.tableRequest2param(request.getOldTable()), + rdbWebConverter.tableRequest2param(request.getNewTable())) + .map(rdbWebConverter::dto2vo); + } + + /** + * 删除表 + * + * @param request + * @return + */ + @PostMapping("/delete") + public ActionResult delete(@Valid @RequestBody TableDeleteRequest request) { + DropParam dropParam = rdbWebConverter.tableDelete2dropParam(request); + return tableService.drop(dropParam); + } +} diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/TriggerController.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/TriggerController.java new file mode 100644 index 000000000..da7bcb979 --- /dev/null +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/TriggerController.java @@ -0,0 +1,27 @@ +package ai.chat2db.server.web.api.controller.rdb; + +import ai.chat2db.server.domain.api.service.TriggerService; +import ai.chat2db.server.tools.base.wrapper.result.ListResult; +import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect; +import ai.chat2db.server.web.api.controller.rdb.request.TableBriefQueryRequest; +import ai.chat2db.spi.model.Trigger; +import jakarta.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@ConnectionInfoAspect +@RequestMapping("/api/rdb/trigger") +@RestController +public class TriggerController { + + @Autowired + private TriggerService triggerService; + + + @GetMapping("/list") + public ListResult list(@Valid TableBriefQueryRequest request) { + return triggerService.triggers(request.getDatabaseName(), request.getSchemaName()); + } +} diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/ViewController.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/ViewController.java new file mode 100644 index 000000000..f2daa2ab3 --- /dev/null +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/ViewController.java @@ -0,0 +1,35 @@ +package ai.chat2db.server.web.api.controller.rdb; + +import java.util.List; + +import ai.chat2db.server.domain.api.service.ViewService; +import ai.chat2db.server.tools.base.wrapper.result.ListResult; +import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect; +import ai.chat2db.server.web.api.controller.rdb.converter.RdbWebConverter; +import ai.chat2db.server.web.api.controller.rdb.request.TableBriefQueryRequest; +import ai.chat2db.server.web.api.controller.rdb.vo.TableVO; +import ai.chat2db.spi.model.Table; +import jakarta.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@ConnectionInfoAspect +@RequestMapping("/api/rdb/view") +@RestController +public class ViewController { + @Autowired + private ViewService viewService; + + + @Autowired + private RdbWebConverter rdbWebConverter; + + @GetMapping("/list") + public ListResult list(@Valid TableBriefQueryRequest request) { + ListResult
tableDTOPageResult = viewService.views(request.getDatabaseName(), request.getSchemaName()); + List tableVOS = rdbWebConverter.tableDto2vo(tableDTOPageResult.getData()); + return ListResult.of(tableVOS); + } +} 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 56c7ed3dd..512a21168 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 @@ -51,7 +51,7 @@ public interface MetaData { * @param databaseName * @return */ - List views(@NotEmpty String databaseName, String schemaName); + List
views(@NotEmpty String databaseName, String schemaName); /** * Querying all functions under a schema. 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 2629e23df..cb7729d83 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 @@ -56,7 +56,7 @@ public List
tables(String databaseName, String schemaName, String tableNa } @Override - public List views(String databaseName, String schemaName) { + public List
views(String databaseName, String schemaName) { return SQLExecutor.getInstance().tables(databaseName, schemaName, null, new String[]{"VIEW"}); }