go gorm极简元数据处理
func Test003_GetDbMeta(t *testing.T) { var dbfacade FindBeanDbmetaFacade() ret : dbfacade.GetDbMeta(plat_menu) golog.Info(dbmeta:, ret) }2026-03-15 13:42:01.939 [INFO] dbmeta: { code: 200, msg: 成功, exist: true, data: { tableExist: false, tableType: , tableSchema: , tableName: plat_menu, indexName: , fieldsName: , columns: [ { goType: int64, tableName: plat_menu, tableSchema: , columnName: id, dbTypeName: int8, primaryKey: true, autoIncrement: true, nullable: false, unique: false, columnType: bigint, length: 64, comment: , defaultValue: , decimalSize: { Precision: 64, Scale: 0 } }, { goType: string, tableName: plat_menu, tableSchema: , columnName: menu_code, dbTypeName: varchar, primaryKey: false, autoIncrement: false, nullable: true, unique: false, columnType: character varying, length: -5, comment: , defaultValue: , decimalSize: { Precision: 0, Scale: 0 } }, { goType: string, tableName: plat_menu, tableSchema: , columnName: menu_name, dbTypeName: varchar, primaryKey: false, autoIncrement: false, nullable: true, unique: false, columnType: character varying, length: -5, comment: , defaultValue: , decimalSize: { Precision: 0, Scale: 0 } }, { goType: string, tableName: plat_menu, tableSchema: , columnName: list_order, dbTypeName: varchar, primaryKey: false, autoIncrement: false, nullable: true, unique: false, columnType: character varying, length: -5, comment: , defaultValue: , decimalSize: { Precision: 0, Scale: 0 } }, { goType: int64, tableName: plat_menu, tableSchema: , columnName: created_by, dbTypeName: int8, primaryKey: false, autoIncrement: false, nullable: true, unique: false, columnType: bigint, length: 64, comment: , defaultValue: 1, decimalSize: { Precision: 64, Scale: 0 } }, { goType: time.Time, tableName: plat_menu, tableSchema: , columnName: created_at, dbTypeName: timestamptz, primaryKey: false, autoIncrement: false, nullable: true, unique: false, columnType: timestamp with time zone, length: 64, comment: , defaultValue: , decimalSize: { Precision: 6, Scale: 0 } }, { goType: bool, tableName: plat_menu, tableSchema: , columnName: active, dbTypeName: bool, primaryKey: false, autoIncrement: false, nullable: true, unique: false, columnType: boolean, length: 8, comment: , defaultValue: true, decimalSize: { Precision: 0, Scale: 0 } }, { goType: bool, tableName: plat_menu, tableSchema: , columnName: visible, dbTypeName: bool, primaryKey: false, autoIncrement: false, nullable: true, unique: false, columnType: boolean, length: 8, comment: , defaultValue: true, decimalSize: { Precision: 0, Scale: 0 } }, { goType: string, tableName: plat_menu, tableSchema: , columnName: subsys, dbTypeName: varchar, primaryKey: false, autoIncrement: false, nullable: true, unique: false, columnType: character varying, length: -5, comment: , defaultValue: opc, decimalSize: { Precision: 0, Scale: 0 } }, { goType: int64, tableName: plat_menu, tableSchema: , columnName: parent_id, dbTypeName: int8, primaryKey: false, autoIncrement: false, nullable: true, unique: false, columnType: bigint, length: 64, comment: 父节点, defaultValue: -1, decimalSize: { Precision: 64, Scale: 0 } }, { goType: int32, tableName: plat_menu, tableSchema: , columnName: page_type, dbTypeName: int4, primaryKey: false, autoIncrement: false, nullable: true, unique: false, columnType: integer, length: 32, comment: 页面组件类型10 菜单 20按扭, defaultValue: 10, decimalSize: { Precision: 32, Scale: 0 } } ], pkInfo: [ { goType: int64, tableName: plat_menu, tableSchema: , columnName: id, dbTypeName: int8, primaryKey: true, autoIncrement: true, nullable: false, unique: false, columnType: bigint, length: 64, comment: , defaultValue: , decimalSize: { Precision: 64, Scale: 0 } } ] }, total: 0, pageSize: 0, pageCurrent: 0 } 代码package dbmeta import ( gitee.com/leijmdas/goweb3/goconfig/base/basedto gitee.com/leijmdas/goweb3/goconfig/dbcontent gitee.com/leijmdas/goweb3/goconfig/ichublog/golog gitee.com/leijmdas/goweb3/goweb/pagemodel _ gorm.io/driver/mysql _ gorm.io/driver/postgres gorm.io/gorm gorm.io/gorm/schema ) type DbmetaFacade struct { basedto.BaseEntitySingle Dbtables } func NewDbmetaFacade() *DbmetaFacade { return DbmetaFacade{} } type Dbtables struct { basedto.BaseEntitySingle TableList []string json:tableList ColumnsMap map[string][]gorm.ColumnType json:columnsMap Columns []gorm.ColumnType json:columns TableName string json:tableName } func NewDbtables() *Dbtables { return Dbtables{} } func (self *DbmetaFacade) Init() { if self.ColumnsMap nil { self.ColumnsMap make(map[string][]gorm.ColumnType) } if self.Columns nil { self.Columns make([]gorm.ColumnType, 0) } } func (self *DbmetaFacade) GetDbTable(table any) (err error) { self.TableList, err self.GetTables() if err ! nil { return err } self.Columns, err self.ColumnTypes(table) return nil } func (self *DbmetaFacade) GetDbTables() (err error) { var dbs dbcontent.GetDB().Migrator().CurrentDatabase() golog.Error(dbs) self.TableList, err self.GetTables() if err ! nil { return err } self.Init() for i : range self.TableList { var columns, err self.ColumnTypes(self.TableList[i]) if err ! nil { return err } self.ColumnsMap[self.TableList[i]] columns } return err } func (self *DbmetaFacade) GetTables() ([]string, error) { tableList, err : dbcontent.GetDB().Migrator().GetTables() return tableList, err } func (self *DbmetaFacade) ColumnTypes(dst any) ([]gorm.ColumnType, error) { columnTypes, err : dbcontent.GetDB().Migrator().ColumnTypes(dst) return columnTypes, err } func (self *DbmetaFacade) GetDbMeta(paramTable any) *pagemodel.IchubResult[*DbmetaTable] { var table FindBeanDbmetaTable() //if tab, err : self.GetTables(); err ! nil { // golog.Error(err,tab) // return pagemodel.ResultErrData(tables, err) //} if tab, ok : paramTable.(string); ok { table.TableName tab } else if tab, ok : paramTable.(schema.Tabler); ok { table.TableName tab.TableName() } var columns, err self.ColumnTypes(paramTable) if err ! nil { golog.Error(err, columns) return pagemodel.ResultErrData(table, err) } for _, column : range columns { var dbcol FindBeanDbmetaColumn() dbcol.TableName table.TableName dbcol.FromColumnType(column) if dbcol.PrimaryKey { table.PkColumns append(table.PkColumns, dbcol) } table.Columns append(table.Columns, dbcol) } return pagemodel.ResultOk(table, true) }package dbmeta import ( gitee.com/leijmdas/goweb3/goconfig/base/baseiface gitee.com/leijmdas/goweb3/goconfig/basedi github.com/sirupsen/logrus ) /* Title 文件名称: dbmeta_column_init.go Desp 描述: 依赖自动注入 Company 公司: www.ichub.com Author 作者: leijmdas163.com 时间: 2026-03-15 12:51:06 Update 作者: leijmdas163.com 时间: 2026-03-15 12:51:06 */ var singleNameDbmetaColumn *dbmeta.DbmetaColumn-9d032f30-4763-45a5-9c04-1a27902a7995 // init register load func init() { registerBeanDbmetaColumn() } // register DbmetaColumn func registerBeanDbmetaColumn() { err : basedi.RegisterLoadBean(singleNameDbmetaColumn, LoadDbmetaColumn) if err ! nil{ logrus.Error(register bean error!, err) } } // FindBeanDbmetaColumn func FindBeanDbmetaColumn() *DbmetaColumn { if bean, ok : basedi.FindBeanOk(singleNameDbmetaColumn); ok { return bean.(*DbmetaColumn) } logrus.Error(not find bean!) return nil } func LoadDbmetaColumn() baseiface.ISingleton { var inst NewDbmetaColumn() InjectDbmetaColumn(inst) return inst } func InjectDbmetaColumn(s *DbmetaColumn) { }package dbmeta import ( strings gitee.com/leijmdas/goweb3/goconfig/base/basedto gitee.com/leijmdas/goweb3/goconfig/base/stringutils github.com/samber/lo ) type DbmetaTable struct { basedto.BaseEntity TableExist bool json:tableExist TableType string json:tableType TableSchema string json:tableSchema gorm:column:table_schema TableName string json:tableName gorm:column:table_name IndexName string json:indexName FieldsName string json:fieldsName Columns []*DbmetaColumn json:columns PkColumns []*DbmetaColumn json:pkInfo } func NewDbmetaTable() *DbmetaTable { var metaTable DbmetaTable{ PkColumns: make([]*DbmetaColumn, 0), Columns: make([]*DbmetaColumn, 0), } metaTable.InitProxy(metaTable) return metaTable } func (self *DbmetaTable) IfPkColumnExists() bool { return self.PkColumns ! nil len(self.PkColumns) 0 } func (self *DbmetaTable) IfView() bool { return self.TableType VIEW } func (self *DbmetaTable) IfFieldNameExists() bool { return self.FieldsName ! } func (self *DbmetaTable) IfFieldExists(field string) bool { var _, ok self.FindColumn(field) return ok } func (self *DbmetaTable) FindColumn(field string) (*DbmetaColumn, bool) { return lo.Find(self.Columns, func(item *DbmetaColumn) bool { return item.ColumnName field }) } func (self *DbmetaTable) ToFieldsName() string { var fields lo.Map(self.Columns, func(item *DbmetaColumn, index int) string { return item.ColumnName }) self.FieldsName strings.Join(fields, ,) return self.FieldsName } func (self *DbmetaTable) FindGoField(propName string) (*DbmetaColumn, bool) { return self.FindColumn(stringutils.Camel2Case(propName)) } func (self *DbmetaTable) FindGoType(propName string) string { var goField, ok self.FindGoField(propName) return lo.Ternary(ok, goField.GoType, string) }package dbmeta import ( gitee.com/leijmdas/goweb3/goconfig/base/basedto github.com/gogf/gf/v2/util/gconv gorm.io/gorm ) type DecimalSize struct { Precision int64 Scale int64 } type DbmetaColumn struct { basedto.BaseEntity GoType string json:goType,gorm:- TableName string json:tableName,gorm:column:table_name TableSchema string json:tableSchema,gorm:column:table_schema ColumnName string json:columnName,gorm:column:column_name DbTypeName string json:dbTypeName,gorm:column:dbTypeName // varchar PrimaryKey bool json:primaryKey,gorm:column:primary_key AutoIncrement bool json:autoIncrement,gorm:column:auto_increment Nullable bool json:nullable,gorm:column:nullable Unique bool json:unique,gorm:column:unique ColumnType string json:columnType,gorm:column:column_type // varchar(64) Length int64 json:length,gorm:column:length Comment string json:comment ,gorm:column:comment DefaultValue string json:defaultValue ,gorm:column:default_value DecimalSize DecimalSize json:decimalSize,gorm:column:decimal_size } func NewDbmetaColumn() *DbmetaColumn { var col DbmetaColumn{} col.InitProxy(col) return col } func (self *DbmetaColumn) FromColumnType(column gorm.ColumnType) *DbmetaColumn { self.ColumnName column.Name() self.GoType gconv.String(column.ScanType()) self.DbTypeName column.DatabaseTypeName() // varchar self.ColumnType, _ column.ColumnType() // varchar(64) self.Length, _ column.Length() self.Comment, _ column.Comment() self.DefaultValue, _ column.DefaultValue() self.DecimalSize.Precision, self.DecimalSize.Scale, _ column.DecimalSize() self.PrimaryKey, _ column.PrimaryKey() self.Nullable, _ column.Nullable() self.Unique, _ column.Unique() self.AutoIncrement, _ column.AutoIncrement() return self }
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2413833.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!