Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When creating a new database, the sorting order selection function is added. #906

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
62 changes: 55 additions & 7 deletions chat2db-client/src/components/CreateDatabase/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
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;
curWorkspaceParams: any;
executedCallback?: () => void;
}

interface IOption {
label: string;
value: string | number | null;
}

export type CreateType = 'database' | 'schema';

export interface ICreateDatabaseRef {
Expand All @@ -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<ICreateDatabaseRef>) => {
const { className, curWorkspaceParams, executedCallback } = props;
Expand All @@ -41,6 +52,9 @@ export default forwardRef((props: IProps, ref: ForwardedRef<ICreateDatabaseRef>)
);
const [confirmLoading, setConfirmLoading] = useState(false);
const [createType, setCreateType] = useState<CreateType>('database');
const [databaseCollationList, setDatabaseCollationList] = useState<IDatabaseCollationList>({
collations: [],
});

useEffect(() => {
if (!open) {
Expand All @@ -50,6 +64,29 @@ export default forwardRef((props: IProps, ref: ForwardedRef<ICreateDatabaseRef>)
}
}, [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'
? {
Expand Down Expand Up @@ -127,7 +164,7 @@ export default forwardRef((props: IProps, ref: ForwardedRef<ICreateDatabaseRef>)
executeUpdateDataSql(sql);
};

return (
return (
<Modal
onCancel={() => {
setOpen(false);
Expand All @@ -143,6 +180,17 @@ export default forwardRef((props: IProps, ref: ForwardedRef<ICreateDatabaseRef>)
<Form.Item label={i18n('common.label.name')} name={config.formName}>
<Input autoComplete="off" />
</Form.Item>
{!supportCollation.includes(curWorkspaceParams.databaseType) ? null :(
<Form.Item label={i18n('common.label.collation')} name="collation">
<Select
bordered={false}
placeholder="请选择排序顺序"
showSearch
popupMatchSelectWidth={false}
options={databaseCollationList.collations}
/>
</Form.Item>
)}
{noCommentDatabase.includes(curWorkspaceParams.databaseType) ? null : (
<Form.Item label={i18n('common.label.comment')} name="comment">
<Input autoComplete="off" />
Expand Down
1 change: 1 addition & 0 deletions chat2db-client/src/i18n/en-us/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions chat2db-client/src/i18n/zh-cn/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 执行完毕',
Expand Down
9 changes: 9 additions & 0 deletions chat2db-client/src/service/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -339,4 +347,5 @@ export default {
// exportResultTable
getAllTableList,
getAllFieldByTable,
getDatabaseCollationList,
};
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,6 @@ public interface TableService {
* @return
*/
DataResult<Boolean> checkTableVector(TableVectorParam param);

TableMeta queryTableCollation(TypeQueryParam param);
}
Original file line number Diff line number Diff line change
Expand Up @@ -577,4 +577,19 @@ public DataResult<Boolean> 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<IndexType> indexTypes = tableMeta.getIndexTypes();
if (CollectionUtils.isNotEmpty(indexTypes)) {
List<IndexType> types = indexTypes.stream().filter(indexType -> !"Primary".equals(indexType.getTypeName())).collect(Collectors.toList());
tableMeta.setIndexTypes(types);
}
}
return tableMeta;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ public DataResult<TableMeta> tableMeta(@Valid TypeQueryRequest request) {
return DataResult.of(tableMeta);
}

@GetMapping("/table_collation")
public DataResult<TableMeta> tableCollation(@Valid TypeQueryRequest request) {
TypeQueryParam typeQueryParam = TypeQueryParam.builder().dataSourceId(request.getDataSourceId()).build();
TableMeta tableMeta = tableService.queryTableCollation(typeQueryParam);
return DataResult.of(tableMeta);
}

/**
* 删除表
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,6 @@ List<TableIndex> indexes(Connection connection, @NotEmpty String databaseName, S
*/
ValueHandler getValueHandler();

TableMeta getTableCollation(String databaseName, String schemaName, String tableName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -203,7 +204,8 @@ public ExecuteResult execute(final String sql, Connection connection, boolean li
// 获取header信息
List<Header> 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);
Expand Down Expand Up @@ -344,7 +346,7 @@ public List<Table> 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<Table> tables = ResultSetUtils.toObjectList(resultSet, Table.class);
if (CollectionUtils.isNotEmpty(tables)) {
Expand Down