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

You can select the character set field when creating a new database. #917

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
52 changes: 51 additions & 1 deletion chat2db-client/src/components/CreateDatabase/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useCallback, forwardRef, ForwardedRef, useImperativeHandle, useMemo, useState, useEffect } from 'react';
import styles from './index.less';
import classnames from 'classnames';
import { Form, Input, Modal } from 'antd';
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';
Expand All @@ -15,6 +15,11 @@ interface IProps {
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 IDatabaseCharsetList {
charsets: IOption[];
}

// 创建database不支持注释的数据库
const noCommentDatabase = [DatabaseTypeCode.MYSQL];
// 支持charset的数据库
const supportCharset = [DatabaseTypeCode.MYSQL];

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 [databaseCharsetList, setDatabaseCharsetList] = useState<IDatabaseCharsetList>({
charsets: [],
});

useEffect(() => {
if (!open) {
Expand All @@ -50,6 +64,33 @@ export default forwardRef((props: IProps, ref: ForwardedRef<ICreateDatabaseRef>)
}
}, [open]);

useEffect(() => {
if (curWorkspaceParams.databaseType && databaseCharsetList.charsets.length === 0) {
getDatabaseCharsetList();
}
}, [curWorkspaceParams])

const initialOption = {
label: '',
value: '',
};
const getDatabaseCharsetList = () => {
sqlService
.getDatabaseCharsetList(curWorkspaceParams)
.then((res) => {
const charsets = [initialOption,
...(res?.charsets?.map((i) => {
return {
label: i.charsetName,
value: i.charsetName,
};
}) || [])];
setDatabaseCharsetList({
charsets,
});
});
}

const config = useMemo(() => {
return createType === 'database'
? {
Expand Down Expand Up @@ -143,6 +184,15 @@ export default forwardRef((props: IProps, ref: ForwardedRef<ICreateDatabaseRef>)
<Form.Item label={i18n('common.label.name')} name={config.formName}>
<Input autoComplete="off" />
</Form.Item>
<Form.Item label={i18n('common.label.charset')} name="charset">
<Select
bordered={false}
placeholder="请选择字符集"
showSearch
popupMatchSelectWidth={false}
options={databaseCharsetList.charsets}
/>
</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.charset': 'Charset',
'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.charset': '字符集',
'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 getDatabaseCharsetList = createRequest<{
dataSourceId: number;
databaseName: string;
}, IDatabaseSupportField>(
'/api/rdb/table/table_charset',
{ method: 'get' },
);

export default {
getCreateSchemaSql,
getCreateDatabaseSql,
Expand Down Expand Up @@ -339,4 +347,5 @@ export default {
// exportResultTable
getAllTableList,
getAllFieldByTable,
getDatabaseCharsetList,
};
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 getTableCharset(String databaseName, String schemaName, String tableName) {
return TableMeta.builder()
.charsets(MysqlCharsetEnum.getCharsets())
.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 queryTableCharset(TypeQueryParam param);
}
Original file line number Diff line number Diff line change
Expand Up @@ -577,4 +577,20 @@ public DataResult<Boolean> checkTableVector(TableVectorParam param) {
}
return DataResult.of(false);
}

@Override
public TableMeta queryTableCharset(TypeQueryParam param) {
MetaData metaSchema = Chat2DBContext.getMetaData();
TableMeta tableMeta = metaSchema.getTableCharset(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 @@ -250,4 +250,17 @@ public ActionResult delete(@Valid @RequestBody TableDeleteRequest request) {
DropParam dropParam = rdbWebConverter.tableDelete2dropParam(request);
return tableService.drop(dropParam);
}

/**
* 数据库支持的字符集
* @param request
* @return
*/
@GetMapping("/table_charset")
public DataResult<TableMeta> tableCharset(@Valid TypeQueryRequest request) {
TypeQueryParam typeQueryParam = TypeQueryParam.builder().dataSourceId(request.getDataSourceId()).build();
TableMeta tableMeta = tableService.queryTableCharset(typeQueryParam);
return DataResult.of(tableMeta);
}

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

TableMeta getTableCharset(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 getTableCharset(String databaseName, String schemaName, String tableName) {
return null;
}
}