Skip to content

Commit 5a80da6

Browse files
committed
edit table
1 parent 74606a8 commit 5a80da6

6 files changed

Lines changed: 91 additions & 58 deletions

File tree

chat2db-client/src/blocks/DatabaseTableEditor/ColumnList/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,14 @@ const ColumnList = forwardRef((props: IProps, ref: ForwardedRef<IColumnListRef>)
387387

388388
function getColumnListInfo(): IColumnItemNew[] {
389389
return dataSource.map((i) => {
390-
return {
390+
const data = {
391391
...i,
392392
tableName: tableDetails?.name,
393393
databaseName,
394394
schemaName: schemaName || null,
395395
};
396+
delete data.key;
397+
return data;
396398
});
397399
}
398400

chat2db-client/src/blocks/DatabaseTableEditor/IncludeCol/index.tsx

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { v4 as uuidv4 } from 'uuid';
66
import { Context } from '../index';
77
import { IColumnItemNew, IIndexIncludeColumnItem } from '@/typings';
88
import i18n from '@/i18n';
9+
import { string } from 'sql-formatter/lib/src/lexer/regexFactory';
910

1011
interface IProps {
1112
includedColumnList: IIndexIncludeColumnItem[];
@@ -14,29 +15,7 @@ interface IProps {
1415
const createInitialData = () => {
1516
return {
1617
key: uuidv4(),
17-
oldName: null,
1818
name: null,
19-
tableName: null,
20-
columnType: null,
21-
dataType: null,
22-
defaultValue: null,
23-
autoIncrement: null,
24-
comment: null,
25-
primaryKey: null,
26-
schemaName: null,
27-
databaseName: null,
28-
typeName: null,
29-
columnSize: null,
30-
bufferLength: null,
31-
decimalDigits: null,
32-
numPrecRadix: null,
33-
nullableInt: null,
34-
sqlDataType: null,
35-
sqlDatetimeSub: null,
36-
charOctetLength: null,
37-
ordinalPosition: null,
38-
nullable: null,
39-
generatedColumn: null,
4019
};
4120
};
4221

@@ -49,46 +28,42 @@ const InitialDataSource = [createInitialData()];
4928
const IncludeCol = forwardRef((props: IProps, ref: ForwardedRef<IIncludeColRef>) => {
5029
const { includedColumnList } = props;
5130
const { columnListRef } = useContext(Context);
52-
const [dataSource, setDataSource] = useState<IIndexIncludeColumnItem[]>(InitialDataSource);
31+
const [dataSource, setDataSource] = useState<any[]>(InitialDataSource);
5332
const [form] = Form.useForm();
54-
const [editingKey, setEditingKey] = useState(dataSource[0]?.key);
33+
const [editingKey, setEditingKey] = useState<string | null>(null);
5534
const isEditing = (record: IIndexIncludeColumnItem) => record.key === editingKey;
5635

5736
useEffect(() => {
5837
if (includedColumnList.length) {
5938
setDataSource(
6039
includedColumnList.map((t) => {
6140
return {
62-
...t,
6341
key: uuidv4(),
42+
name: t.name,
6443
};
6544
}),
6645
);
6746
}
6847
}, [includedColumnList]);
6948

7049
const columnList: IColumnItemNew[] = useMemo(() => {
71-
const columnListInfo = columnListRef.current?.getColumnListInfo()?.filter((i) => i.name);
50+
const columnListInfo = columnListRef.current?.getColumnListInfo()?.filter((i) => i.name !== null);
7251
return columnListInfo || [];
7352
}, []);
7453

75-
const edit = (record: IIndexIncludeColumnItem) => {
54+
const edit = (record: any) => {
7655
form.setFieldsValue({ ...record });
77-
setEditingKey(record.key);
56+
setEditingKey(record.key || null);
7857
};
7958

8059
const addData = () => {
8160
const newData = createInitialData();
8261
setDataSource([...dataSource, newData]);
62+
console.log([...dataSource, newData]);
8363
edit(newData);
8464
};
8565

8666
const deleteData = () => {
87-
// if (dataSource.length === 1) {
88-
// message.warning('至少保留一条数据')
89-
// return
90-
// }
91-
9267
setDataSource(dataSource.filter((i) => i.key !== editingKey));
9368
};
9469

@@ -103,12 +78,12 @@ const IncludeCol = forwardRef((props: IProps, ref: ForwardedRef<IIncludeColRef>)
10378
},
10479
{
10580
title: i18n('editTable.label.columnName'),
106-
dataIndex: 'columnName',
81+
dataIndex: 'name',
10782
// width: '45%',
10883
render: (text: string, record: IIndexIncludeColumnItem) => {
10984
const editable = isEditing(record);
11085
return editable ? (
111-
<Form.Item name="columnName" style={{ margin: 0 }}>
86+
<Form.Item name="name" style={{ margin: 0 }}>
11287
<Select options={columnList.map((i) => ({ label: i.name, value: i.name }))} />
11388
</Form.Item>
11489
) : (
@@ -137,24 +112,28 @@ const IncludeCol = forwardRef((props: IProps, ref: ForwardedRef<IIncludeColRef>)
137112
// },
138113
];
139114

140-
const onValuesChange = (changedValues: any, allValues: any) => {
141-
const newDataSource = dataSource?.map((i) => {
142-
if (i.key === editingKey) {
115+
const handelFieldsChange = (field: any) => {
116+
const { value } = field[0];
117+
const { name: nameList } = field[0];
118+
const name = nameList[0];
119+
const newData = dataSource.map((item) => {
120+
if (item.key === editingKey) {
143121
return {
144-
...i,
145-
...allValues,
122+
...item,
123+
[name]: value,
146124
};
147125
}
148-
return i;
126+
return item;
149127
});
150-
setDataSource(newDataSource);
128+
setDataSource(newData);
151129
};
152130

153131
const getIncludeColInfo = () => {
154132
const includeColInfo: IIndexIncludeColumnItem[] = [];
155133
dataSource.forEach((t) => {
156134
columnList.forEach((columnItem) => {
157135
if (t.name === columnItem.name) {
136+
delete columnItem.key;
158137
includeColInfo.push({
159138
...columnItem,
160139
});
@@ -174,7 +153,7 @@ const IncludeCol = forwardRef((props: IProps, ref: ForwardedRef<IIncludeColRef>)
174153
<Button onClick={addData}>{i18n('editTable.button.add')}</Button>
175154
<Button onClick={deleteData}>{i18n('editTable.button.delete')}</Button>
176155
</div>
177-
<Form form={form} onValuesChange={onValuesChange}>
156+
<Form form={form} onFieldsChange={handelFieldsChange}>
178157
<Table pagination={false} rowKey="key" columns={columns} dataSource={dataSource} />
179158
</Form>
180159
</div>

chat2db-client/src/blocks/DatabaseTableEditor/IndexList/index.tsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { IndexesType, EditColumnOperationType } from '@/constants';
2323
import { Context } from '../index';
2424
import i18n from '@/i18n';
2525

26-
const indexesTypeList = [IndexesType['Normal'], IndexesType['Unique'], IndexesType['Fulltext'], IndexesType['Spatial']];
26+
const indexesTypeList = Object.values(IndexesType);
2727

2828
interface IProps {}
2929

@@ -165,7 +165,10 @@ const IndexList = forwardRef((props: IProps, ref: ForwardedRef<IIndexListRef>) =
165165
};
166166

167167
function getIndexListInfo(): IIndexListInfo {
168-
return dataSource;
168+
return dataSource.map((i) => {
169+
delete i.key;
170+
return i;
171+
});
169172
}
170173

171174
useImperativeHandle(ref, () => ({
@@ -254,29 +257,47 @@ const IndexList = forwardRef((props: IProps, ref: ForwardedRef<IIndexListRef>) =
254257
);
255258
},
256259
},
260+
// {
261+
// title: i18n('editTable.label.comment'),
262+
// dataIndex: 'comment',
263+
// render: (text: string, record: IIndexItem) => {
264+
// const editable = isEditing(record);
265+
// return editable ? (
266+
// <Form.Item name="comment" style={{ margin: 0 }}>
267+
// <Input />
268+
// </Form.Item>
269+
// ) : (
270+
// <div className={styles.editableCell} onClick={() => edit(record)}>
271+
// {text}
272+
// </div>
273+
// );
274+
// },
275+
// },
257276
];
258277

259278
const getIncludeColInfo = () => {
260279
setDataSource(
261280
dataSource.map((i) => {
262281
const columnList = includeColRef.current?.getIncludeColInfo();
282+
console.log(columnList);
263283
if (i.key === editingKey && columnList) {
264284
i.columnList = columnList;
265285
}
266286
return i;
267287
}),
268288
);
289+
269290
setIncludeColModalOpen(false);
270291
};
271292

272293
const indexIncludedColumnList: IIndexIncludeColumnItem[] = useMemo(() => {
273-
let data: IIndexIncludeColumnItem[] | null = [];
294+
let list: IIndexIncludeColumnItem[] = [];
274295
dataSource.forEach((i) => {
275296
if (i.key === editingKey) {
276-
data = i.columnList;
297+
list = i.columnList || [];
277298
}
278299
});
279-
return data;
300+
return list;
280301
}, [editingKey]);
281302

282303
return (

chat2db-client/src/blocks/DatabaseTableEditor/index.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import BaseInfo, { IBaseInfoRef } from './BaseInfo';
88
import sqlService, { IModifyTableSqlParams } from '@/service/sql';
99
import { IEditTableInfo } from '@/typings';
1010
import i18n from '@/i18n';
11+
import lodash from 'lodash';
1112

1213
interface IProps {
1314
dataSourceId: number;
@@ -73,7 +74,20 @@ export default memo((props: IProps) => {
7374
refresh: true,
7475
};
7576
sqlService.getTableDetails(params).then((res) => {
76-
setTableDetails(res || {});
77+
const newTableDetails = lodash.cloneDeep(res);
78+
newTableDetails.indexList.forEach((i) => {
79+
i.columnList = i.columnList.map((j: any) => {
80+
let newColumn: any = {};
81+
newTableDetails.columnList.forEach((k: any) => {
82+
if (j.columnName === k.name) {
83+
newColumn = k;
84+
}
85+
});
86+
return newColumn;
87+
});
88+
});
89+
console.log(newTableDetails);
90+
setTableDetails(newTableDetails || {});
7791
setOldTableDetails(res);
7892
});
7993
}

chat2db-client/src/constants/editTable.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
// 索引类型
22
export enum IndexesType {
3-
// 普通索引
4-
Normal = 'normal',
5-
// 唯一索引
6-
Unique = 'unique',
7-
// 全文索引
8-
Fulltext = 'fulltext',
9-
// 空间索引
10-
Spatial = 'spatial',
3+
NormPRIMARY_KEYal = 'PRIMARY_KEY',
4+
NORMAL = 'NORMAL',
5+
UNIQUE = 'UNIQUE',
6+
FULLTEXT = 'FULLTEXT',
7+
SPATIAL = 'SPATIAL',
118
}
129

1310
export enum EditColumnOperationType {

chat2db-client/src/typings/editTable.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,30 @@ export interface IColumnItemNew {
3636
generatedColumn: string | null; // 是否生成列
3737
}
3838

39+
//
3940
export interface IIndexIncludeColumnItem extends IColumnItemNew {
41+
}
4042

43+
// 后端给的索引内列的数据结构
44+
export interface IAfterEndIndexIncludeColumnItem {
45+
ascOrDesc: string | null; // 升序还是降序
46+
cardinality: number | null; // 基数
47+
collation: string | null; // 排序规则
48+
columnName: string | null; // 列名
49+
comment: string | null; // 注释
50+
databaseName: string | null; // 数据库名
51+
filterCondition: string | null; // 过滤条件
52+
indexName: string | null; // 索引名
53+
indexQualifier: string | null; // 索引限定符
54+
nonUnique: boolean | null; // 是否唯一
55+
ordinalPosition: number | null; // 位置
56+
schemaName: string | null; // 模式名
57+
tableName: string | null; // 表名
58+
type: string | null; // 类型
59+
pages: number | null; // 页数
4160
}
4261

62+
4363
// 编辑表时索引的数据结构
4464
export interface IIndexItem {
4565
key?: string;

0 commit comments

Comments
 (0)