Documentation
¶
Overview ¶
Package schema provides struct reflection and model metadata caching for Quark ORM. It parses Go struct tags (db, pk, rel, join) and caches the result using sync.Map to ensure O(1) lookups after the first access per model type.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ToSnakeCase ¶
ToSnakeCase converts CamelCase to snake_case, intelligently handling acronyms.
Types ¶
type FieldMeta ¶
type FieldMeta struct {
Index int
Column string // value of the db:"" tag
Kind reflect.Kind
Type reflect.Type
IsPK bool
OldColumn string // for renames
NotNull bool // from tag: quark:"not_null" or nullable:"false"
Default string // from tag: default:"value"
Unique bool // from tag: quark:"unique"
}
FieldMeta holds metadata about a single struct field.
type ModelMeta ¶
type ModelMeta struct {
Table string
PK PKMeta
HasPK bool
CompositePK []PKMeta // populated when two or more fields carry pk:"true"
HasCompositePK bool // true when len(CompositePK) > 1
Fields []FieldMeta
FieldByCol map[string]*FieldMeta // lookup by db column name
Relations map[string]*RelationMeta // lookup by field name
}
ModelMeta holds cached metadata about a model struct. Computed once per type and stored in a global registry.
func GetModelMeta ¶
GetModelMeta returns the cached metadata for model type T. If not cached, it computes and stores it.
func GetModelMetaByType ¶
GetModelMetaByType returns the cached metadata for a reflect.Type.
type PKMeta ¶
PKMeta holds primary key metadata for a single PK column.
func FindPK ¶
FindPK finds the primary key field in a struct value. It first looks for a pk:"true" tag, then falls back to db:"id". When multiple fields carry pk:"true" the first one is returned for backward-compatibility; use FindPKs to obtain all of them.
func FindPKs ¶
FindPKs returns all primary key fields from a struct value. Fields tagged with pk:"true" are returned in declaration order. When no pk:"true" tag is present it falls back to the single db:"id" field.
func (PKMeta) IsComposite ¶
IsComposite returns true when the model uses a multi-column primary key. Use ModelMeta.CompositePK instead of ModelMeta.PK when this is true.
type RelationMeta ¶
type RelationMeta struct {
Type string // "has_one", "has_many", "belongs_to", "m2m", "polymorphic"
Field string // struct field name
JoinCol string // foreign key column (for belongs_to, has_one, has_many)
JoinTable string // join table name (for m2m)
JoinFK string // foreign key in join table pointing to this model (for m2m)
JoinRefFK string // foreign key in join table pointing to related model (for m2m)
PolyType string // polymorphic type identifier (for polymorphic)
PolyTypeColumn string // column storing the polymorphic type (for polymorphic)
PolyIDColumn string // column storing the polymorphic foreign key (for polymorphic)
RefType reflect.Type // type of the related model (the struct type)
IsSlice bool // true for has_many, m2m
}
RelationMeta holds metadata about a model relation.
type TableNamer ¶
type TableNamer interface {
TableName() string
}
TableNamer interface for custom table names.