Documentation ¶
Overview ¶
Provides SQL table metadata, enabling select field lists, easy getters, relations when using a query builder.
Index ¶
- Variables
- func MustParse(i interface{})
- func Parse(i interface{}) error
- type BelongsTo
- type BelongsToMany
- type BelongsToManyIDs
- type HasMany
- type HasOne
- type Meta
- func (m *Meta) For(i interface{}) *TableInfo
- func (m *Meta) ForName(name string) *TableInfo
- func (m *Meta) ForType(t reflect.Type) *TableInfo
- func (m *Meta) MustParse(i interface{})
- func (m *Meta) Parse(i interface{}) error
- func (m *Meta) ParseType(t reflect.Type) error
- func (m *Meta) ParseTypeNamed(t reflect.Type, name string) error
- func (m *Meta) ReplaceSQLNames(namer func(name string) string)
- func (m *Meta) SetTableInfo(ty reflect.Type, ti *TableInfo)
- type Relation
- type RelationMap
- type TableInfo
- func (ti *TableInfo) AddRelation(relation Relation) *TableInfo
- func (ti *TableInfo) GoPKFields() []string
- func (ti *TableInfo) GoType() reflect.Type
- func (ti *TableInfo) IsSQLPKField(sqlName string) bool
- func (ti *TableInfo) Name() string
- func (ti *TableInfo) PKAutoIncr() bool
- func (ti *TableInfo) PKValues(o interface{}) []interface{}
- func (ti *TableInfo) SQLFields(withPK bool) []string
- func (ti *TableInfo) SQLFieldsExcept(exceptFields ...string) []string
- func (ti *TableInfo) SQLName() string
- func (ti *TableInfo) SQLPKFields() []string
- func (ti *TableInfo) SQLPKWhere() string
- func (ti *TableInfo) SQLValueMap(o interface{}, includePks bool) map[string]interface{}
- func (ti *TableInfo) SQLVersionField() string
- func (ti *TableInfo) SetGoType(goType reflect.Type) *TableInfo
- func (ti *TableInfo) SetName(name string) *TableInfo
- func (ti *TableInfo) SetSQLName(sqlName string) *TableInfo
- func (ti *TableInfo) SetSQLPKFields(isAutoIncr bool, sqlPKFields []string) *TableInfo
- func (ti *TableInfo) SetSQLVersionField(sqlVersionField string) *TableInfo
Constants ¶
This section is empty.
Variables ¶
var DefaultMeta = NewMeta()
DefaultMeta is a global instance, for convenience.
Functions ¶
Types ¶
type BelongsTo ¶
type BelongsTo struct { Name string GoValueField string // e.g. "Author" (of type *Author) SQLIDField string // e.g. "author_id" }
BelongsTo is a relation for a single struct pointer where the ID of the linked row is stored on this table.
Example using struct tags:
type Book struct { // ... // This ID points to the row in the "author" table. AuthorID string `db:"author_id"` // This is the "author" relation that can be loaded from it. Author *Author `db:"-" tmeta:"belongs_to"` }
Full form with all options:
// The sql_id_field here must match the db struct tag in the field above. Author *Author `db:"-" tmeta:"belongs_to,relation_name=author,sql_id_field=author_id"`
No options are required except the relation type ("belongs_to").
func (*BelongsTo) RelationGoValueField ¶
func (*BelongsTo) RelationName ¶
type BelongsToMany ¶
type BelongsToMany struct { Name string GoValueField string // e.g. "BookLists" (of type []Book) JoinName string // the name of the join table (not necessarily the SQL name, it's Name()), e.g. "" SQLIDField string // SQL ID field on join table corresponding to this side SQLOtherIDField string // SQL ID field on join table corresponding to the other side }
BelongsToMany is a relation that uses a join table as a many to many relation.
Example using struct tags:
type Book struct { // ... CategoryList []Category `db:"-" tmeta:"belongs_to_many,join_name=book_category"` } // BookCategory is the join table. type BookCategory struct { BookID string `db:"book_id" tmeta:"pk"` CategoryID string `db:"category_id" tmeta:"pk"` } type Category struct { CategoryID string `db:"category_id" tmeta:"pk"` Name string `db:"name"` }
Full form with all options:
CategoryList []Category `db:"-" tmeta:"belongs_to_many,join_name=book_category,sql_id_field=book_id,sql_other_id_field=category_id"`
The join_name option is required.
func (*BelongsToMany) RelationGoValueField ¶
func (r *BelongsToMany) RelationGoValueField() string
func (*BelongsToMany) RelationName ¶
func (r *BelongsToMany) RelationName() string
type BelongsToManyIDs ¶
type BelongsToManyIDs struct { Name string GoValueField string // e.g. "BookIDList" (of type []string) JoinName string // the name of the join table (not necessarily the SQL name, it's Name()), e.g. "" SQLIDField string // SQL ID field on join table corresponding to this side SQLOtherIDField string // SQL ID field on join table corresponding to the other side }
BelongsToManyIDs is a relation that uses a join table as a many to many relation but stores the IDs in a slice instead of the instances directly. Useful for easily updating the join table.
Example using struct tags:
type Book struct { // ... CategoryIDList []string `db:"-" tmeta:"belongs_to_many_ids,join_name=book_category"` } // BookCategory is the join table. type BookCategory struct { BookID string `db:"book_id" tmeta:"pk"` CategoryID string `db:"category_id" tmeta:"pk"` } type Category struct { CategoryID string `db:"category_id" tmeta:"pk"` Name string `db:"name"` }
Full form with all options:
CategoryIDList []string `db:"-" tmeta:"belongs_to_many_ids,join_name=book_category,sql_id_field=book_id,sql_other_id_field=category_id"`
The join_name option is required.
func (*BelongsToManyIDs) RelationGoValueField ¶
func (r *BelongsToManyIDs) RelationGoValueField() string
func (*BelongsToManyIDs) RelationName ¶
func (r *BelongsToManyIDs) RelationName() string
type HasMany ¶
type HasMany struct { Name string GoValueField string // e.g. "Books" (of type []Book) SQLOtherIDField string // e.g. "author_id" - on the other table }
HasMany is a relation for a slice where the ID of the linked rows are stored on the other table.
Example using struct tags:
type Publisher struct { // ... BookList []Book `db:"-" tmeta:"has_many"` } type Book { // ... PublisherID string `db:"publisher_id"` }
Full form with all options:
// The sql_other_id_field here must match the ID field in the other table. BookList []Book `db:"-" tmeta:"has_many,relation_name=book_list,sql_other_id_field=publisher_id"`
No options are required except the relation type ("has_many").
func (*HasMany) RelationGoValueField ¶
func (*HasMany) RelationName ¶
type HasOne ¶
type HasOne struct { Name string GoValueField string // e.g. "CategoryInfo" (of type *CategoryInfo) SQLOtherIDField string // e.g. "category_id" - on the other table }
HasOne is a relation for a slice where the ID of the linked rows are stored on the other table.
Example using struct tags:
type Category struct { // ... CategoryInfo *CategoryInfo `db:"-" tmeta:"has_one"` } type CategoryInfo struct { // ... CategoryID string `db:"category_id"` // one to one relation }
Full form with all options:
CategoryInfo *CategoryInfo `db:"-" tmeta:"has_one,relation_name=category_info,sql_other_id_field=category_id"`
No options are required except the relation type ("has_one").
func (*HasOne) RelationGoValueField ¶
func (*HasOne) RelationName ¶
type Meta ¶
type Meta struct {
// contains filtered or unexported fields
}
Meta knows about your tables and the Go structs they correspond to.
func (*Meta) For ¶
For will return the TableInfo for a struct. Pointers will be dereferenced. Nil will be returned if no such table exists.
func (*Meta) ForName ¶
ForName will return the TableInfo with the given name. Nil will be returned if no such table exists.
func (*Meta) ForType ¶
For will return the TableInfo for a struct type. Pointers will be dereferenced. Nil will be returned if no such table exists.
func (*Meta) MustParse ¶
func (m *Meta) MustParse(i interface{})
MustParse is like Parse but will panic on error.
func (*Meta) Parse ¶
Parse will extract TableInfo data from the type of the value given (must be a properly tagged struct). The resulting TableInfo will be set as if by SetTableInfo.
func (*Meta) ParseType ¶
ParseType will extract TableInfo data from the given type (must be a properly tagged struct). The resulting TableInfo will be set as if by SetTableInfo.
func (*Meta) ParseTypeNamed ¶
ParseTypeNamed works like ParseType but allows you to specify the name rather than having it being derived from the name of the Go struct. This is intended to allow you to override an existing type with your own struct. Example: A package comes with a "Widget" type, named "widget", and you make a "CustomWidget" with whatever additional fields (optionally) embedding the original "Widget". You can then call meta.ParseTypeNamed(reflect.TypeOf(CustomWidget{}), "widget") to override the original definition.
func (*Meta) ReplaceSQLNames ¶
ReplaceSQLNames provides the SQLName of each table to a function and sets the table name to the return value. For example, you can easily prefix all of the tables by doing: m.ReplaceSQLNames(func(n string) string { return "prefix_" + n })
func (*Meta) SetTableInfo ¶
SetTableInfo assigns the TableInfo for a specific Go type, overwriting any existing value. This will remove/overwrite the name associated with that type as well, and will also remove any entry with the same name before setting. This behavior allows overrides where a package a default TableInfo can exist for a type but a specific usage requires it to be assigned differently.
type Relation ¶
Relation is implemented by the supported types of relations, BelongsTo, HasMany, etc.
type RelationMap ¶
RelationMap is a map of named relations to the relation data itself. Values must of one of the supported relation types.
func (RelationMap) CheckRelationNames ¶ added in v0.2.0
func (rm RelationMap) CheckRelationNames(names ...string) error
CheckRelationNames will return an error if any of the names you provide is not a valid relation name.
func (RelationMap) RelationNamed ¶
func (rm RelationMap) RelationNamed(n string) Relation
RelationNamed is a simple map lookup.
func (RelationMap) RelationTargetPtr ¶
func (rm RelationMap) RelationTargetPtr(o interface{}, n string) interface{}
RelationTargetPtr will find the named relation and use it's RelationGoValueField to obtain a pointer to the target field and return it. If this is not possible then nil is returned. If successful, the returned value will be a pointer to the type of the indicated field, e.g. if the field is of type `*Widget`, the return value will be of type `**Widget`, for type `[]Widget` it will return type `*[]Widget`.
type TableInfo ¶
type TableInfo struct { RelationMap // contains filtered or unexported fields }
TableInfo is the information for a single table.
func NewTableInfo ¶
NewTableInfo makes a new instance.
func (*TableInfo) AddRelation ¶
AddRelation adds a relation.
func (*TableInfo) GoPKFields ¶
GoPKFields returns the Go struct field name(s) of the primary key(s).
func (*TableInfo) IsSQLPKField ¶
IsSQLPKField returns true if the SQL field name provided is one of the primary key fields.
func (*TableInfo) PKAutoIncr ¶
PKAutoIncr returns true if the primary key is auto incrementing.
func (*TableInfo) PKValues ¶
func (ti *TableInfo) PKValues(o interface{}) []interface{}
PKValues scans an object for the pk fields. Will panic if pk fields cannot be found (e.g. if `o` is of the wrong type).
func (*TableInfo) SQLFields ¶
SQLFields returns the SQL field names for this table. If withPK is true the primary key(s) are included in the result.
func (*TableInfo) SQLFieldsExcept ¶ added in v0.2.0
SQLFieldsExcept returns the SQL field names for this table excluding the ones you provide.
func (*TableInfo) SQLPKFields ¶
SQLPKFields returns the SQL table field name(s) of the primary key(s).
func (*TableInfo) SQLPKWhere ¶
SQLPKWhere returns a where clause with the primary key fields ANDed together and "?" for placeholders. For example: "key1 = ? AND key2 = ?"
func (*TableInfo) SQLValueMap ¶
SQLValueMap returns a map of [SQLField]->[Value] for all database fields on this struct. If includePks is false then primary key fields are omitted.
func (*TableInfo) SQLVersionField ¶
SQLVersionField returns the SQL field name of the version field, empty string if the version field/optimistic locking is disabled.
func (*TableInfo) SetGoType ¶
SetGoType assigns the struct type. Must be of kind struct. The Name and SQLName will be assigned to the snake case name of the struct if not already set.
func (*TableInfo) SetName ¶
SetName sets the logical name of this table. The SQLName will be set if not already assigned.
func (*TableInfo) SetSQLName ¶
SetSQLName sets the SQL name of the table.
func (*TableInfo) SetSQLPKFields ¶
SetSQLPKFields sets the primary key fields.
func (*TableInfo) SetSQLVersionField ¶
SetSQLVersionField sets the version field.
Directories ¶
Path | Synopsis |
---|---|
Adapt tmeta to github.com/gocraft/dbr for easy query building.
|
Adapt tmeta to github.com/gocraft/dbr for easy query building. |
Utility stuff that goes along with tmeta but is not core functionality.
|
Utility stuff that goes along with tmeta but is not core functionality. |