* fix: 表结构同步修复
* fix: 达梦低权限兼容,pgsql bool值转换
This commit is contained in:
zongyangleo
2025-03-17 11:12:52 +00:00
committed by Coder慌
parent bc21ba7c1e
commit 3c0292b56e
10 changed files with 77 additions and 24 deletions

View File

@@ -156,6 +156,12 @@ func SQLValueNumeric(val any) string {
}
return fmt.Sprintf("%v", val)
}
func SQLValueBool(val any) string {
if val == nil {
return "false"
}
return fmt.Sprintf("%v", cast.ToBool(val))
}
func SQLValueString(val any) string {
if val == nil {
@@ -177,6 +183,12 @@ var (
SQLValue: SQLValueNumeric,
}
DTBool = &DataType{
Name: "bool",
Valuer: ValuerBit,
SQLValue: SQLValueBool,
}
DTByte = &DataType{
Name: "uint8",
Valuer: ValuerByte,

View File

@@ -1,8 +1,5 @@
--DM_DB_SCHEMAS schemas
SELECT
DISTINCT object_name as SCHEMA_NAME
FROM ALL_OBJECTS
WHERE OBJECT_TYPE = 'SCH'
SELECT NAME as SCHEMA_NAME FROM SYS.SYSOBJECTS WHERE TYPE$ = 'SCH' ORDER BY NAME ASC
---------------------------------------
--DM_TABLE_INFO 表详细信息
SELECT a.object_name as TABLE_NAME,
@@ -19,7 +16,7 @@ SELECT a.object_name as TABLE_NAME,
WHERE OWNER = 'wxb'
AND TABLE_NAME = a.object_name)) as INDEX_LENGTH,
c.num_rows as TABLE_ROWS
FROM all_objects a
FROM SYSOBJECTS a
LEFT JOIN ALL_TAB_COMMENTS b ON b.TABLE_TYPE = 'TABLE'
AND a.object_name = b.TABLE_NAME
AND b.owner = a.owner
@@ -33,6 +30,18 @@ WHERE a.owner = (SELECT SF_GET_SCHEMA_NAME_BY_ID(CURRENT_SCHID))
{{end}}
ORDER BY a.object_name
---------------------------------------
--DM_TABLE_INFO_NAME_ONLY 表名列表
SELECT TABS.NAME as TABLE_NAME FROM
(SELECT ID, PID FROM SYS.SYSOBJECTS WHERE TYPE$ = 'SCH' AND NAME = (SELECT SF_GET_SCHEMA_NAME_BY_ID(CURRENT_SCHID))) SCHEMAS,
(SELECT ID, SCHID, NAME FROM SYS.SYSOBJECTS WHERE TYPE$ = 'SCHOBJ' AND SUBTYPE$ IN ('UTAB', 'STAB', 'VIEW', 'SYNOM')
AND ((SUBTYPE$ ='UTAB' AND CAST((INFO3 & 0x00FF & 0x003F) AS INT) not in (9, 27, 29, 25, 12, 7, 21, 23, 18, 5))
OR SUBTYPE$ in ('STAB', 'VIEW', 'SYNOM'))) TABS
WHERE TABS.SCHID = SCHEMAS.ID
AND SF_CHECK_PRIV_OPT(UID(), CURRENT_USERTYPE(), TABS.ID, SCHEMAS.PID, -1, TABS.ID) = 1
{{if .tableNames}}
and TABS.NAME in ({{.tableNames}})
{{end}}
---------------------------------------
--DM_INDEX_INFO 表索引信息
select a.index_name as INDEX_NAME,
a.index_type as INDEX_TYPE,
@@ -81,4 +90,23 @@ from all_tab_columns a
where a.owner = (SELECT SF_GET_SCHEMA_NAME_BY_ID(CURRENT_SCHID))
and a.table_name in (%s)
order by a.table_name,
a.column_id
a.column_id
---------------------------------------
--DM_COLUMN_MA_EX 表列信息
SELECT
TABS.TABLE_NAME TABLE_NAME,
COLS.NAME COLUMN_NAME,
case when COLS.NULLABLE$ = 'Y' then 'YES' when COLS.NULLABLE$ = 'N' then 'NO' else 'NO' end as NULLABLE,
COLS.TYPE$ DATA_TYPE,
COLS.LENGTH$ CHAR_MAX_LENGTH,
COLS.SCALE NUM_SCALE,
COLS.DEFVAL COLUMN_DEFAULT,
case when COLS.INFO2 & 0x01 = 0x01 then 1 else 0 end as IS_IDENTITY,
case when COLS.INFO2 & 0x01 = 0x01 then 1 else 0 end as IS_PRIMARY_KEY
FROM
(SELECT ID FROM SYS.SYSOBJECTS WHERE TYPE$ = 'SCH' AND NAME = (SELECT SF_GET_SCHEMA_NAME_BY_ID(CURRENT_SCHID))) SCHS,
(SELECT ID,SCHID,NAME TABLE_NAME FROM SYS.SYSOBJECTS WHERE TYPE$ = 'SCHOBJ' AND SUBTYPE$ IN ('UTAB', 'STAB', 'VIEW') AND NAME in (%s)) TABS,
SYS.SYSCOLUMNS COLS
WHERE TABS.ID = COLS.ID AND SCHS.ID = TABS.SCHID;

View File

@@ -15,6 +15,7 @@ const (
CTLongtext
CTBit // 1 bit
CTBool // 1 bit
CTInt1 // 1字节 -128~127
CTInt2 // 2字节 -32768~32767
CTInt4 // 4字节 -2147483648~2147483647

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"mayfly-go/internal/db/dbm/dbi"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/utils/anyx"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/stringx"
@@ -13,11 +14,13 @@ import (
)
const (
DM_META_FILE = "metasql/dm_meta.sql"
DM_DB_SCHEMAS = "DM_DB_SCHEMAS"
DM_TABLE_INFO_KEY = "DM_TABLE_INFO"
DM_INDEX_INFO_KEY = "DM_INDEX_INFO"
DM_COLUMN_MA_KEY = "DM_COLUMN_MA"
DM_META_FILE = "metasql/dm_meta.sql"
DM_DB_SCHEMAS = "DM_DB_SCHEMAS"
DM_TABLE_INFO_KEY = "DM_TABLE_INFO"
DM_TABLE_INFO_NAME_ONLY = "DM_TABLE_INFO_NAME_ONLY" // 当查询表详情失败时,可能是因为没有系统表查询权限,所以尝试只查表名
DM_INDEX_INFO_KEY = "DM_INDEX_INFO"
DM_COLUMN_MA_KEY = "DM_COLUMN_MA"
DM_COLUMN_MA_EX_KEY = "DM_COLUMN_MA_EX"
)
type DMMetadata struct {
@@ -67,7 +70,12 @@ func (dd *DMMetadata) GetTables(tableNames ...string) ([]dbi.Table, error) {
_, res, err = dd.dc.Query(sql)
if err != nil {
return nil, err
// 尝试只查表名
sql, err = stringx.TemplateParse(dbi.GetLocalSql(DM_META_FILE, DM_TABLE_INFO_NAME_ONLY), collx.M{"tableNames": names})
_, res, err = dd.dc.Query(sql)
if err != nil {
return nil, err
}
}
tables := make([]dbi.Table, 0)
@@ -93,7 +101,10 @@ func (dd *DMMetadata) GetColumns(tableNames ...string) ([]dbi.Column, error) {
_, res, err := dd.dc.Query(fmt.Sprintf(dbi.GetLocalSql(DM_META_FILE, DM_COLUMN_MA_KEY), tableName))
if err != nil {
return nil, err
_, res, err = dd.dc.Query(fmt.Sprintf(dbi.GetLocalSql(DM_META_FILE, DM_COLUMN_MA_EX_KEY), tableName))
if err != nil {
return nil, err
}
}
columns := make([]dbi.Column, 0)
@@ -138,7 +149,8 @@ func (dd *DMMetadata) GetPrimaryKey(tablename string) (string, error) {
func (dd *DMMetadata) GetTableIndex(tableName string) ([]dbi.Index, error) {
_, res, err := dd.dc.Query(fmt.Sprintf(dbi.GetLocalSql(DM_META_FILE, DM_INDEX_INFO_KEY), tableName))
if err != nil {
return nil, err
logx.Error("查询达梦索引信息失败", err)
return nil, nil
}
indexs := make([]dbi.Index, 0)

View File

@@ -5,7 +5,7 @@ import (
)
var (
Bool = dbi.NewDbDataType("bool", dbi.DTBit).WithCT(dbi.CTBit).WithFixColumn(dbi.ClearNumScale)
Bool = dbi.NewDbDataType("bool", dbi.DTBool).WithCT(dbi.CTBool).WithFixColumn(dbi.ClearNumScale)
Int2 = dbi.NewDbDataType("int2", dbi.DTInt16).WithCT(dbi.CTInt2).WithFixColumn(dbi.ClearNumScale)
Int4 = dbi.NewDbDataType("int4", dbi.DTInt32).WithCT(dbi.CTInt4).WithFixColumn(dbi.ClearNumScale)
Int8 = dbi.NewDbDataType("int8", dbi.DTInt64).WithCT(dbi.CTInt8).WithFixColumn(dbi.ClearNumScale)

View File

@@ -11,6 +11,6 @@ type DbSql struct {
DbId uint64 `json:"dbId" gorm:"not null;"`
Db string `json:"db" gorm:"size:100;not null;"`
Type int `json:"type" gorm:"not null;"` // 类型
Sql string `json:"sql" gorm:"type:text;comment:sql语句"`
Sql string `json:"sql" gorm:"type:longtext;comment:sql语句"`
Name string `json:"name" gorm:"size:255;not null;comment:sql模板名"`
}

View File

@@ -5,9 +5,9 @@ import "mayfly-go/pkg/model"
type Resource struct {
model.Model
Pid int64 `json:"pid" gorm:"not null;comment:父节点id;"`
UiPath string `json:"ui_path" gorm:"size:300;not null;comment:唯一标识路径;"` // 唯一标识路径
Type int8 `json:"type" gorm:"not null;comment:1菜单路由2资源按钮等;"` // 1菜单路由2资源按钮等
Status int8 `json:"status" gorm:"not null;comment:状态1:可用,-1:禁用;"` // 1可用-1不可用
UiPath string `json:"ui_path" gorm:"size:300;comment:唯一标识路径;"` // 唯一标识路径
Type int8 `json:"type" gorm:"not null;comment:1菜单路由2资源按钮等;"` // 1菜单路由2资源按钮等
Status int8 `json:"status" gorm:"not null;comment:状态1:可用,-1:禁用;"` // 1可用-1不可用
Code string `json:"code" gorm:"size:300;comment:菜单路由为path其他为唯一标识;"`
Name string `json:"name" gorm:"size:255;not null;"`
Weight int `json:"weight"`

View File

@@ -10,9 +10,9 @@ type SysLog struct {
Type int8 `json:"type" gorm:"not null;"`
Description string `json:"description" gorm:"size:255;"`
ReqParam string `json:"reqParam" gorm:"size:2000"` // 请求参数
Resp string `json:"resp" gorm:"type:text;"` // 响应结构
Extra string `json:"extra" gorm:"type:text;"` // 日志额外信息
ReqParam string `json:"reqParam" gorm:"type:text;"` // 请求参数
Resp string `json:"resp" gorm:"type:text;"` // 响应结构
Extra string `json:"extra" gorm:"type:text;"` // 日志额外信息
}
func (a *SysLog) TableName() string {

View File

@@ -16,7 +16,7 @@ type TagTree struct {
Type TagType `json:"type" gorm:"not null;default:-1;comment:类型: -1.普通标签; 1机器 2db 3redis 4mongo"` // 类型: -1.普通标签; 其他值则为对应的资源类型
Code string `json:"code" gorm:"not null;size:50;index:idx_tag_code;comment:标识符"` // 标识编码, 若类型不为-1则为对应资源编码
CodePath string `json:"codePath" gorm:"not null;size:800;index:idx_tag_code_path,length:255;comment:标识符路径"` // 标识路径tag1/tag2/tagType1|tagCode/tagType2|yyycode/,非普通标签类型段含有标签类型
CodePath string `json:"codePath" gorm:"not null;size:700;index:idx_tag_code_path,length:255;comment:标识符路径"` // 标识路径tag1/tag2/tagType1|tagCode/tagType2|yyycode/,非普通标签类型段含有标签类型
Name string `json:"name" gorm:"size:50;comment:名称"` // 名称
Remark string `json:"remark" gorm:"size:255;"` // 备注说明
}