db

package
v0.0.0-...-00587b8 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2020 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeColumn = iota
	TypeSlice
	TypeMap
)

Possible type code.

Variables

View Source
var Imports = []string{
	"database/sql",
	"fmt",
	"github.com/lxc/lxd/lxd/db/cluster",
	"github.com/lxc/lxd/lxd/db/query",
	"github.com/lxc/lxd/shared/api",
	"github.com/pkg/errors",
}

Imports is a list of the package imports every generated source file has.

Functions

func Criteria

func Criteria(pkg *ast.Package, entity string) ([]string, error)

Criteria returns a list of criteria

func FieldArgs

func FieldArgs(fields []*Field) string

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

func FieldColumns

func FieldColumns(fields []*Field) string

FieldColumns converts thegiven fields to list of column names separated by a comma.

func FieldCriteria

func FieldCriteria(fields []*Field) string

FieldCriteria converts the given fields to AND-separated WHERE criteria.

func FieldParams

func FieldParams(fields []*Field) string

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

func Filters

func Filters(pkg *ast.Package, entity string) [][]string

Filters parses all filtering statement defined for the given entity. It returns all supported combinations of filters, sorted by number of criteria.

func IsColumnType

func IsColumnType(name string) bool

IsColumnType returns true if the given type name is one mapping directly to a database column.

func Packages

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

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

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

func RefFilters

func RefFilters(pkg *ast.Package, entity string, ref string) [][]string

RefFilters 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) 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) 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.

func (*Field) ZeroValue

func (f *Field) ZeroValue() string

ZeroValue returns the literal representing the zero value for this field. The type code of the field must be TypeColumn.

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.
}

Mapping holds information for mapping database tables to a Go structure.

func Parse

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

Parse the structure declaration with the given name found in the given Go package.

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) 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) string

FieldColumnName returns the column name of the field with the given name, prefixed with the entity's table 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) 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.

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.

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.

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