diff --git a/chat2db-client/src/components/CreateDatabase/index.tsx b/chat2db-client/src/components/CreateDatabase/index.tsx index 665d5b57f..230832c09 100644 --- a/chat2db-client/src/components/CreateDatabase/index.tsx +++ b/chat2db-client/src/components/CreateDatabase/index.tsx @@ -1,13 +1,13 @@ -import React, { useCallback, forwardRef, ForwardedRef, useImperativeHandle, useMemo, useState, useEffect } from 'react'; +import React, {ForwardedRef, forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useState} from 'react'; import styles from './index.less'; import classnames from 'classnames'; -import { Form, Input, Modal } from 'antd'; -import MonacoEditor, { IExportRefFunction } from '@/components/Console/MonacoEditor'; -import { v4 as uuid } from 'uuid'; +import {Form, Input, Modal, Select} from 'antd'; +import MonacoEditor, {IExportRefFunction} from '@/components/Console/MonacoEditor'; +import {v4 as uuid} from 'uuid'; import sqlService from '@/service/sql'; import i18n from '@/i18n'; -import { debounce } from 'lodash'; -import { DatabaseTypeCode } from '@/constants'; +import {debounce} from 'lodash'; +import {DatabaseTypeCode} from '@/constants'; interface IProps { className?: string; @@ -15,6 +15,11 @@ interface IProps { executedCallback?: () => void; } +interface IOption { + label: string; + value: string | number | null; +} + export type CreateType = 'database' | 'schema'; export interface ICreateDatabaseRef { @@ -27,8 +32,14 @@ export interface ICreateDatabase { comment?: string; } +export interface IDatabaseCollationList { + collations: IOption[]; +} + // 创建database不支持注释的数据库 const noCommentDatabase = [DatabaseTypeCode.MYSQL]; +// 支持collation的数据库 +const supportCollation = [DatabaseTypeCode.MYSQL,DatabaseTypeCode.POSTGRESQL,DatabaseTypeCode.SQLITE]; export default forwardRef((props: IProps, ref: ForwardedRef) => { const { className, curWorkspaceParams, executedCallback } = props; @@ -41,6 +52,9 @@ export default forwardRef((props: IProps, ref: ForwardedRef) ); const [confirmLoading, setConfirmLoading] = useState(false); const [createType, setCreateType] = useState('database'); + const [databaseCollationList, setDatabaseCollationList] = useState({ + collations: [], + }); useEffect(() => { if (!open) { @@ -50,6 +64,29 @@ export default forwardRef((props: IProps, ref: ForwardedRef) } }, [open]); + useEffect(() => { + if (curWorkspaceParams.databaseType && databaseCollationList.collations.length === 0) { + getDatabaseCollationList(); + } + }, [curWorkspaceParams]) + + const getDatabaseCollationList = () => { + sqlService + .getDatabaseCollationList(curWorkspaceParams) + .then((res) => { + const collations = + res?.collations?.map((i) => { + return { + label: i.collationName, + value: i.collationName, + }; + }) || []; + setDatabaseCollationList({ + collations, + }); + }); + } + const config = useMemo(() => { return createType === 'database' ? { @@ -127,7 +164,7 @@ export default forwardRef((props: IProps, ref: ForwardedRef) executeUpdateDataSql(sql); }; - return ( + return ( { setOpen(false); @@ -143,6 +180,17 @@ export default forwardRef((props: IProps, ref: ForwardedRef) + {!supportCollation.includes(curWorkspaceParams.databaseType) ? null :( + + diff --git a/chat2db-client/src/i18n/en-us/common.ts b/chat2db-client/src/i18n/en-us/common.ts index 2216f71e9..5c7a0314b 100644 --- a/chat2db-client/src/i18n/en-us/common.ts +++ b/chat2db-client/src/i18n/en-us/common.ts @@ -100,6 +100,7 @@ export default { 'common.Button.addSchema': 'Add schema', 'common.label.comment': 'Comment', 'common.label.name': 'Name', + 'common.label.collation': 'Collation', 'common.title.create': 'Create', 'common.title.executiveLogging': 'Executive logging', 'common.text.executionTime': 'Affected in {1} ms', diff --git a/chat2db-client/src/i18n/zh-cn/common.ts b/chat2db-client/src/i18n/zh-cn/common.ts index 3d59bf3c6..d28ad079f 100644 --- a/chat2db-client/src/i18n/zh-cn/common.ts +++ b/chat2db-client/src/i18n/zh-cn/common.ts @@ -99,6 +99,7 @@ export default { 'common.Button.addSchema': '添加Schema', 'common.label.comment': '备注', 'common.label.name': '名称', + 'common.label.collation': '排序规则', 'common.title.create': '创建', 'common.title.executiveLogging': '执行记录', 'common.text.executionTime': '{1}ms 执行完毕', diff --git a/chat2db-client/src/service/sql.ts b/chat2db-client/src/service/sql.ts index 8754246fa..49bc73fd6 100644 --- a/chat2db-client/src/service/sql.ts +++ b/chat2db-client/src/service/sql.ts @@ -301,6 +301,14 @@ const getCreateSchemaSql = createRequest<{ schemaName?: string; }, {sql:string}>('/api/rdb/schema/create_schema_sql', { method: 'post' }); +const getDatabaseCollationList = createRequest<{ + dataSourceId: number; + databaseName: string; +}, IDatabaseSupportField>( + '/api/rdb/table/table_collation', + { method: 'get' }, +); + export default { getCreateSchemaSql, getCreateDatabaseSql, @@ -339,4 +347,5 @@ export default { // exportResultTable getAllTableList, getAllFieldByTable, + getDatabaseCollationList, }; diff --git a/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/MysqlMetaData.java b/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/MysqlMetaData.java index 40a291955..cd82fb044 100644 --- a/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/MysqlMetaData.java +++ b/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/MysqlMetaData.java @@ -297,4 +297,11 @@ public String getMetaDataName(String... names) { public ValueHandler getValueHandler() { return new MysqlValueHandler(); } + + @Override + public TableMeta getTableCollation(String databaseName, String schemaName, String tableName) { + return TableMeta.builder() + .collations(MysqlCollationEnum.getCollations()) + .build(); + } } diff --git a/chat2db-server/chat2db-plugins/chat2db-postgresql/src/main/java/ai/chat2db/plugin/postgresql/PostgreSQLMetaData.java b/chat2db-server/chat2db-plugins/chat2db-postgresql/src/main/java/ai/chat2db/plugin/postgresql/PostgreSQLMetaData.java index 4d46c647c..06ffab02e 100644 --- a/chat2db-server/chat2db-plugins/chat2db-postgresql/src/main/java/ai/chat2db/plugin/postgresql/PostgreSQLMetaData.java +++ b/chat2db-server/chat2db-plugins/chat2db-postgresql/src/main/java/ai/chat2db/plugin/postgresql/PostgreSQLMetaData.java @@ -302,4 +302,10 @@ public TableMeta getTableMeta(String databaseName, String schemaName, String tab .build(); } + @Override + public TableMeta getTableCollation(String databaseName, String schemaName, String tableName) { + return TableMeta.builder() + .collations(PostgreSQLCollationEnum.getCollations()) + .build(); + } } diff --git a/chat2db-server/chat2db-plugins/chat2db-sqlite/src/main/java/ai/chat2db/plugin/sqlite/SqliteMetaData.java b/chat2db-server/chat2db-plugins/chat2db-sqlite/src/main/java/ai/chat2db/plugin/sqlite/SqliteMetaData.java index f8bc1213d..ec24fd124 100644 --- a/chat2db-server/chat2db-plugins/chat2db-sqlite/src/main/java/ai/chat2db/plugin/sqlite/SqliteMetaData.java +++ b/chat2db-server/chat2db-plugins/chat2db-sqlite/src/main/java/ai/chat2db/plugin/sqlite/SqliteMetaData.java @@ -66,4 +66,11 @@ public TableMeta getTableMeta(String databaseName, String schemaName, String tab public String getMetaDataName(String... names) { return Arrays.stream(names).filter(name -> StringUtils.isNotBlank(name)).map(name -> "\"" + name + "\"").collect(Collectors.joining(".")); } + + @Override + public TableMeta getTableCollation(String databaseName, String schemaName, String tableName) { + return TableMeta.builder() + .collations(SqliteCollationEnum.getCollations()) + .build(); + } } diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/TableService.java b/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/TableService.java index 93f245af2..695d91315 100644 --- a/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/TableService.java +++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/TableService.java @@ -129,4 +129,6 @@ public interface TableService { * @return */ DataResult checkTableVector(TableVectorParam param); + + TableMeta queryTableCollation(TypeQueryParam param); } diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/TableServiceImpl.java b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/TableServiceImpl.java index 5fd24c465..302ef59d6 100644 --- a/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/TableServiceImpl.java +++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/TableServiceImpl.java @@ -577,4 +577,19 @@ public DataResult checkTableVector(TableVectorParam param) { } return DataResult.of(false); } + + @Override + public TableMeta queryTableCollation(TypeQueryParam param) { + MetaData metaSchema = Chat2DBContext.getMetaData(); + TableMeta tableMeta = metaSchema.getTableCollation(null, null, null); + if (tableMeta != null) { + //filter primary key + List indexTypes = tableMeta.getIndexTypes(); + if (CollectionUtils.isNotEmpty(indexTypes)) { + List types = indexTypes.stream().filter(indexType -> !"Primary".equals(indexType.getTypeName())).collect(Collectors.toList()); + tableMeta.setIndexTypes(types); + } + } + return tableMeta; + } } 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 index 131a6bf6c..34eea3232 100644 --- 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 @@ -239,6 +239,13 @@ public DataResult tableMeta(@Valid TypeQueryRequest request) { return DataResult.of(tableMeta); } + @GetMapping("/table_collation") + public DataResult tableCollation(@Valid TypeQueryRequest request) { + TypeQueryParam typeQueryParam = TypeQueryParam.builder().dataSourceId(request.getDataSourceId()).build(); + TableMeta tableMeta = tableService.queryTableCollation(typeQueryParam); + return DataResult.of(tableMeta); + } + /** * 删除表 * 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 02fab4057..8c25d3824 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 @@ -214,4 +214,6 @@ List indexes(Connection connection, @NotEmpty String databaseName, S */ ValueHandler getValueHandler(); + TableMeta getTableCollation(String databaseName, String schemaName, String tableName); + } \ No newline at end of file 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 624814080..c4da5d966 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 @@ -136,4 +136,9 @@ public String getMetaDataName(String... names) { public ValueHandler getValueHandler() { return new DefaultValueHandler(); } + + @Override + public TableMeta getTableCollation(String databaseName, String schemaName, String tableName) { + return null; + } } \ No newline at end of file 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 f3706538f..fce7db266 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 @@ -1,6 +1,7 @@ package ai.chat2db.spi.sql; import ai.chat2db.server.tools.base.constant.EasyToolsConstant; +import ai.chat2db.server.tools.base.enums.DataSourceTypeEnum; import ai.chat2db.server.tools.common.util.I18nUtils; import ai.chat2db.spi.ValueHandler; import ai.chat2db.spi.jdbc.DefaultValueHandler; @@ -203,7 +204,8 @@ public ExecuteResult execute(final String sql, Connection connection, boolean li // 获取header信息 List
headerList = Lists.newArrayListWithExpectedSize(col); executeResult.setHeaderList(headerList); - int chat2dbAutoRowIdIndex = -1;// chat2db自动生成的行分页ID + // chat2db自动生成的行分页ID + int chat2dbAutoRowIdIndex = -1; for (int i = 1; i <= col; i++) { String name = ResultSetUtils.getColumnName(resultSetMetaData, i); @@ -344,7 +346,7 @@ public List tables(Connection connection, String databaseName, String sch ResultSet resultSet = metadata.getTables(databaseName, schemaName, tableName, types); // 如果connection为mysql - if ("MySQL".equalsIgnoreCase(metadata.getDatabaseProductName())) { + if (DataSourceTypeEnum.MYSQL.getCode().equalsIgnoreCase(metadata.getDatabaseProductName())) { // 获取mysql表的comment List
tables = ResultSetUtils.toObjectList(resultSet, Table.class); if (CollectionUtils.isNotEmpty(tables)) {