当前位置: 首页 > news >正文

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 作者: leijmdas@163.com 时间: 2026-03-15 12:51:06 @Update 作者: leijmdas@163.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.jsqmd.com/news/481289/

相关文章:

  • 汽车制造经验:JS如何基于百度WebUploader插件实现设计图纸的加密分片断传?
  • 2026年湘菜加盟品牌怎么选,排行榜前十名有答案 - myqiye
  • 航空航天需求:Vue3如何扩展百度WebUploader支持卫星遥感数据的分片校验上传?
  • 深聊上海专业的企业法律律师咨询,推荐优质企业法律团队 - 工业推荐榜
  • 2026年GEO推广服务费用大揭秘,南京哪家性价比高 - 工业品网
  • 2026年全国风机品牌推荐,北京艾尔凡机电设备公司口碑靠谱吗 - mypinpai
  • 盘点味派菜园子加盟政策,全国靠谱的加盟公司怎么选? - myqiye
  • 分析2026年哈尔滨公考培训服务哪家好,友恒公考 - 工业设备
  • 中空板定制性能哪家好,南京扬旺值得选吗? - 工业品牌热点
  • 问卷调研的水有多深?13年从业者揭秘假数据背后的灰色产业链 - ui设计公司兰亭妙微
  • 解读埃斯顿协作机器人厂商,口碑好的是哪家? - 工业推荐榜
  • 北京/上海/深圳/杭州/南京/无锡高端腕表维修性价比指南,36品牌分预算解决方案+正规门店适配 - 时光修表匠
  • 总结2026年GEO推广创新模式,看看哪家效果好 - 工业设备
  • 五轴高速机多少钱,华南地区有哪些性价比高的品牌推荐? - 工业品牌热点
  • 探寻2026年江苏精密钣金制作优质供应商,口碑哪家好 - 工业品网
  • 最近在折腾步科触摸屏和台达VFD-M变频器的通讯项目,实测走通了一套控制方案。先给大伙儿分享下关键配置和脚本实现,手头有设备的可以直接抄作业
  • 卸灰阀、星型卸料阀、旋转卸料阀cad总装配图纸
  • 【IT老齐245 笔记 + 思考】综合对比九种 MySQL 高可用方案
  • 靳金萍
  • Ubuntu 22.04 局域网安装GitLab
  • day10
  • Redis 集群从裸奔到全副武装:搭建、可视化、监控、告警、看板一条龙
  • 毕设程序java车辆保养管理平台 基于SpringBoot的汽车养护服务系统 智慧车辆维保一体化平台
  • 揭秘团团圆家具价格,其客户评价和市场口碑哪个好? - 工业推荐榜
  • 导师推荐 10个一键生成论文工具:本科生毕业论文+开题报告写作全测评
  • 织带来样定制费用多少,恒信服饰的口碑咋样? - 工业品网
  • 2026年埃斯顿机器人价格盘点,生产厂哪家技术强有答案 - mypinpai
  • 毕设程序java车险理赔管理系统 基于SpringBoot的车辆保险智能定损与理赔平台 汽车保险全流程数字化管理与在线理赔系统
  • 医疗系统如何开发KindEditor的截图OCR智能识别?
  • 计算机毕业设计 java 养老院管理系统 Java+SpringBoot 智慧养老服务平台 Web 版养老院综合管理系统