!65 feat: 达梦数据库支持编辑表结构、索引

Merge pull request !65 from zongyangleo/dev_1207_dm
This commit is contained in:
Coder慌
2023-12-07 10:45:23 +00:00
committed by Gitee
8 changed files with 302 additions and 248 deletions

View File

@@ -98,7 +98,7 @@
<el-checkbox v-if="item.prop === 'unique'" size="small" v-model="scope.row.unique" @change="indexChanges(scope.row)">
</el-checkbox>
<el-select v-if="item.prop === 'indexType'" filterable size="small" v-model="scope.row.indexType">
<el-select v-if="item.prop === 'indexType'" disabled size="small" v-model="scope.row.indexType">
<el-option v-for="typeValue in indexTypeList" :key="typeValue" :value="typeValue">{{ typeValue }}</el-option>
</el-select>
@@ -134,7 +134,7 @@
import { reactive, ref, toRefs, watch } from 'vue';
import { ElMessage } from 'element-plus';
import SqlExecBox from '../sqleditor/SqlExecBox';
import { getDbDialect, DbType } from '../../dialect/index';
import { getDbDialect, DbType, RowDefinition, IndexDefinition } from '../../dialect/index';
const props = defineProps({
visible: {
@@ -168,7 +168,7 @@ const state = reactive({
btnloading: false,
activeName: '1',
columnTypeList: dbDialect.getInfo().columnTypes,
indexTypeList: ['BTREE'], // mysql索引类型详解 http://c.biancheng.net/view/7897.html
indexTypeList: ['BTREE', 'NORMAL'], // mysql索引类型详解 http://c.biancheng.net/view/7897.html
tableData: {
fields: {
colNames: [
@@ -214,17 +214,7 @@ const state = reactive({
label: '操作',
},
],
res: [] as {
name: string;
type: string;
value: string;
length: string;
numScale: string;
notNull: boolean;
pri: boolean;
auto_increment: boolean;
remark: string;
}[],
res: [] as RowDefinition[],
},
indexs: {
colNames: [
@@ -254,13 +244,7 @@ const state = reactive({
},
],
columns: [{ name: '', remark: '' }],
res: [] as {
indexName: string;
columnNames: string[];
unique: boolean;
indexType: 'BTREE';
indexComment: string;
}[],
res: [] as IndexDefinition[],
},
tableName: '',
tableComment: '',
@@ -294,117 +278,11 @@ const addRow = () => {
};
const addIndex = () => {
state.tableData.indexs.res.push({
indexName: '',
columnNames: [],
unique: false,
indexType: 'BTREE',
indexComment: '',
});
state.tableData.indexs.res.push(dbDialect.getDefaultIndex());
};
const addDefaultRows = () => {
if (props.dbType === DbType.mysql) {
state.tableData.fields.res.push(
{ name: 'id', type: 'bigint', length: '20', numScale: '', value: '', notNull: true, pri: true, auto_increment: true, remark: '主键ID' },
{ name: 'creator_id', type: 'bigint', length: '20', numScale: '', value: '', notNull: true, pri: false, auto_increment: false, remark: '创建人id' },
{
name: 'creator',
type: 'varchar',
length: '100',
numScale: '',
value: '',
notNull: true,
pri: false,
auto_increment: false,
remark: '创建人姓名',
},
{
name: 'create_time',
type: 'datetime',
length: '',
numScale: '',
value: 'CURRENT_TIMESTAMP',
notNull: true,
pri: false,
auto_increment: false,
remark: '创建时间',
},
{ name: 'updator_id', type: 'bigint', length: '20', numScale: '', value: '', notNull: true, pri: false, auto_increment: false, remark: '修改人id' },
{
name: 'updator',
type: 'varchar',
length: '100',
numScale: '',
value: '',
notNull: true,
pri: false,
auto_increment: false,
remark: '修改人姓名',
},
{
name: 'update_time',
type: 'datetime',
length: '',
numScale: '',
value: 'CURRENT_TIMESTAMP',
notNull: true,
pri: false,
auto_increment: false,
remark: '修改时间',
}
);
} else if (props.dbType === DbType.postgresql) {
state.tableData.fields.res.push(
{ name: 'id', type: 'bigserial', length: '', numScale: '', value: '', notNull: true, pri: true, auto_increment: true, remark: '主键ID' },
{ name: 'creator_id', type: 'int8', length: '', numScale: '', value: '', notNull: true, pri: false, auto_increment: false, remark: '创建人id' },
{
name: 'creator',
type: 'varchar',
length: '100',
numScale: '',
value: '',
notNull: true,
pri: false,
auto_increment: false,
remark: '创建人姓名',
},
{
name: 'create_time',
type: 'timestamp',
length: '',
numScale: '',
value: 'CURRENT_TIMESTAMP',
notNull: true,
pri: false,
auto_increment: false,
remark: '创建时间',
},
{ name: 'updator_id', type: 'int8', length: '', numScale: '', value: '', notNull: true, pri: false, auto_increment: false, remark: '修改人id' },
{
name: 'updator',
type: 'varchar',
length: '100',
numScale: '',
value: '',
notNull: true,
pri: false,
auto_increment: false,
remark: '修改人姓名',
},
{
name: 'update_time',
type: 'timestamp',
length: '',
numScale: '',
value: 'CURRENT_TIMESTAMP',
notNull: true,
pri: false,
auto_increment: false,
remark: '修改时间',
}
);
}
state.tableData.fields.res.push(...dbDialect.getDefaultRows());
};
const deleteRow = (index: any) => {
@@ -562,7 +440,7 @@ const indexChanges = (row: any) => {
row.indexComment = `${tableData.value.tableName}表(${name.replaceAll('_', ',')})${commentSuffix}`;
};
const oldData = { indexs: [] as any[], fields: [] as any[] };
const oldData = { indexs: [] as any[], fields: [] as RowDefinition[] };
watch(
() => props.data,
(newValue: any) => {

View File

@@ -181,7 +181,7 @@ const state = reactive({
visible: false,
activeName: '1',
type: '',
enableEditTypes: [DbType.mysql, DbType.postgresql], // 支持"编辑表"的数据库类型
enableEditTypes: [DbType.mysql, DbType.postgresql, DbType.dm], // 支持"编辑表"的数据库类型
data: {
// 修改表时,传递修改数据
edit: false,

View File

@@ -1,92 +1,52 @@
import { DbDialect, sqlColumnType, DialectInfo } from './index';
import { DbDialect, sqlColumnType, DialectInfo, RowDefinition, IndexDefinition } from './index';
export { DMDialect, GAUSS_TYPE_LIST };
export { DMDialect, DM_TYPE_LIST };
const GAUSS_TYPE_LIST: sqlColumnType[] = [
// 数值 - 整数型
{ udtName: 'int1', dataType: 'tinyint', desc: '微整数别名为INT1', space: '1字节', range: '0 ~ +255' },
{ udtName: 'int2', dataType: 'smallint', desc: '小范围整数别名为INT2。', space: '2字节', range: '-32,768 ~ +32,767' },
{ udtName: 'int4', dataType: 'integer', desc: '常用的整数别名为INT4。', space: '4字节', range: '-2,147,483,648 ~ +2,147,483,647' },
{ udtName: 'int8', dataType: 'bigint', desc: '大范围的整数别名为INT8。', space: '8字节', range: '很大' },
// 参考文档:https://eco.dameng.com/document/dm/zh-cn/sql-dev/dmpl-sql-datatype.html#%E5%AD%97%E7%AC%A6%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B
const DM_TYPE_LIST: sqlColumnType[] = [
// 字符数据类型
{ udtName: 'CHAR', dataType: 'VARCHAR', desc: '定长字符串', space: '', range: '1 - 32767' },
{ udtName: 'VARCHAR', dataType: 'VARCHAR', desc: '变长字符串', space: '', range: '1 - 32767' },
// 数值 - 任意精度型
// 精确数值数据类型 NUMERIC、DECIMAL、DEC 类型、NUMBER 类型、INTEGER 类型、INT 类型、BIGINT 类型、TINYINT 类型、BYTE 类型、SMALLINT
{ udtName: 'NUMERIC', dataType: 'NUMERIC', desc: '零、正负定点数', space: '1-38', range: '' },
{ udtName: 'DECIMAL', dataType: 'DECIMAL', desc: '与NUMERIC相似', space: '1-38', range: '' },
{ udtName: 'NUMBER', dataType: 'NUMBER', desc: '同NUMERIC', space: '1-38', range: '' },
{ udtName: 'INTEGER', dataType: 'INTEGER', desc: '有符号整数', space: '10', range: '-2^31-1 ~ 2^31-1' },
{ udtName: 'INT', dataType: 'INT', desc: '同INTEGER', space: '10', range: '' },
{ udtName: 'BIGINT', dataType: 'BIGINT', desc: '有符号整数', space: '19', range: '-2^63-1 ~ 2^63-1' },
{ udtName: 'TINYINT', dataType: 'TINYINT', desc: '有符号整数', space: '3', range: '-128~+127' },
{ udtName: 'BYTE', dataType: 'BYTE', desc: '与 TINYINT 相似', space: '3', range: '' },
{ udtName: 'SMALLINT', dataType: 'SMALLINT', desc: '有符号整数', space: '5', range: '-2^15-1 ~ 2^15-1' },
// (用得少,忽略)近似数值类型包括FLOAT 类型、DOUBLE 类型、REAL 类型、DOUBLE PRECISION 类型。
// 位串数据类型 BIT 用于存储整数数据 1、0 或 NULL只有 0 才转换为假,其他非空、非 0 值都会自动转换为真
{ udtName: 'BIT', dataType: 'BIT', desc: '用于存储整数数据 1、0 或 NULL', space: '1', range: '1' },
// 一般日期时间数据类型 DATE TIME TIMESTAMP 默认精度 6
// 多媒体数据类型 TEXT/LONG/LONGVARCHAR 类型:变长字符串类型 IMAGE/LONGVARBINARY 类型 BLOB CLOB BFILE 100G-1
{ udtName: 'DATE', dataType: 'DATE', desc: '年、月、日', space: '', range: '' },
{ udtName: 'TIME', dataType: 'TIME', desc: '时、分、秒', space: '', range: '' },
{
udtName: 'numeric',
dataType: 'numeric',
desc: '精度(总位数)取值范围为[1,1000],标度(小数位数)取值范围为[0,精度]。',
space: '每四位(十进制位)占用两个字节,然后在整个数据上加上八个字节的额外开销',
range: '未指定精度的情况下小数点前最大131,072位小数点后最大16,383位',
udtName: 'TIMESTAMP',
dataType: 'TIMESTAMP',
desc: '年、月、日、时、分、秒',
space: '',
range: '-4712-01-01 00:00:00.000000000 ~ 9999-12-31 23:59:59.999999999',
},
// 数值 - 任意精度型
{ udtName: 'decimal', dataType: 'decimal', desc: '等同于number类型', space: '等同于number类型' },
// 数值 - 序列整型
{ udtName: 'smallserial', dataType: 'smallserial', desc: '二字节序列整型。', space: '2字节', range: '-32,768 ~ +32,767' },
{ udtName: 'serial', dataType: 'serial', desc: '四字节序列整型。', space: '4字节', range: '-2,147,483,648 ~ +2,147,483,647' },
{ udtName: 'bigserial', dataType: 'bigserial', desc: '八字节序列整型', space: '8字节', range: '-9,223,372,036,854,775,808 ~ +9,223,372,036,854,775,807' },
{
udtName: 'largeserial',
dataType: 'largeserial',
desc: '默认插入十六字节序列整型实际数值类型和numeric相同',
space: '变长类型,每四位(十进制位)占用两个字节,然后在整个数据上加上八个字节的额外开销。',
range: '小数点前最大131,072位小数点后最大16,383位。',
},
// 数值 - 浮点类型(不常用 就不列出来了)
// 货币类型
{ udtName: 'money', dataType: 'money', desc: '货币金额', space: '8字节', range: '-92233720368547758.08 ~ +92233720368547758.07' },
// 布尔类型
{ udtName: 'bool', dataType: 'bool', desc: '布尔类型', space: '1字节', range: 'true真 , false假 , null未知unknown' },
// 字符类型
{ udtName: 'char', dataType: 'char', desc: '定长字符串不足补空格。n是指字节长度如不带精度n默认精度为1。', space: '最大为10MB' },
{ udtName: 'character', dataType: 'character', desc: '定长字符串不足补空格。n是指字节长度如不带精度n默认精度为1。', space: '最大为10MB' },
{ udtName: 'nchar', dataType: 'nchar', desc: '定长字符串不足补空格。n是指字节长度如不带精度n默认精度为1。', space: '最大为10MB' },
{ udtName: 'varchar', dataType: 'varchar', desc: '变长字符串。PG兼容模式下n是字符长度。其他兼容模式下n是指字节长度。', space: '最大为10MB。' },
{ udtName: 'text', dataType: 'text', desc: '变长字符串。', space: '最大稍微小于1GB-1。' },
{ udtName: 'clob', dataType: 'clob', desc: '文本大对象。是TEXT类型的别名。', space: '最大稍微小于32TB-1。' },
//特殊字符类型 用的很少,先屏蔽了
// { udtName: 'name', dataType: 'name', desc: '用于对象名的内部类型。', space: '64字节。' },
// { udtName: '"char"', dataType: '"char"', desc: '单字节内部类型。', space: '1字节。' },
// 二进制类型
{ udtName: 'bytea', dataType: 'bytea', desc: '变长的二进制字符串', space: '4字节加上实际的二进制字符串。最大为1GB减去8203字节即1073733621字节。' },
// 日期/时间类型
{ udtName: 'date', dataType: 'date', desc: '日期', space: '4字节' },
{ udtName: 'time', dataType: 'time', desc: 'TIME [(p)] 只用于一日内时间,p表示小数点后的精度取值范围为0~6。', space: '8-12字节' },
{ udtName: 'timestamp', dataType: 'timestamp', desc: 'TIMESTAMP[(p)]日期和时间,p表示小数点后的精度取值范围为0~6', space: '8字节' },
// 带时区的时间戳用的少,先屏蔽了
//{ udtName: 'TIMESTAMPTZ', dataType: 'TIMESTAMP WITH TIME ZONE', desc: '带时区的时间戳', space: '8字节' },
{
udtName: 'interval',
dataType: 'interval',
desc: '时间间隔', // 可以跟参数YEARMONTHDAYHOURMINUTESECONDDAY TO HOURDAY TO MINUTEDAY TO SECONDHOUR TO MINUTEHOUR TO SECONDMINUTE TO SECOND
space: '精度取值范围为0~6且参数为SECONDDAY TO SECONDHOUR TO SECOND或MINUTE TO SECOND时参数p才有效',
},
// 几何类型
{ udtName: 'point', dataType: 'point', desc: '平面中的点, 如:(x,y)', space: '16字节' },
{ udtName: 'lseg', dataType: 'lseg', desc: '(有限)线段, 如:((x1,y1),(x2,y2))', space: '32字节' },
{ udtName: 'box', dataType: 'box', desc: '矩形, 如:((x1,y1),(x2,y2))', space: '32字节' },
{ udtName: 'path', dataType: 'path', desc: '闭合路径(与多边形类似), 如:((x1,y1),...)', space: '16+16n字节' },
{ udtName: 'path', dataType: 'path', desc: '开放路径(与多边形类似), 如:[(x1,y1),...]', space: '16+16n字节' },
{ udtName: 'polygon', dataType: 'polygon', desc: '多边形(与闭合路径相似), 如:((x1,y1),...)', space: '40+16n字节' },
{ udtName: 'circle', dataType: 'polygon', desc: '圆,如:<(x,y),r> (圆心和半径)', space: '24 字节' },
// 网络地址类型
{ udtName: 'cidr', dataType: 'cidr', desc: 'IPv4网络', space: '7字节' },
{ udtName: 'inet', dataType: 'inet', desc: 'IPv4主机和网络', space: '7字节' },
{ udtName: 'macaddr', dataType: 'macaddr', desc: 'MAC地址', space: '6字节' },
{ udtName: 'TEXT', dataType: 'TEXT', desc: '变长字符串', space: '', range: '100G-1' },
{ udtName: 'LONG', dataType: 'LONG', desc: '同TEXT', space: '', range: '100G-1' },
{ udtName: 'LONGVARCHAR', dataType: 'LONGVARCHAR', desc: '同TEXT', space: '', range: '100G-1' },
{ udtName: 'IMAGE', dataType: 'IMAGE', desc: '图像二进制类型', space: '', range: '100G-1' },
{ udtName: 'LONGVARBINARY', dataType: 'LONGVARBINARY', desc: '同IMAGE', space: '', range: '100G-1' },
{ udtName: 'BLOB', dataType: 'BLOB', desc: '变长的二进制大对象', space: '', range: '100G-1' },
{ udtName: 'CLOB', dataType: 'CLOB', desc: '同TEXT', space: '', range: '100G-1' },
{ udtName: 'BFILE', dataType: 'BFILE', desc: '二进制文件', space: '', range: '100G-1' },
];
const dmDialectInfo: DialectInfo = {
icon: 'iconfont icon-db-dm',
defaultPort: 5236,
formatSqlDialect: 'postgresql',
columnTypes: GAUSS_TYPE_LIST.sort((a, b) => a.udtName.localeCompare(b.udtName)),
columnTypes: DM_TYPE_LIST.sort((a, b) => a.udtName.localeCompare(b.udtName)),
};
class DMDialect implements DbDialect {
@@ -100,6 +60,67 @@ class DMDialect implements DbDialect {
} LIMIT ${limit};`;
}
getDefaultRows(): RowDefinition[] {
return [
{ name: 'id', type: 'BIGINT', length: '', numScale: '', value: '', notNull: true, pri: true, auto_increment: true, remark: '主键ID' },
{ name: 'creator_id', type: 'BIGINT', length: '', numScale: '', value: '', notNull: true, pri: false, auto_increment: false, remark: '创建人id' },
{
name: 'creator',
type: 'VARCHAR',
length: '100',
numScale: '',
value: '',
notNull: true,
pri: false,
auto_increment: false,
remark: '创建人姓名',
},
{
name: 'create_time',
type: 'TIMESTAMP',
length: '',
numScale: '',
value: 'SYSDATE',
notNull: true,
pri: false,
auto_increment: false,
remark: '创建时间',
},
{ name: 'updator_id', type: 'BIGINT', length: '', numScale: '', value: '', notNull: true, pri: false, auto_increment: false, remark: '修改人id' },
{
name: 'updator',
type: 'VARCHAR',
length: '100',
numScale: '',
value: '',
notNull: true,
pri: false,
auto_increment: false,
remark: '修改人姓名',
},
{
name: 'update_time',
type: 'TIMESTAMP',
length: '',
numScale: '',
value: 'SYSDATE',
notNull: true,
pri: false,
auto_increment: false,
remark: '修改时间',
},
];
}
getDefaultIndex(): IndexDefinition {
return {
indexName: '',
columnNames: [],
unique: false,
indexType: 'NORMAL',
indexComment: '',
};
}
wrapName = (name: string) => {
return name;
};
@@ -120,9 +141,10 @@ class DMDialect implements DbDialect {
if (cl.value && cl.value.length > 0) {
// 哪些字段默认值需要加引号
let marks = false;
if (this.matchType(cl.type, ['char', 'time', 'date', 'text'])) {
if (this.matchType(cl.type, ['CHAR', 'TIME', 'DATE', 'TEXT'])) {
// 默认值是now()的time或date不需要加引号
if (cl.value.toLowerCase().replace(' ', '') === 'CURRENT_TIMESTAMP' && this.matchType(cl.type, ['time', 'date'])) {
let val = cl.value.toUpperCase().replace(' ', '');
if (this.matchType(cl.type, ['TIME', 'DATE']) && ['CURRENT_DATE', 'SYSDATE', 'CURDATE', 'CURTIME'].includes(val)) {
marks = false;
} else {
marks = true;
@@ -138,10 +160,10 @@ class DMDialect implements DbDialect {
}
getTypeLengthSql(cl: any) {
// 哪些字段可以指定长度
if (cl.length && this.matchType(cl.type, ['char', 'time', 'bit', 'num', 'decimal'])) {
// 哪些字段可以指定长度 VARCHAR/VARCHAR2/CHAR/BIT/NUMBER/NUMERIC/TIME、TIMESTAMP(可以指定小数秒精度)
if (cl.length && this.matchType(cl.type, ['CHAR', 'BIT', 'TIME', 'NUM', 'DEC'])) {
// 哪些字段类型可以指定小数点
if (cl.numScale && this.matchType(cl.type, ['num', 'decimal'])) {
if (cl.numScale && this.matchType(cl.type, ['NUM', 'DEC'])) {
return `(${cl.length}, ${cl.numScale})`;
} else {
return `(${cl.length})`;
@@ -150,11 +172,12 @@ class DMDialect implements DbDialect {
return '';
}
genColumnBasicSql(cl: any): string {
genColumnBasicSql(cl: RowDefinition): string {
let length = this.getTypeLengthSql(cl);
// 默认值
let defVal = this.getDefaultValueSql(cl);
return ` ${cl.name} ${cl.type}${length} ${cl.notNull ? 'NOT NULL' : ''} ${defVal} `;
let incr = cl.auto_increment ? 'IDENTITY' : '';
return ` ${cl.name} ${cl.type}${length} ${incr} ${cl.notNull ? 'NOT NULL' : ''} ${defVal} `;
}
getCreateTableSql(data: any): string {
@@ -196,30 +219,26 @@ class DMDialect implements DbDialect {
let sql: string[] = [];
tableData.indexs.res.forEach((a: any) => {
sql.push(` CREATE ${a.unique ? 'UNIQUE' : ''} INDEX ${a.indexName} USING btree ("${a.columnNames.join('","')})"`);
if (a.indexComment) {
sql.push(`COMMENT ON INDEX ${a.indexName} IS '${a.indexComment}'`);
}
});
return sql.join(';');
}
getModifyColumnSql(tableName: string, changeData: { del: any[]; add: any[]; upd: any[] }): string {
getModifyColumnSql(tableName: string, changeData: { del: RowDefinition[]; add: RowDefinition[]; upd: RowDefinition[] }): string {
let sql: string[] = [];
if (changeData.add.length > 0) {
changeData.add.forEach((a) => {
let typeLength = this.getTypeLengthSql(a);
let defaultSql = this.getDefaultValueSql(a);
sql.push(`ALTER TABLE ${tableName} add ${a.name} ${a.type}${typeLength} ${defaultSql}`);
sql.push(`ALTER TABLE ${tableName} add COLUMN ${this.genColumnBasicSql(a)}`);
if (a.remark) {
sql.push(`comment on COLUMN "${tableName}"."${a.name}" is '${a.remark}'`);
}
});
}
if (changeData.upd.length > 0) {
changeData.upd.forEach((a) => {
let typeLength = this.getTypeLengthSql(a);
sql.push(`ALTER TABLE ${tableName} alter column ${a.name} type ${a.type}${typeLength}`);
let defaultSql = this.getDefaultValueSql(a);
if (defaultSql) {
sql.push(`alter table ${tableName} alter column ${a.name} set ${defaultSql}`);
sql.push(`ALTER TABLE ${tableName} MODIFY ${this.genColumnBasicSql(a)}`);
if (a.remark) {
sql.push(`comment on COLUMN "${tableName}"."${a.name}" is '${a.remark}'`);
}
});
}

View File

@@ -11,6 +11,26 @@ export interface sqlColumnType {
range?: string;
}
export interface RowDefinition {
name: string;
type: string;
value: string;
length: string;
numScale: string;
notNull: boolean;
pri: boolean;
auto_increment: boolean;
remark: string;
}
export interface IndexDefinition {
indexName: string;
columnNames: string[];
unique: boolean;
indexType: string;
indexComment?: string;
}
// 数据库基础信息
export interface DialectInfo {
/**
@@ -56,6 +76,10 @@ export interface DbDialect {
*/
getDefaultSelectSql(table: string, condition: string, orderBy: string, pageNum: number, limit: number): string;
getDefaultRows(): RowDefinition[];
getDefaultIndex(): IndexDefinition;
/**
* 包裹数据库表名、字段名等,避免使用关键字为字段名或表名时报错
* @param name 名称
@@ -79,7 +103,7 @@ export interface DbDialect {
* @param tableName 表名
* @param changeData 改变信息
*/
getModifyColumnSql(tableName: string, changeData: { del: any[]; add: any[]; upd: any[] }): string;
getModifyColumnSql(tableName: string, changeData: { del: RowDefinition[]; add: RowDefinition[]; upd: RowDefinition[] }): string;
/**
* 生成编辑索引sql

View File

@@ -1,4 +1,4 @@
import { DbDialect, DialectInfo } from './index';
import { DbDialect, DialectInfo, IndexDefinition, RowDefinition } from './index';
export { MYSQL_TYPE_LIST, MysqlDialect, language };
@@ -47,6 +47,68 @@ class MysqlDialect implements DbDialect {
}, ${limit};`;
}
getDefaultRows(): RowDefinition[] {
return [
{ name: 'id', type: 'bigint', length: '20', numScale: '', value: '', notNull: true, pri: true, auto_increment: true, remark: '主键ID' },
{ name: 'creator_id', type: 'bigint', length: '20', numScale: '', value: '', notNull: true, pri: false, auto_increment: false, remark: '创建人id' },
{
name: 'creator',
type: 'varchar',
length: '100',
numScale: '',
value: '',
notNull: true,
pri: false,
auto_increment: false,
remark: '创建人姓名',
},
{
name: 'create_time',
type: 'datetime',
length: '',
numScale: '',
value: 'CURRENT_TIMESTAMP',
notNull: true,
pri: false,
auto_increment: false,
remark: '创建时间',
},
{ name: 'updator_id', type: 'bigint', length: '20', numScale: '', value: '', notNull: true, pri: false, auto_increment: false, remark: '修改人id' },
{
name: 'updator',
type: 'varchar',
length: '100',
numScale: '',
value: '',
notNull: true,
pri: false,
auto_increment: false,
remark: '修改人姓名',
},
{
name: 'update_time',
type: 'datetime',
length: '',
numScale: '',
value: 'CURRENT_TIMESTAMP',
notNull: true,
pri: false,
auto_increment: false,
remark: '修改时间',
},
];
}
getDefaultIndex(): IndexDefinition {
return {
indexName: '',
columnNames: [],
unique: false,
indexType: 'BTREE',
indexComment: '',
};
}
wrapName = (name: string) => {
return `\`${name}\``;
};
@@ -86,7 +148,7 @@ class MysqlDialect implements DbDialect {
return sql.substring(0, sql.length - 1) + ';';
}
getModifyColumnSql(tableName: string, changeData: { del: any[]; add: any[]; upd: any[] }): string {
getModifyColumnSql(tableName: string, changeData: { del: RowDefinition[]; add: RowDefinition[]; upd: RowDefinition[] }): string {
let addSql = '',
updSql = '',
delSql = '';

View File

@@ -1,4 +1,4 @@
import { DbDialect, DialectInfo, sqlColumnType } from './index';
import { DbDialect, DialectInfo, IndexDefinition, RowDefinition, sqlColumnType } from './index';
export { PostgresqlDialect, GAUSS_TYPE_LIST };
@@ -100,6 +100,68 @@ class PostgresqlDialect implements DbDialect {
} LIMIT ${limit};`;
}
getDefaultRows(): RowDefinition[] {
return [
{ name: 'id', type: 'bigserial', length: '', numScale: '', value: '', notNull: true, pri: true, auto_increment: true, remark: '主键ID' },
{ name: 'creator_id', type: 'int8', length: '', numScale: '', value: '', notNull: true, pri: false, auto_increment: false, remark: '创建人id' },
{
name: 'creator',
type: 'varchar',
length: '100',
numScale: '',
value: '',
notNull: true,
pri: false,
auto_increment: false,
remark: '创建人姓名',
},
{
name: 'create_time',
type: 'timestamp',
length: '',
numScale: '',
value: 'CURRENT_TIMESTAMP',
notNull: true,
pri: false,
auto_increment: false,
remark: '创建时间',
},
{ name: 'updator_id', type: 'int8', length: '', numScale: '', value: '', notNull: true, pri: false, auto_increment: false, remark: '修改人id' },
{
name: 'updator',
type: 'varchar',
length: '100',
numScale: '',
value: '',
notNull: true,
pri: false,
auto_increment: false,
remark: '修改人姓名',
},
{
name: 'update_time',
type: 'timestamp',
length: '',
numScale: '',
value: 'CURRENT_TIMESTAMP',
notNull: true,
pri: false,
auto_increment: false,
remark: '修改时间',
},
];
}
getDefaultIndex(): IndexDefinition {
return {
indexName: '',
columnNames: [],
unique: false,
indexType: 'BTREE',
indexComment: '',
};
}
wrapName = (name: string) => {
return name;
};
@@ -122,7 +184,7 @@ class PostgresqlDialect implements DbDialect {
let marks = false;
if (this.matchType(cl.type, ['char', 'time', 'date', 'text'])) {
// 默认值是now()的time或date不需要加引号
if (cl.value.toLowerCase().replace(' ', '') === 'CURRENT_TIMESTAMP' && this.matchType(cl.type, ['time', 'date'])) {
if (cl.value.toLowerCase().replace(' ', '') === 'current_timestamp' && this.matchType(cl.type, ['time', 'date'])) {
marks = false;
} else {
marks = true;
@@ -203,13 +265,16 @@ class PostgresqlDialect implements DbDialect {
return sql.join(';');
}
getModifyColumnSql(tableName: string, changeData: { del: any[]; add: any[]; upd: any[] }): string {
getModifyColumnSql(tableName: string, changeData: { del: RowDefinition[]; add: RowDefinition[]; upd: RowDefinition[] }): string {
let sql: string[] = [];
if (changeData.add.length > 0) {
changeData.add.forEach((a) => {
let typeLength = this.getTypeLengthSql(a);
let defaultSql = this.getDefaultValueSql(a);
sql.push(`ALTER TABLE ${tableName} add ${a.name} ${a.type}${typeLength} ${defaultSql}`);
if (a.remark) {
sql.push(`comment on column "${tableName}"."${a.name}" is '${a.remark}'`);
}
});
}
@@ -221,6 +286,9 @@ class PostgresqlDialect implements DbDialect {
if (defaultSql) {
sql.push(`alter table ${tableName} alter column ${a.name} set ${defaultSql}`);
}
if (a.remark) {
sql.push(`comment on column "${tableName}"."${a.name}" is '${a.remark}'`);
}
});
}

View File

@@ -137,7 +137,7 @@ func (pd *DMDialect) GetTableIndex(tableName string) ([]Index, error) {
indexs = append(indexs, Index{
IndexName: re["indexName"].(string),
ColumnName: anyx.ConvString(re["columnName"]),
IndexType: anyx.ConvString(re["IndexType"]),
IndexType: anyx.ConvString(re["indexType"]),
IndexComment: anyx.ConvString(re["indexComment"]),
NonUnique: anyx.ConvInt(re["nonUnique"]),
SeqInIndex: anyx.ConvInt(re["seqInIndex"]),

View File

@@ -15,19 +15,22 @@ where a.owner = (SELECT SF_GET_SCHEMA_NAME_BY_ID(CURRENT_SCHID))
and a.object_type = 'TABLE'
---------------------------------------
--DM_INDEX_INFO 表索引信息
SELECT
indexname AS "indexName",
'BTREE' AS "IndexType",
case when indexdef like 'CREATE UNIQUE INDEX%%' then 0 else 1 end as "nonUnique",
obj_description(b.oid, 'pg_class') AS "indexComment",
indexdef AS "indexDef",
c.attname AS "columnName",
c.attnum AS "seqInIndex"
FROM pg_indexes a
join pg_class b on a.indexname = b.relname
join pg_attribute c on b.oid = c.attrelid
WHERE a.schemaname = (select current_schema())
AND a.tablename = '%s';
select
a.index_name as indexName,
a.index_type as indexType,
case when a.uniqueness = 'UNIQUE' then 1 else 0 end as nonUnique,
indexdef(b.object_id,1) as indexDef,
c.column_name as columnName,
c.column_position as seqInIndex,
'' as indexComment
FROM DBA_INDEXES a
JOIN dba_objects b on a.owner = b.owner and b.object_name = a.index_name and b.object_type = 'INDEX'
JOIN DBA_IND_COLUMNS c on a.owner = c.table_owner and a.index_name = c.index_name and a.TABLE_NAME = c.table_name
WHERE a.owner = (SELECT SF_GET_SCHEMA_NAME_BY_ID(CURRENT_SCHID))
and a.TABLE_NAME = '%s'
and indexdef(b.object_id,1) != '禁止查看系统定义的索引信息'
order by a.TABLE_NAME, a.index_name, c.column_position asc
---------------------------------------
--DM_COLUMN_MA 表列信息
select a.table_name as tableName,