Remove Table field from Cols, use GORM conventions
This commit is contained in:
46
gormcol.go
46
gormcol.go
@@ -1,23 +1,26 @@
|
||||
// Package gormcol provides type-safe GORM column descriptors.
|
||||
//
|
||||
// This package enables defining struct-like variables that map Go field names
|
||||
// to database table.column pairs, allowing type-safe queries in GORM.
|
||||
// to database column names, allowing type-safe queries in GORM.
|
||||
//
|
||||
// Example usage:
|
||||
//
|
||||
// var PsAccess = struct {
|
||||
// var PsAccessCols = struct {
|
||||
// IDProfile gormcol.Field
|
||||
// IDAuthorizationRole gormcol.Field
|
||||
// }{
|
||||
// IDProfile: gormcol.Field{Table: "ps_access", Column: "id_profile"},
|
||||
// IDAuthorizationRole: gormcol.Field{Table: "ps_access", Column: "id_authorization_role"},
|
||||
// IDProfile: gormcol.Field{Column: "id_profile"},
|
||||
// IDAuthorizationRole: gormcol.Field{Column: "id_authorization_role"},
|
||||
// }
|
||||
//
|
||||
// Then in queries, use the model's TableName():
|
||||
//
|
||||
// PsAccess{}.TableName() + "." + gormcol.Column(PsAccessCols.IDProfile) // "ps_access.id_profile"
|
||||
package gormcol
|
||||
|
||||
// Field represents a GORM column descriptor with table context.
|
||||
// It holds the table name and column name for type-safe column references.
|
||||
// Field represents a GORM column descriptor.
|
||||
// It holds only the column name; use the model's TableName() for table qualification.
|
||||
type Field struct {
|
||||
Table string // Database table name
|
||||
Column string // Database column name
|
||||
}
|
||||
|
||||
@@ -25,34 +28,7 @@ type Field struct {
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// gormcol.Column(dbmodel.PsAccess.IDAuthorizationRole) // "id_authorization_role"
|
||||
// gormcol.Column(dbmodel.PsAccessCols.IDAuthorizationRole) // "id_authorization_role"
|
||||
func Column(f Field) string {
|
||||
return f.Column
|
||||
}
|
||||
|
||||
// ColumnOnTable returns a qualified "table.column" string from a Field descriptor.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// gormcol.ColumnOnTable(dbmodel.PsAccess.IDAuthorizationRole) // "ps_access.id_authorization_role"
|
||||
func ColumnOnTable(f Field) string {
|
||||
return f.Table + "." + f.Column
|
||||
}
|
||||
|
||||
// TableField returns the table name from a Field descriptor.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// gormcol.TableField(dbmodel.PsAccess.IDAuthorizationRole) // "ps_access"
|
||||
func TableField(f Field) string {
|
||||
return f.Table
|
||||
}
|
||||
|
||||
// Table is an alias for TableField, returning the table name from a Field descriptor.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// gormcol.Table(dbmodel.PsAccess.IDAuthorizationRole) // "ps_access"
|
||||
func Table(f Field) string {
|
||||
return f.Table
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user