db

package
v0.0.0-...-a5439fb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2024 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeColumn = iota
	TypeSlice
	TypeMap
)

Possible type code.

Variables

View Source
var AssociationTable = TableType(2)

AssociationTable represents the type for an entity that associates two other entities.

View Source
var EntityTable = TableType(0)

EntityTable represents the type for any entity that maps to a Go struct.

View Source
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.

View Source
var MapTable = TableType(3)

MapTable represents the type for a table storing key/value pairs.

View Source
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

func FieldNames(fields []*Field) []string

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 GetVars

func GetVars(pkg *packages.Package) map[string]*ast.ValueSpec

GetVars gets all variable declarations in the given package, by name.

func Packages

func Packages() (map[string]*packages.Package, error)

Packages returns the AST packages in which to search for structs.

By default it includes the lxd/db and shared/api packages.

func ParsePackage

func ParsePackage(pkgPath string) (*packages.Package, error)

ParsePackage returns the AST package in which to search for structs.

func ParseStmt

func ParseStmt(pkg *packages.Package, dbPkg *packages.Package, name string) (string, error)

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

func (f *Field) Column() string

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

func (f *Field) IsIndirect() bool

IsIndirect returns true if the field is a scalar column value from a joined table that in turn requires another join.

func (*Field) IsPrimary

func (f *Field) IsPrimary() bool

IsPrimary returns true if the field part of the natural key.

func (*Field) IsScalar

func (f *Field) IsScalar() bool

IsScalar returns true if the field is a scalar column value from a joined table.

func (*Field) JoinClause

func (f *Field) JoinClause(mapping *Mapping, table string) (string, error)

JoinClause returns an SQL 'JOIN' clause using the 'join' and 'joinon' tags, if present.

func (Field) JoinConfig

func (f Field) JoinConfig() string

JoinConfig returns the `join` or `leftjoin` tag values.

func (*Field) OrderBy

func (f *Field) OrderBy(mapping *Mapping, primaryTable string) (string, error)

OrderBy returns a column name suitable for use with the 'ORDER BY' clause.

func (Field) SQLConfig

func (f Field) SQLConfig() (tableName string, columnName string, err error)

SQLConfig returns the table and column specified by the 'sql' config key, if present.

func (Field) ScalarTableColumn

func (f Field) ScalarTableColumn() (tableName string, columnName string, err error)

ScalarTableColumn gets the table and column from the join configuration.

func (*Field) SelectColumn

func (f *Field) SelectColumn(mapping *Mapping, primaryTable string) (string, error)

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.

func (*Field) Stmt

func (f *Field) Stmt() string

Stmt must be used only on a non-columnar field. It returns the name of statement that should be used to fetch this field. A statement with that name must have been generated for the entity at hand.

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

func Parse(pkg *packages.Package, name string, kind string) (*Mapping, error)

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

func (m *Mapping) ActiveFilters(kind string) []*Field

ActiveFilters returns the active filter fields for the kind of method.

func (*Mapping) ColumnFields

func (m *Mapping) ColumnFields(exclude ...string) []*Field

ColumnFields returns the fields that map directly to a database column, either on this table or on a joined one.

func (*Mapping) ContainsFields

func (m *Mapping) ContainsFields(fields []*Field) bool

ContainsFields checks that the mapping contains fields with the same type and name of given ones.

func (*Mapping) FieldArgs

func (m *Mapping) FieldArgs(fields []*Field, extra ...string) string

FieldArgs converts the given fields to function arguments, rendering their name and type.

func (*Mapping) FieldByName

func (m *Mapping) FieldByName(name string) *Field

FieldByName returns the field with the given name, if any.

func (*Mapping) FieldColumnName

func (m *Mapping) FieldColumnName(name string, table string) string

FieldColumnName returns the column name of the field with the given name, prefixed with the entity's table name.

func (*Mapping) FieldParams

func (m *Mapping) FieldParams(fields []*Field) string

FieldParams converts the given fields to function parameters, rendering their name.

func (*Mapping) FieldParamsMarshal

func (m *Mapping) FieldParamsMarshal(fields []*Field) string

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

func (m *Mapping) FilterFieldByName(name string) (*Field, error)

FilterFieldByName returns the field with the given name if that field can be used as query filter, an error otherwise.

func (*Mapping) Identifier

func (m *Mapping) Identifier() *Field

Identifier returns the field that uniquely identifies this entity.

func (*Mapping) NaturalKey

func (m *Mapping) NaturalKey() []*Field

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

func (m *Mapping) RefFields() []*Field

RefFields returns the fields that are one-to-many references to other tables.

func (*Mapping) ScalarFields

func (m *Mapping) ScalarFields() []*Field

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

func (m *Mapping) TableName(entity string, override string) string

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.

func NewMethod

func NewMethod(database, pkg, entity, kind string, config map[string]string) (*Method, error)

NewMethod return a new method code snippet for executing a certain mapping.

func (*Method) Generate

func (m *Method) Generate(buf *file.Buffer) error

Generate the desired method.

func (*Method) GenerateSignature

func (m *Method) GenerateSignature(buf *file.Buffer) error

GenerateSignature generates an interface signature for the method.

type Stmt

type Stmt struct {
	// contains filtered or unexported fields
}

Stmt generates a particular database query statement.

func NewStmt

func NewStmt(database, pkg, entity, kind string, config map[string]string) (*Stmt, error)

NewStmt return a new statement code snippet for running the given kind of query against the given database entity.

func (*Stmt) Generate

func (s *Stmt) Generate(buf *file.Buffer) error

Generate plumbing and wiring code for the desired statement.

func (*Stmt) GenerateSignature

func (s *Stmt) GenerateSignature(buf *file.Buffer) error

GenerateSignature is not used for statements.

type TableType

type TableType int

TableType represents the logical type of the table defined by the Go struct.

type Type

type Type struct {
	Name string
	Code int
}

Type holds all information about a field in a field type that is relevant for database code generation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL