Documentation ¶
Index ¶
- Constants
- Variables
- func FieldNames(fields []*Field) []string
- func FiltersFromStmt(pkg *packages.Package, kind string, entity string, filters []*Field) ([][]string, [][]string)
- func GetVars(pkg *packages.Package) map[string]*ast.ValueSpec
- func Packages() (map[string]*packages.Package, error)
- func ParsePackage(pkgPath string) (*packages.Package, error)
- func ParseStmt(pkg *packages.Package, dbPkg *packages.Package, name string) (string, error)
- func RefFiltersFromStmt(pkg *packages.Package, entity string, ref string, filters []*Field) ([][]string, [][]string)
- func UpdateSchema() error
- type Field
- func (f *Field) Column() string
- func (f *Field) InsertColumn(pkg *packages.Package, dbPkg *packages.Package, mapping *Mapping, ...) (column string, value string, err error)
- func (f *Field) IsIndirect() bool
- func (f *Field) IsPrimary() bool
- func (f *Field) IsScalar() bool
- func (f *Field) JoinClause(mapping *Mapping, table string) (string, error)
- func (f Field) JoinConfig() string
- func (f *Field) OrderBy(mapping *Mapping, primaryTable string) (string, error)
- func (f Field) SQLConfig() (tableName string, columnName string, err error)
- func (f Field) ScalarTableColumn() (tableName string, columnName string, err error)
- func (f *Field) SelectColumn(mapping *Mapping, primaryTable string) (string, error)
- func (f *Field) Stmt() string
- type Mapping
- func (m *Mapping) ActiveFilters(kind string) []*Field
- func (m *Mapping) ColumnFields(exclude ...string) []*Field
- func (m *Mapping) ContainsFields(fields []*Field) bool
- func (m *Mapping) FieldArgs(fields []*Field, extra ...string) string
- func (m *Mapping) FieldByName(name string) *Field
- func (m *Mapping) FieldColumnName(name string, table string) string
- func (m *Mapping) FieldParams(fields []*Field) string
- func (m *Mapping) FieldParamsMarshal(fields []*Field) string
- func (m *Mapping) FilterFieldByName(name string) (*Field, error)
- func (m *Mapping) Identifier() *Field
- func (m *Mapping) NaturalKey() []*Field
- func (m *Mapping) RefFields() []*Field
- func (m *Mapping) ScalarFields() []*Field
- func (m *Mapping) TableName(entity string, override string) string
- type Method
- type Stmt
- type TableType
- type Type
Constants ¶
const ( TypeColumn = iota TypeSlice TypeMap )
Possible type code.
Variables ¶
var AssociationTable = TableType(2)
AssociationTable represents the type for an entity that associates two other entities.
var EntityTable = TableType(0)
EntityTable represents the type for any entity that maps to a Go struct.
var Imports = []string{
"database/sql",
"fmt",
"github.com/canonical/lxd/lxd/db/query",
"github.com/canonical/lxd/shared/api",
}
Imports is a list of the package imports every generated source file has.
var MapTable = TableType(3)
MapTable represents the type for a table storing key/value pairs.
var ReferenceTable = TableType(1)
ReferenceTable represents the type for for any entity that contains an 'entity_id' field mapping to a parent entity.
Functions ¶
func FieldNames ¶
FieldNames returns the names of the given fields.
func FiltersFromStmt ¶
func FiltersFromStmt(pkg *packages.Package, kind string, entity string, filters []*Field) ([][]string, [][]string)
FiltersFromStmt parses all filtering statement defined for the given entity. It returns all supported combinations of filters, sorted by number of criteria, and the corresponding set of unused filters from the Filter struct.
func Packages ¶
Packages returns the AST packages in which to search for structs.
By default it includes the lxd/db and shared/api packages.
func ParsePackage ¶
ParsePackage returns the AST package in which to search for structs.
func ParseStmt ¶
ParseStmt returns the SQL string passed as an argument to a variable declaration of a call to RegisterStmt with the given name. e.g. the SELECT string from 'var instanceObjects = RegisterStmt(`SELECT * from instances...`)'.
func RefFiltersFromStmt ¶
func RefFiltersFromStmt(pkg *packages.Package, entity string, ref string, filters []*Field) ([][]string, [][]string)
RefFiltersFromStmt parses all filtering statement defined for the given entity reference.
func UpdateSchema ¶
func UpdateSchema() error
UpdateSchema updates the schema.go file of the cluster and node databases.
Types ¶
type Field ¶
type Field struct { Name string Type Type Primary bool // Whether this field is part of the natural primary key. Config url.Values }
Field holds all information about a field in a Go struct that is relevant for database code generation.
func (*Field) Column ¶
Column returns the name of the database column the field maps to. The type code of the field must be TypeColumn.
func (*Field) InsertColumn ¶
func (f *Field) InsertColumn(pkg *packages.Package, dbPkg *packages.Package, mapping *Mapping, primaryTable string) (column string, value string, err error)
InsertColumn returns a column name and parameter value suitable for an 'INSERT', 'UPDATE', or 'DELETE' statement. - If a 'join' tag is present, the package will be searched for the corresponding 'jointableID' registered statement to select the ID to insert into this table. - If a 'joinon' tag is present, but this table is not among the conditions, then the join will be considered indirect, and an empty string will be returned.
func (*Field) IsIndirect ¶
IsIndirect returns true if the field is a scalar column value from a joined table that in turn requires another join.
func (*Field) IsScalar ¶
IsScalar returns true if the field is a scalar column value from a joined table.
func (*Field) JoinClause ¶
JoinClause returns an SQL 'JOIN' clause using the 'join' and 'joinon' tags, if present.
func (Field) JoinConfig ¶
JoinConfig returns the `join` or `leftjoin` tag values.
func (Field) SQLConfig ¶
SQLConfig returns the table and column specified by the 'sql' config key, if present.
func (Field) ScalarTableColumn ¶
ScalarTableColumn gets the table and column from the join configuration.
func (*Field) SelectColumn ¶
SelectColumn returns a column name suitable for use with 'SELECT' statements. - Applies a `coalesce()` function if the 'coalesce' tag is present. - Returns the column in the form '<joinTable>.<joinColumn> AS <column>' if the `join` tag is present.
type Mapping ¶
type Mapping struct { Package string // Package of the Go struct Name string // Name of the Go struct. Fields []*Field // Metadata about the Go struct. Filterable bool // Whether the Go struct has a Filter companion struct for filtering queries. Filters []*Field // Metadata about the Go struct used for filter fields. Type TableType // Type of table structure for this Go struct. }
Mapping holds information for mapping database tables to a Go structure.
func Parse ¶
Parse the structure declaration with the given name found in the given Go package. Any 'Entity' struct should also have an 'EntityFilter' struct defined in the same file.
func (*Mapping) ActiveFilters ¶
ActiveFilters returns the active filter fields for the kind of method.
func (*Mapping) ColumnFields ¶
ColumnFields returns the fields that map directly to a database column, either on this table or on a joined one.
func (*Mapping) ContainsFields ¶
ContainsFields checks that the mapping contains fields with the same type and name of given ones.
func (*Mapping) FieldArgs ¶
FieldArgs converts the given fields to function arguments, rendering their name and type.
func (*Mapping) FieldByName ¶
FieldByName returns the field with the given name, if any.
func (*Mapping) FieldColumnName ¶
FieldColumnName returns the column name of the field with the given name, prefixed with the entity's table name.
func (*Mapping) FieldParams ¶
FieldParams converts the given fields to function parameters, rendering their name.
func (*Mapping) FieldParamsMarshal ¶
FieldParamsMarshal converts the given fields to function parameters, rendering their name. If the field is configured to marshal input/output, the name will be `marshaled{name}`.
func (*Mapping) FilterFieldByName ¶
FilterFieldByName returns the field with the given name if that field can be used as query filter, an error otherwise.
func (*Mapping) Identifier ¶
Identifier returns the field that uniquely identifies this entity.
func (*Mapping) NaturalKey ¶
NaturalKey returns the struct fields that can be used as natural key for uniquely identifying a row in the underlying table (==.
By convention the natural key field is the one called "Name", unless specified otherwise with the `db:natural_key` tags.
func (*Mapping) RefFields ¶
RefFields returns the fields that are one-to-many references to other tables.
func (*Mapping) ScalarFields ¶
ScalarFields returns the fields that map directly to a single database column on another table that can be joined to this one.
func (*Mapping) TableName ¶
TableName determines the table associated to the struct. - Individual fields may bypass this with their own `sql=<table>.<column>` tags. - The override `table=<name>` directive key is checked first. - The struct name itself is used to approximate the table name if none of the above apply.
type Method ¶
type Method struct {
// contains filtered or unexported fields
}
Method generates a code snippet for a particular database query method.
type Stmt ¶
type Stmt struct {
// contains filtered or unexported fields
}
Stmt generates a particular database query statement.
func NewStmt ¶
NewStmt return a new statement code snippet for running the given kind of query against the given database entity.