From 664776a3ec973357c1fe34466bcbab8246c4c9cd Mon Sep 17 00:00:00 2001 From: SliverHorn <503551462@qq.com> Date: Mon, 22 Nov 2021 18:03:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20-=20pgsql=20=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E6=95=B0=E6=8D=AE=E5=BA=93=E5=92=8C=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E6=95=B0=E6=8D=AE=E8=A1=A8=E7=9A=84=E6=89=80=E6=9C=89?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E5=90=8D,=E7=B1=BB=E5=9E=8B=E5=80=BC?= =?UTF-8?q?=E7=AD=89=20sql=20=E6=B7=BB=E5=8A=A0=E5=AD=97=E6=AE=B5=E5=A4=87?= =?UTF-8?q?=E6=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/service/system/sys_auto_code_pgsql.go | 59 ++++++++++++-------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/server/service/system/sys_auto_code_pgsql.go b/server/service/system/sys_auto_code_pgsql.go index 1f0b1b15..659b9e44 100644 --- a/server/service/system/sys_auto_code_pgsql.go +++ b/server/service/system/sys_auto_code_pgsql.go @@ -7,6 +7,7 @@ import ( "gorm.io/driver/postgres" "gorm.io/gorm" "gorm.io/gorm/logger" + "strings" ) var AutoCodePgsql = new(autoCodePgsql) @@ -43,34 +44,46 @@ func (a *autoCodePgsql) GetTables(dbName string) (data []response.Table, err err func (a *autoCodePgsql) GetColumn(tableName string, dbName string) (data []response.Column, err error) { // todo 数据获取不全, 待完善sql sql := ` - SELECT columns.COLUMN_NAME as column_name, - columns.DATA_TYPE as data_type, - CASE - columns.DATA_TYPE - WHEN 'text' THEN - concat_ws('', '', columns.CHARACTER_MAXIMUM_LENGTH) - WHEN 'varchar' THEN - concat_ws('', '', columns.CHARACTER_MAXIMUM_LENGTH) - WHEN 'smallint' THEN - concat_ws(',', columns.NUMERIC_PRECISION, columns.NUMERIC_SCALE) - WHEN 'decimal' THEN - concat_ws(',', columns.NUMERIC_PRECISION, columns.NUMERIC_SCALE) - WHEN 'integer' THEN - concat_ws('', '', columns.NUMERIC_PRECISION) - WHEN 'bigint' THEN - concat_ws('', '', columns.NUMERIC_PRECISION) - ELSE '' - END AS data_type_long - FROM INFORMATION_SCHEMA.COLUMNS columns - WHERE table_catalog = ? - and table_schema = ? - and table_name = ? + SELECT columns.COLUMN_NAME as column_name, + columns.DATA_TYPE as data_type, + CASE + columns.DATA_TYPE + WHEN 'text' THEN + concat_ws('', '', columns.CHARACTER_MAXIMUM_LENGTH) + WHEN 'varchar' THEN + concat_ws('', '', columns.CHARACTER_MAXIMUM_LENGTH) + WHEN 'smallint' THEN + concat_ws(',', columns.NUMERIC_PRECISION, columns.NUMERIC_SCALE) + WHEN 'decimal' THEN + concat_ws(',', columns.NUMERIC_PRECISION, columns.NUMERIC_SCALE) + WHEN 'integer' THEN + concat_ws('', '', columns.NUMERIC_PRECISION) + WHEN 'bigint' THEN + concat_ws('', '', columns.NUMERIC_PRECISION) + ELSE '' + END AS data_type_long, + (select description.description + from pg_description description + where description.objoid = (select attribute.attrelid + from pg_attribute attribute + where attribute.attrelid = + (select oid from pg_class class where class.relname = '@table_name') and attname =columns.COLUMN_NAME ) + and description.objsubid = (select attribute.attnum + from pg_attribute attribute + where attribute.attrelid = + (select oid from pg_class class where class.relname = '@table_name') and attname =columns.COLUMN_NAME )) as column_comment + FROM INFORMATION_SCHEMA.COLUMNS columns + WHERE table_catalog = '@table_catalog' + and table_schema = 'public' + and table_name = '@table_name'; ` var entities []response.Column db, _err := gorm.Open(postgres.Open(global.GVA_CONFIG.Pgsql.LinkDsn(dbName)), &gorm.Config{Logger: logger.Default.LogMode(logger.Info)}) if _err != nil { return nil, errors.Wrapf(err, "[pgsql] 连接 数据库(%s)的表(%s)失败!", dbName, tableName) } - err = db.Raw(sql, dbName, "public", tableName).Scan(&entities).Error + sql = strings.ReplaceAll(sql, "@table_catalog", dbName) + sql = strings.ReplaceAll(sql, "@table_name", tableName) + err = db.Raw(sql).Scan(&entities).Error return entities, err }