diff --git a/app/model/dbmodel/ps_module_shop.go b/app/model/dbmodel/ps_module_shop.go index a200d7a..67d185b 100644 --- a/app/model/dbmodel/ps_module_shop.go +++ b/app/model/dbmodel/ps_module_shop.go @@ -25,7 +25,7 @@ var PsModuleShopCols = struct { 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"}, + IDModule: gormcol.Field{}.Set((&PsModuleShop{}).TableName(), "id_module"), + IDShop: gormcol.Field{}.Set((&PsModuleShop{}).TableName(), "id_shop"), + EnableDevice: gormcol.Field{}.Set((&PsModuleShop{}).TableName(), "enable_device"), } diff --git a/cmd/gormcol/main.go b/cmd/gormcol/main.go index fb6850c..c657bef 100644 --- a/cmd/gormcol/main.go +++ b/cmd/gormcol/main.go @@ -11,6 +11,7 @@ import ( "time" "git.ma-al.com/goc_marek/gormcol" + "git.ma-al.com/goc_marek/gormcol/app/model/dbmodel" "github.com/joho/godotenv" "gorm.io/gorm" ) @@ -19,6 +20,9 @@ import ( // It parses flags, loads configuration from .env, connects to the database, // and generates GORM models with column descriptors. func main() { + + fmt.Printf("gormcol.Column(dbmodel.PsModuleShop.IDModule): %v\n", dbmodel.PsModuleShopCols.EnableDevice.TabCol()) + dsn := flag.String("dsn", "", "database DSN (e.g. user:pass@tcp(host:3306)/dbname)") filter := flag.String("filter", "", "regex to match table names (triggers batch mode)") all := flag.Bool("all", false, "generate all tables matching filter (shows confirmation)") diff --git a/gencols.go b/gencols.go index 1f0acec..e7da424 100644 --- a/gencols.go +++ b/gencols.go @@ -175,7 +175,7 @@ func generateColsVarBlock(si *structInfo) string { } b.WriteString("}{\n") for _, f := range si.Fields { - b.WriteString(fmt.Sprintf("\t%s: gormcol.Field{Table: (&%s{}).TableName(), Column: %q},\n", f.GoName, si.Name, f.ColName)) + b.WriteString(fmt.Sprintf("\t%s: gormcol.Field{}.Set((&%s{}).TableName(), %q),\n", f.GoName, si.Name, f.ColName)) } b.WriteString("}\n") return b.String() diff --git a/gormcol b/gormcol index aef64cf..4f8debf 100755 Binary files a/gormcol and b/gormcol differ diff --git a/gormcol.go b/gormcol.go index 2909932..acadde7 100644 --- a/gormcol.go +++ b/gormcol.go @@ -9,19 +9,33 @@ // IDProfile gormcol.Field // IDAuthorizationRole gormcol.Field // }{ -// IDProfile: gormcol.Field{Table: (PsAccess{}).TableName(), Column: "id_profile"}, -// IDAuthorizationRole: gormcol.Field{Table: (PsAccess{}).TableName(), Column: "id_authorization_role"}, +// IDProfile: gormcol.Field{}.Set((&PsAccess{}).TableName(), "id_profile"), +// IDAuthorizationRole: gormcol.Field{}.Set((&PsAccess{}).TableName(), "id_authorization_role"), // } package gormcol // Field represents a GORM column descriptor. // Table should be set using the model's TableName() function for type safety. type Field struct { - Table string // Database table name (use model's TableName()) - Column string // Database column name + table string // Database table name (use model's TableName()) + column string // Database column name } -// Column returns the column name from a Field descriptor. -func Column(f Field) string { - return f.Column +func (f Field) TabCol() string { + return f.table + "." + f.column +} + +func (f Field) Col() string { + return f.column +} + +func (f Field) Tab() string { + return f.table +} + +func (f Field) Set(tab, field string) Field { + return Field{ + table: tab, + column: field, + } }