Use model's TableName() in Cols for dynamic table names

This commit is contained in:
2026-03-29 15:43:42 +02:00
parent 97e6f82737
commit f6506584d1
5 changed files with 36 additions and 40 deletions

View File

@@ -1,28 +0,0 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package dbmodel
import "git.ma-al.com/goc_marek/gormcol"
const TableNamePsAccess = "ps_access"
// PsAccess mapped from table <ps_access>
type PsAccess struct {
IDProfile int32 `gorm:"column:id_profile;primaryKey" json:"id_profile"`
IDAuthorizationRole int32 `gorm:"column:id_authorization_role;primaryKey" json:"id_authorization_role"`
}
// TableName PsAccess's table name
func (*PsAccess) TableName() string {
return TableNamePsAccess
}
var PsAccessCols = struct {
IDProfile gormcol.Field
IDAuthorizationRole gormcol.Field
}{
IDProfile: gormcol.Field{Column: "id_profile"},
IDAuthorizationRole: gormcol.Field{Column: "id_authorization_role"},
}

View File

@@ -0,0 +1,31 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package dbmodel
import "git.ma-al.com/goc_marek/gormcol"
const TableNamePsModuleShop = "ps_module_shop"
// PsModuleShop mapped from table <ps_module_shop>
type PsModuleShop struct {
IDModule int32 `gorm:"column:id_module;primaryKey" json:"id_module"`
IDShop int32 `gorm:"column:id_shop;primaryKey;index:id_shop,priority:1" json:"id_shop"`
EnableDevice bool `gorm:"column:enable_device;not null;default:7" json:"enable_device"`
}
// TableName PsModuleShop's table name
func (*PsModuleShop) TableName() string {
return TableNamePsModuleShop
}
var PsModuleShopCols = struct {
IDModule gormcol.Field
IDShop gormcol.Field
EnableDevice gormcol.Field
}{
IDModule: gormcol.Field{Table: (PsModuleShop{}).TableName(), Column: "id_module"},
IDShop: gormcol.Field{Table: (PsModuleShop{}).TableName(), Column: "id_shop"},
EnableDevice: gormcol.Field{Table: (PsModuleShop{}).TableName(), Column: "enable_device"},
}

View File

@@ -175,7 +175,7 @@ func generateColsVarBlock(si *structInfo) string {
} }
b.WriteString("}{\n") b.WriteString("}{\n")
for _, f := range si.Fields { for _, f := range si.Fields {
b.WriteString(fmt.Sprintf("\t%s: gormcol.Field{Column: %q},\n", f.GoName, f.ColName)) b.WriteString(fmt.Sprintf("\t%s: gormcol.Field{Table: (%s{}).TableName(), Column: %q},\n", f.GoName, si.Name, f.ColName))
} }
b.WriteString("}\n") b.WriteString("}\n")
return b.String() return b.String()

BIN
gormcol

Binary file not shown.

View File

@@ -9,26 +9,19 @@
// IDProfile gormcol.Field // IDProfile gormcol.Field
// IDAuthorizationRole gormcol.Field // IDAuthorizationRole gormcol.Field
// }{ // }{
// IDProfile: gormcol.Field{Column: "id_profile"}, // IDProfile: gormcol.Field{Table: (PsAccess{}).TableName(), Column: "id_profile"},
// IDAuthorizationRole: gormcol.Field{Column: "id_authorization_role"}, // IDAuthorizationRole: gormcol.Field{Table: (PsAccess{}).TableName(), Column: "id_authorization_role"},
// } // }
//
// Then in queries, use the model's TableName():
//
// PsAccess{}.TableName() + "." + gormcol.Column(PsAccessCols.IDProfile) // "ps_access.id_profile"
package gormcol package gormcol
// Field represents a GORM column descriptor. // Field represents a GORM column descriptor.
// It holds only the column name; use the model's TableName() for table qualification. // Table should be set using the model's TableName() function for type safety.
type Field struct { type Field struct {
Table string // Database table name (use model's TableName())
Column string // Database column name Column string // Database column name
} }
// Column returns the column name from a Field descriptor. // Column returns the column name from a Field descriptor.
//
// Example:
//
// gormcol.Column(dbmodel.PsAccessCols.IDAuthorizationRole) // "id_authorization_role"
func Column(f Field) string { func Column(f Field) string {
return f.Column return f.Column
} }