drivers

package
v0.30.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2025 License: MIT Imports: 10 Imported by: 1

Documentation

Overview

Package drivers talks to various database backends and retrieves table, column, type, and foreign key information

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColumnDBTypes

func ColumnDBTypes(cols []Column) map[string]string

ColumnDBTypes of the columns.

func ColumnNames

func ColumnNames(cols []Column) []string

ColumnNames of the columns.

func ColumnsFromList

func ColumnsFromList(list []string, tablename string) []string

ColumnsFromList takes a whitelist or blacklist and returns the columns for a given table.

func Skip added in v0.15.0

func Skip(name string, include, exclude []string) bool

Types

type Aliases added in v0.29.0

type Aliases map[string]TableAlias

Aliases defines aliases for the generation run

func (Aliases) Table added in v0.29.0

func (a Aliases) Table(table string) TableAlias

Table gets a table alias, panics if not found.

type Check added in v0.29.0

type Check[Extra any] struct {
	Constraint[Extra] `yaml:",inline" json:"-"`
	Expression        string `yaml:"expression" json:"expression"`
}

Check represents a check constraint in a database

func (Check[E]) MarshalJSON added in v0.29.0

func (c Check[E]) MarshalJSON() ([]byte, error)

func (*Check[E]) UnmarshalJSON added in v0.29.0

func (c *Check[E]) UnmarshalJSON(data []byte) error

type Column

type Column struct {
	Name      string `json:"name" yaml:"name"`
	DBType    string `json:"db_type" yaml:"db_type"`
	Default   string `json:"default" yaml:"default"`
	Comment   string `json:"comment" yaml:"comment"`
	Nullable  bool   `json:"nullable" yaml:"nullable"`
	Generated bool   `json:"generated" yaml:"generated"`
	AutoIncr  bool   `json:"autoincr" yaml:"autoincr"`

	// DomainName is the domain type name associated to the column. See here:
	// https://www.postgresql.org/docs/16/extend-type-system.html
	DomainName string `json:"domain_name" yaml:"domain_name"`

	Type string `json:"type" yaml:"type"`
}

Column holds information about a database column. Types are Go types, converted by TranslateColumnType.

type ColumnFilter

type ColumnFilter map[string]Filter

func ParseColumnFilter

func ParseColumnFilter(tables []string, only, except map[string][]string) ColumnFilter

type Constraint

type Constraint[Extra any] struct {
	Name    string   `yaml:"name" json:"name"`
	Columns []string `yaml:"columns" json:"columns"`
	Comment string   `json:"comment" yaml:"comment"`
	Extra   Extra    `yaml:"extra" json:"extra"`
}

Constraint represents a constraint in a database

type Constraints added in v0.23.0

type Constraints[Extra any] struct {
	Primary *Constraint[Extra]  `yaml:"primary" json:"primary"`
	Foreign []ForeignKey[Extra] `yaml:"foreign" json:"foreign"`
	Uniques []Constraint[Extra] `yaml:"uniques" json:"uniques"`
	Checks  []Check[Extra]      `yaml:"check" json:"check"`
}

type Constructor

type Constructor[ConstraintExtra, IndexExtra any] interface {
	// Load basic info about all tables
	TablesInfo(context.Context, Filter) (TablesInfo, error)
	// Load details about a single table
	TableDetails(ctx context.Context, info TableInfo, filter ColumnFilter) (schema, name string, _ []Column, _ error)
	// Load all table comments, keyed by TableInfo.Key
	Comments(ctx context.Context) (map[string]string, error)
	// Load all constraints in the database, keyed by TableInfo.Key
	Constraints(context.Context, ColumnFilter) (DBConstraints[ConstraintExtra], error)
	// Load all indexes in the database, keyed by TableInfo.Key
	Indexes(ctx context.Context) (DBIndexes[IndexExtra], error)
}

Constructor breaks down the functionality required to implement a driver such that the drivers.Tables method can be used to reduce duplication in driver implementations.

type DBConstraints

type DBConstraints[Extra any] struct {
	PKs     map[string]*Constraint[Extra]
	FKs     map[string][]ForeignKey[Extra]
	Uniques map[string][]Constraint[Extra]
	Checks  map[string][]Check[Extra]
}

type DBIndexes added in v0.28.0

type DBIndexes[Extra any] map[string][]Index[Extra]

DBIndexes lists all indexes in the database schema keyed by table name

type DBInfo

type DBInfo[DBExtra, ConstraintExtra, IndexExtra any] struct {
	Tables       Tables[ConstraintExtra, IndexExtra] `json:"tables"`
	QueryFolders []QueryFolder                       `json:"query_folders"`
	Enums        []Enum                              `json:"enums"`
	ExtraInfo    DBExtra                             `json:"extra_info"`
	// DriverName is the module name of the underlying `database/sql` driver
	DriverName string `json:"driver_name"`
}

DBInfo is the database's table data and dialect.

type Enum added in v0.15.0

type Enum struct {
	Type   string
	Values []string
}

type Filter

type Filter struct {
	Only   []string
	Except []string
}

func ParseTableFilter added in v0.15.0

func ParseTableFilter(only, except map[string][]string) Filter

func (Filter) ClassifyPatterns added in v0.29.0

func (f Filter) ClassifyPatterns(patterns []string) ([]string, []string)

type ForeignKey

type ForeignKey[Extra any] struct {
	Constraint[Extra] `yaml:",inline" json:"-"`
	ForeignTable      string   `yaml:"foreign_table" json:"foreign_table"`
	ForeignColumns    []string `yaml:"foreign_columns" json:"foreign_columns"`
}

ForeignKey represents a foreign key constraint in a database

func (ForeignKey[E]) MarshalJSON added in v0.29.0

func (f ForeignKey[E]) MarshalJSON() ([]byte, error)

func (*ForeignKey[E]) UnmarshalJSON added in v0.29.0

func (f *ForeignKey[E]) UnmarshalJSON(data []byte) error

type Importer added in v0.29.0

type Importer interface{ Import(...string) string }

type Index added in v0.28.0

type Index[Extra any] struct {
	Type    string        `yaml:"type" json:"type"`
	Name    string        `yaml:"name" json:"name"`
	Columns []IndexColumn `yaml:"columns" json:"columns"`
	Unique  bool          `yaml:"unique" json:"unique"`
	Comment string        `json:"comment" yaml:"comment"`
	Extra   Extra         `yaml:"extra" json:"extra"`
}

Index represents an index in a table

func (Index[E]) HasExpressionColumn added in v0.29.0

func (i Index[E]) HasExpressionColumn() bool

func (Index[E]) NonExpressionColumns added in v0.29.0

func (i Index[E]) NonExpressionColumns() []string

type IndexColumn added in v0.29.0

type IndexColumn struct {
	Name         string `yaml:"name" json:"name"`
	Desc         bool   `yaml:"desc" json:"desc"`
	IsExpression bool   `yaml:"is_expression" json:"is_expression"`
}

type Interface

type Interface[DBExtra, ConstraintExtra, IndexExtra any] interface {
	// The dialect
	Dialect() string
	// Assemble the database information into a nice struct
	Assemble(ctx context.Context) (*DBInfo[DBExtra, ConstraintExtra, IndexExtra], error)
	// Custom types defined by the driver
	Types() Types
}

Interface abstracts either a side-effect imported driver or a binary that is called in order to produce the data required for generation.

type Query added in v0.30.0

type Query struct {
	Name        string `yaml:"name"`
	SQL         string `yaml:"raw"`
	RowName     string `yaml:"row_name"`
	GenerateRow bool   `yaml:"generate_row"`

	Columns []QueryArg `yaml:"columns"`
	Args    []QueryArg `yaml:"args"`
}

type QueryArg added in v0.30.0

type QueryArg struct {
	Name     string `yaml:"name"`
	Nullable bool   `yaml:"nullable"`
	TypeName string `yaml:"type"`
	Refs     []Ref  `yaml:"refs"`
}

func (QueryArg) Type added in v0.30.0

func (c QueryArg) Type(db db) string

type QueryFile added in v0.30.0

type QueryFile struct {
	Path    string
	Queries []Query
}

type QueryFolder added in v0.30.0

type QueryFolder struct {
	Path  string
	Files []QueryFile
}

type Ref added in v0.30.0

type Ref struct {
	Key    string `yaml:"key"`
	Column string `yaml:"column"`
}

type Table

type Table[ConstraintExtra, IndexExtra any] struct {
	Key string `yaml:"key" json:"key"`
	// For dbs with real schemas, like Postgres.
	// Example value: "schema_name"."table_name"
	Schema      string                       `yaml:"schema" json:"schema"`
	Name        string                       `yaml:"name" json:"name"`
	Columns     []Column                     `yaml:"columns" json:"columns"`
	Indexes     []Index[IndexExtra]          `yaml:"indexes" json:"indexes"`
	Constraints Constraints[ConstraintExtra] `yaml:"constraints" json:"constraints"`
	Comment     string                       `json:"comment" yaml:"comment"`
}

Table metadata from the database schema.

func BuildDBInfo added in v0.15.0

func BuildDBInfo[DBExtra, ConstraintExtra, IndexExtra any](
	ctx context.Context, c Constructor[ConstraintExtra, IndexExtra],
	concurrency int, only, except map[string][]string,
) ([]Table[ConstraintExtra, IndexExtra], error)

This returns the metadata for all tables, minus the tables specified in the excludes.

func (Table[C, I]) CanSoftDelete

func (t Table[C, I]) CanSoftDelete(deleteColumn string) bool

func (Table[C, I]) DBTag added in v0.29.0

func (t Table[C, I]) DBTag(c Column) string

func (Table[C, I]) GetColumn

func (t Table[C, I]) GetColumn(name string) Column

GetColumn by name. Panics if not found (for use in templates mostly).

func (Table[C, I]) HasExactUnique added in v0.29.0

func (t Table[C, I]) HasExactUnique(cols ...string) bool

Returns true if the table has a unique constraint on exactly these columns

func (Table[C, I]) IsJoinTable

func (t Table[C, I]) IsJoinTable() bool

Used in templates to know if the given table is a join table for this relationship

func (Table[C, I]) IsJoinTableForRel added in v0.29.0

func (t Table[C, I]) IsJoinTableForRel(r orm.Relationship, position int) bool

Used in templates to know if the given table is a join table for this relationship

func (Table[C, I]) NonGeneratedColumns added in v0.22.0

func (t Table[C, I]) NonGeneratedColumns() []Column

func (Table[C, I]) RelIsRequired added in v0.29.0

func (tables Table[C, I]) RelIsRequired(rel orm.Relationship) bool

func (Table[C, I]) UniqueColPairs added in v0.29.0

func (t Table[C, I]) UniqueColPairs() string

type TableAlias added in v0.29.0

type TableAlias struct {
	UpPlural     string `yaml:"up_plural,omitempty" json:"up_plural,omitempty"`
	UpSingular   string `yaml:"up_singular,omitempty" json:"up_singular,omitempty"`
	DownPlural   string `yaml:"down_plural,omitempty" json:"down_plural,omitempty"`
	DownSingular string `yaml:"down_singular,omitempty" json:"down_singular,omitempty"`

	Columns       map[string]string `yaml:"columns,omitempty" json:"columns,omitempty"`
	Relationships map[string]string `yaml:"relationships,omitempty" json:"relationships,omitempty"`
}

TableAlias defines the spellings for a table name in Go

func (TableAlias) Column added in v0.29.0

func (t TableAlias) Column(column string) string

Column get's a column's aliased name, panics if not found.

func (TableAlias) Relationship added in v0.29.0

func (t TableAlias) Relationship(fkey string) string

Relationship looks up a relationship, panics if not found.

type TableInfo added in v0.15.0

type TableInfo struct {
	Key     string
	Schema  string
	Name    string
	Comment string
}

type Tables

type Tables[C, I any] []Table[C, I]

func (Tables[C, I]) ColumnGetter added in v0.29.0

func (tables Tables[C, I]) ColumnGetter(alias TableAlias, table, column string) string

func (Tables[C, I]) ColumnSetter added in v0.29.0

func (tables Tables[C, I]) ColumnSetter(table, column string) bool

func (Tables[C, I]) Get added in v0.29.0

func (tables Tables[C, I]) Get(name string) Table[C, I]

GetTable by name. Panics if not found (for use in templates mostly).

func (Tables[C, I]) GetColumn added in v0.29.0

func (tables Tables[C, I]) GetColumn(table string, column string) Column

func (Tables[C, I]) NeededBridgeRels added in v0.29.0

func (tables Tables[C, I]) NeededBridgeRels(r orm.Relationship) []struct {
	Table    string
	Position int
	Many     bool
}

func (Tables[C, I]) RelArgs added in v0.29.0

func (tables Tables[C, I]) RelArgs(aliases Aliases, r orm.Relationship) string

func (Tables[C, I]) RelDependencies added in v0.29.0

func (tables Tables[C, I]) RelDependencies(aliases Aliases, r orm.Relationship, preSuf ...string) string

func (Tables[C, I]) RelDependenciesPos added in v0.29.0

func (tables Tables[C, I]) RelDependenciesPos(aliases Aliases, r orm.Relationship) string

func (Tables[C, I]) RelDependenciesTyp added in v0.29.0

func (tables Tables[C, I]) RelDependenciesTyp(aliases Aliases, r orm.Relationship) string

func (Tables[C, I]) RelDependenciesTypSet added in v0.29.0

func (tables Tables[C, I]) RelDependenciesTypSet(aliases Aliases, r orm.Relationship) string

func (Tables[C, I]) RelIsView added in v0.29.0

func (tables Tables[C, I]) RelIsView(rel orm.Relationship) bool

func (Tables[C, I]) SetFactoryDeps added in v0.29.0

func (tables Tables[C, I]) SetFactoryDeps(i Importer, aliases Aliases, r orm.Relationship, inLoop bool) string

type TablesInfo added in v0.15.0

type TablesInfo []TableInfo

func (TablesInfo) Keys added in v0.15.0

func (t TablesInfo) Keys() []string

type Type added in v0.24.0

type Type struct {
	// If this type is an alias of another type
	// this is useful to have custom randomization for a type e.g. xml
	AliasOf string `yaml:"alias_of"`
	// Imports needed for the type
	Imports importers.List `yaml:"imports"`
	// Any other types that this type depends on
	DependsOn []string `yaml:"depends_on"`
	// To be used in factory.random_type
	// a variable `f` of type `faker.Faker` is available
	RandomExpr string `yaml:"random_expr"`
	// Additional imports for the randomize expression
	RandomExprImports importers.List `yaml:"random_expr_imports"`
	// Set this to true if the randomization should not be tested
	// this is useful for low-cardinality types like bool
	NoRandomizationTest bool `yaml:"no_randomization_test"`
	// Set this to true if the test to see if the type implements
	// the scanner and valuer interfaces should be skipped
	// this is useful for types that are based on a primitive type
	NoScannerValuerTest bool `yaml:"no_scanner_valuer_test"`
	// CompareExpr is used to compare two values of this type
	// if not provided, == is used
	// Used AAA and BBB as placeholders for the two values
	CompareExpr string `yaml:"compare_expr"`
	// Imports needed for the compare expression
	CompareExprImports importers.List `yaml:"compare_expr_imports"`
}

type Types added in v0.24.0

type Types map[string]Type

Jump to

Keyboard shortcuts

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