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

DATETIME and TIMESTAMP types of MySQL database are added to update attributes according to the current timestamp. #1086

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions chat2db-client/src/blocks/DatabaseTableEditor/ColumnList/index.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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'],
Expand Down Expand Up @@ -91,6 +91,7 @@ const createInitialData = () => {
collationName: null,
value: null,
editStatus: EditColumnOperationType.Add,
onUpdate: false,
};
};

Expand Down Expand Up @@ -506,6 +507,14 @@ const ColumnList = forwardRef((props: IProps, ref: ForwardedRef<IColumnListRef>)
<CustomSelect options={databaseSupportField.defaultValues} />
</Form.Item>
)}
{editingConfig?.supportDefaultValue && databaseType === DatabaseTypeCode.MYSQL
&& onUpdateTypeName.includes(editingConfig?.typeName) && (
<Form.Item labelCol={labelCol} label="" name="onUpdate" valuePropName="checked" style={{paddingLeft: 100}}>
<Checkbox >
{i18n('editTable.label.onUpdate')}
</Checkbox>
</Form.Item>
)}
{editingConfig?.supportCharset && (
<Form.Item labelCol={labelCol} label={i18n('editTable.label.characterSet')} name="charSetName">
<CustomSelect options={databaseSupportField.charsets} />
Expand Down
1 change: 1 addition & 0 deletions chat2db-client/src/i18n/en-us/editTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions chat2db-client/src/i18n/ja-jp/editTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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': '照合順序',
Expand Down
1 change: 1 addition & 0 deletions chat2db-client/src/i18n/tr-tr/editTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions chat2db-client/src/i18n/zh-cn/editTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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': '排序规则',
Expand Down
1 change: 1 addition & 0 deletions chat2db-client/src/typings/editTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface IColumnItemNew {
charSetName: string | null; // 字符集名
collationName: string | null; // 排序规则名
value: string | null; // 值
onUpdate: boolean | null; // 是否根据当前时间戳更新
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ public List<TableColumn> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ");
Expand Down Expand Up @@ -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 "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}