diff --git a/chat2db-client/src/blocks/DatabaseTableEditor/ColumnList/index.tsx b/chat2db-client/src/blocks/DatabaseTableEditor/ColumnList/index.tsx index ad6b6718f..89b357b5e 100644 --- a/chat2db-client/src/blocks/DatabaseTableEditor/ColumnList/index.tsx +++ b/chat2db-client/src/blocks/DatabaseTableEditor/ColumnList/index.tsx @@ -1,17 +1,17 @@ -import React, { useContext, useEffect, useState, useRef, forwardRef, ForwardedRef, useImperativeHandle } from 'react'; +import React, {ForwardedRef, forwardRef, useContext, useEffect, useImperativeHandle, useRef, useState} from 'react'; import styles from './index.less'; import classnames from 'classnames'; -import { MenuOutlined } from '@ant-design/icons'; -import { DndContext, type DragEndEvent } from '@dnd-kit/core'; -import { restrictToVerticalAxis } from '@dnd-kit/modifiers'; -import { Table, InputNumber, Input, Form, Select, Checkbox } from 'antd'; -import { v4 as uuidv4 } from 'uuid'; -import { arrayMove, SortableContext, useSortable, verticalListSortingStrategy } from '@dnd-kit/sortable'; -import { CSS } from '@dnd-kit/utilities'; -import { Context } from '../index'; -import { IColumnItemNew, IColumnTypes } from '@/typings'; +import {MenuOutlined} from '@ant-design/icons'; +import {DndContext, type DragEndEvent} from '@dnd-kit/core'; +import {restrictToVerticalAxis} from '@dnd-kit/modifiers'; +import {Checkbox, Form, Input, InputNumber, Select, Table} from 'antd'; +import {v4 as uuidv4} from 'uuid'; +import {arrayMove, SortableContext, useSortable, verticalListSortingStrategy} from '@dnd-kit/sortable'; +import {CSS} from '@dnd-kit/utilities'; +import {Context} from '../index'; +import {IColumnItemNew, IColumnTypes} from '@/typings'; import i18n from '@/i18n'; -import { EditColumnOperationType, DatabaseTypeCode, NullableType } from '@/constants'; +import {DatabaseTypeCode, EditColumnOperationType, NullableType} from '@/constants'; import CustomSelect from '@/components/CustomSelect'; import Iconfont from '@/components/Iconfont'; @@ -30,7 +30,7 @@ interface IEditingConfig extends IColumnTypes { export interface IColumnListRef { getColumnListInfo: () => IColumnItemNew[]; } - +const onUpdateTypeName = ['TIMESTAMP', 'DATETIME']; const Row = ({ children, ...props }: RowProps) => { const { attributes, listeners, setNodeRef, setActivatorNodeRef, transform, transition, isDragging } = useSortable({ id: props['data-row-key'], @@ -91,6 +91,7 @@ const createInitialData = () => { collationName: null, value: null, editStatus: EditColumnOperationType.Add, + onUpdate: false, }; }; @@ -506,6 +507,14 @@ const ColumnList = forwardRef((props: IProps, ref: ForwardedRef) )} + {editingConfig?.supportDefaultValue && databaseType === DatabaseTypeCode.MYSQL + && onUpdateTypeName.includes(editingConfig?.typeName) && ( + + + {i18n('editTable.label.onUpdate')} + + + )} {editingConfig?.supportCharset && ( diff --git a/chat2db-client/src/i18n/en-us/editTable.ts b/chat2db-client/src/i18n/en-us/editTable.ts index 04cc2404d..09cb0aad9 100644 --- a/chat2db-client/src/i18n/en-us/editTable.ts +++ b/chat2db-client/src/i18n/en-us/editTable.ts @@ -21,6 +21,7 @@ export default { 'editTable.label.nullable': 'Nullable', 'editTable.label.prefixLength': 'Prefix length', 'editTable.label.defaultValue': 'Default value', + 'editTable.label.onUpdate': 'Update based on current timestamp', 'editTable.label.sparse': 'Sparse', 'editTable.label.characterSet': 'Character set', 'editTable.label.collation': 'Collation', diff --git a/chat2db-client/src/i18n/ja-jp/editTable.ts b/chat2db-client/src/i18n/ja-jp/editTable.ts index f51c09fa2..fca4c6aa4 100644 --- a/chat2db-client/src/i18n/ja-jp/editTable.ts +++ b/chat2db-client/src/i18n/ja-jp/editTable.ts @@ -21,6 +21,7 @@ export default { 'editTable.label.nullable': 'NULL可能', 'editTable.label.prefixLength': '接頭辞の長さ', 'editTable.label.defaultValue': 'デフォルト値', + 'editTable.label.onUpdate': '現在のタイムスタンプに基づいて更新します', 'editTable.label.sparse': 'スパース', 'editTable.label.characterSet': '文字セット', 'editTable.label.collation': '照合順序', diff --git a/chat2db-client/src/i18n/tr-tr/editTable.ts b/chat2db-client/src/i18n/tr-tr/editTable.ts index 597b0c0e5..89a2dfc45 100644 --- a/chat2db-client/src/i18n/tr-tr/editTable.ts +++ b/chat2db-client/src/i18n/tr-tr/editTable.ts @@ -21,6 +21,7 @@ export default { 'editTable.label.nullable': 'Boş bırakılabilir', 'editTable.label.prefixLength': 'Önek uzunluğu', 'editTable.label.defaultValue': 'Varsayılan değer', + 'editTable.label.onUpdate': 'Geçerli zaman damgasına göre güncelleme', 'editTable.label.sparse': 'Düzensiz', 'editTable.label.characterSet': 'Karakter kümesi', 'editTable.label.collation': 'Düzenleme', diff --git a/chat2db-client/src/i18n/zh-cn/editTable.ts b/chat2db-client/src/i18n/zh-cn/editTable.ts index 711abe141..7c5941f20 100644 --- a/chat2db-client/src/i18n/zh-cn/editTable.ts +++ b/chat2db-client/src/i18n/zh-cn/editTable.ts @@ -21,6 +21,7 @@ export default { 'editTable.label.nullable': '可空', 'editTable.label.prefixLength': '前缀长度', 'editTable.label.defaultValue': '默认值', + 'editTable.label.onUpdate': '根据当前时间戳更新', 'editTable.label.sparse': '稀疏', 'editTable.label.characterSet': '字符集', 'editTable.label.collation': '排序规则', diff --git a/chat2db-client/src/typings/editTable.ts b/chat2db-client/src/typings/editTable.ts index a00a6987c..a52ea3df6 100644 --- a/chat2db-client/src/typings/editTable.ts +++ b/chat2db-client/src/typings/editTable.ts @@ -42,6 +42,7 @@ export interface IColumnItemNew { charSetName: string | null; // 字符集名 collationName: string | null; // 排序规则名 value: string | null; // 值 + onUpdate: boolean | null; // 是否根据当前时间戳更新 } // 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..1085a7910 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 @@ -156,6 +156,9 @@ public List columns(Connection connection, String databaseName, Str column.setDecimalDigits(resultSet.getInt("NUMERIC_SCALE")); column.setCharSetName(resultSet.getString("CHARACTER_SET_NAME")); column.setCollationName(resultSet.getString("COLLATION_NAME")); + if (resultSet.getString("extra").toLowerCase().contains("on update CURRENT_TIMESTAMP".toLowerCase())) { + column.setOnUpdate(true); + } setColumnSize(column, resultSet.getString("COLUMN_TYPE")); tableColumns.add(column); } diff --git a/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlColumnTypeEnum.java b/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlColumnTypeEnum.java index c6ed4175b..284e9171a 100644 --- a/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlColumnTypeEnum.java +++ b/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlColumnTypeEnum.java @@ -157,6 +157,8 @@ public String buildCreateColumnSql(TableColumn column) { script.append(buildDefaultValue(column,type)).append(" "); + script.append(buildOnUpdate(column,type)).append(" "); + script.append(buildExt(column,type)).append(" "); script.append(buildAutoIncrement(column,type)).append(" "); @@ -254,6 +256,14 @@ private String buildDefaultValue(TableColumn column, MysqlColumnTypeEnum type) { return StringUtils.join("DEFAULT ",column.getDefaultValue()); } + private String buildOnUpdate(TableColumn column, MysqlColumnTypeEnum type) { + if (column.getOnUpdate() != null && column.getOnUpdate()) { + return StringUtils.join("ON UPDATE CURRENT_TIMESTAMP"); + + } + return ""; + } + private String buildNullable(TableColumn column,MysqlColumnTypeEnum type) { if(!type.getColumnType().isSupportNullable()){ return ""; diff --git a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/TableColumn.java b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/TableColumn.java index 215f17344..adcdcba8d 100644 --- a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/TableColumn.java +++ b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/TableColumn.java @@ -199,16 +199,22 @@ public class TableColumn { // sqlserver private String defaultConstraintName; + /** + * 根据当前时间戳更新 + * mysql + */ + private Boolean onUpdate; + @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TableColumn that = (TableColumn) o; - return Objects.equals(name, that.name) && Objects.equals(tableName, that.tableName) && Objects.equals(columnType, that.columnType) && Objects.equals(defaultValue, that.defaultValue) && Objects.equals(autoIncrement, that.autoIncrement) && Objects.equals(comment, that.comment) && Objects.equals(columnSize, that.columnSize) && Objects.equals(decimalDigits, that.decimalDigits) && Objects.equals(numPrecRadix, that.numPrecRadix) && Objects.equals(sqlDataType, that.sqlDataType) && Objects.equals(ordinalPosition, that.ordinalPosition) && Objects.equals(nullable, that.nullable) && Objects.equals(extent, that.extent) && Objects.equals(charSetName, that.charSetName) && Objects.equals(collationName, that.collationName) && Objects.equals(value, that.value) && Objects.equals(unit, that.unit) && Objects.equals(sparse, that.sparse) && Objects.equals(defaultConstraintName, that.defaultConstraintName); + return Objects.equals(name, that.name) && Objects.equals(tableName, that.tableName) && Objects.equals(columnType, that.columnType) && Objects.equals(defaultValue, that.defaultValue) && Objects.equals(autoIncrement, that.autoIncrement) && Objects.equals(comment, that.comment) && Objects.equals(columnSize, that.columnSize) && Objects.equals(decimalDigits, that.decimalDigits) && Objects.equals(numPrecRadix, that.numPrecRadix) && Objects.equals(sqlDataType, that.sqlDataType) && Objects.equals(ordinalPosition, that.ordinalPosition) && Objects.equals(nullable, that.nullable) && Objects.equals(extent, that.extent) && Objects.equals(charSetName, that.charSetName) && Objects.equals(collationName, that.collationName) && Objects.equals(value, that.value) && Objects.equals(unit, that.unit) && Objects.equals(sparse, that.sparse) && Objects.equals(defaultConstraintName, that.defaultConstraintName) && Objects.equals(onUpdate, that.onUpdate); } @Override public int hashCode() { - return Objects.hash(name, tableName, columnType, defaultValue, autoIncrement, comment, columnSize, decimalDigits, numPrecRadix, sqlDataType, ordinalPosition, nullable, extent, charSetName, collationName, value, unit, sparse, defaultConstraintName); + return Objects.hash(name, tableName, columnType, defaultValue, autoIncrement, comment, columnSize, decimalDigits, numPrecRadix, sqlDataType, ordinalPosition, nullable, extent, charSetName, collationName, value, unit, sparse, defaultConstraintName, onUpdate); } }