Documentation ¶
Overview ¶
Package drivers talks to various database backends and retrieves table, column, type, and foreign key information
Index ¶
- Constants
- func ColumnDBTypes(cols []Column) map[string]string
- func ColumnNames(cols []Column) []string
- func ColumnsFromList(list []string, tablename string) []string
- func DefaultEnv(key, def string) string
- func DriverExists(name string) bool
- func DriverMain(driver Interface)
- func RegisterBinary(name, path string)
- func RegisterFromInit(name string, driver Interface)
- func TablesFromList(list []string) []string
- type Column
- type ColumnList
- type Config
- type ConfigMap
- func (c ConfigMap) DefaultInt(key string, def int) int
- func (c ConfigMap) DefaultString(key, def string) string
- func (c ConfigMap) Int(key string) (int, bool)
- func (c ConfigMap) MustInt(key string) int
- func (c ConfigMap) MustString(key string) string
- func (c ConfigMap) String(key string) (string, bool)
- func (c ConfigMap) StringSlice(key string) ([]string, bool)
- type Constraint
- type ConstraintList
- func (cl ConstraintList) AllIndexed() ConstraintList
- func (cl ConstraintList) AllRequired() ConstraintList
- func (cl ConstraintList) Checks() ConstraintList
- func (cl ConstraintList) Constraint(columnName string) (Constraint, bool)
- func (cl ConstraintList) PrimaryKey() *PrimaryKey
- func (cl ConstraintList) Uniques() ConstraintList
- type Constructor
- type DBInfo
- type Dialect
- type ForeignKey
- type Interface
- type PrimaryKey
- type SQLColumnDef
- type SQLColumnDefs
- type Table
- type ToManyRelationship
- type ToOneRelationship
Constants ¶
const ( ConfigBlacklist = "blacklist" ConfigWhitelist = "whitelist" ConfigSchema = "schema" ConfigUser = "user" ConfigPass = "pass" ConfigHost = "host" ConfigPort = "port" ConfigDBName = "dbname" ConfigSSLMode = "sslmode" )
These constants are used in the config map passed into the driver
Variables ¶
This section is empty.
Functions ¶
func ColumnDBTypes ¶
ColumnDBTypes of the columns.
func ColumnsFromList ¶
ColumnsFromList takes a whitelist or blacklist and returns the columns for a given table.
func DefaultEnv ¶
DefaultEnv grabs a value from the environment or a default. This is shared by drivers to get config for testing.
func DriverMain ¶
func DriverMain(driver Interface)
DriverMain helps dry up the implementation of main.go for drivers
func RegisterBinary ¶
func RegisterBinary(name, path string)
RegisterBinary is used to register drivers that are binaries. Panics if a driver with the same name has been previously loaded.
func RegisterFromInit ¶
RegisterFromInit is typically called by a side-effect loaded driver during init time. Panics if a driver with the same name has been previously loaded.
func TablesFromList ¶
TablesFromList takes a whitelist or blacklist and returns the table names.
Types ¶
type Column ¶
type Column struct { Name string `json:"name" toml:"name" yaml:"name"` Type string `json:"type" toml:"type" yaml:"type"` DBType string `json:"db_type" toml:"db_type" yaml:"db_type"` Default string `json:"default" toml:"default" yaml:"default"` Nullable bool `json:"nullable" toml:"nullable" yaml:"nullable"` Unique bool `json:"unique" toml:"unique" yaml:"unique"` Validated bool `json:"validated" toml:"validated" yaml:"validated"` // Postgres only extension bits // ArrType is the underlying data type of the Postgres // ARRAY type. See here: // https://www.postgresql.org/docs/9.1/static/infoschema-element-types.html ArrType *string `json:"arr_type" toml:"arr_type" yaml:"arr_type"` UDTName string `json:"udt_name" toml:"udt_name" yaml:"udt_name"` // DomainName is the domain type name associated to the column. See here: // https://www.postgresql.org/docs/10/extend-type-system.html#EXTEND-TYPE-SYSTEM-DOMAINS DomainName *string `json:"domain_name" toml:"domain_name" yaml:"domain_name"` // MySQL only bits // Used to get full type, ex: // tinyint(1) instead of tinyint // Used for "tinyint-as-bool" flag FullDBType string `json:"full_db_type" toml:"full_db_type" yaml:"full_db_type"` // MS SQL only bits // Used to indicate that the value // for this column is auto generated by database on insert (i.e. - timestamp (old) or rowversion (new)) AutoGenerated bool `json:"auto_generated" toml:"auto_generated" yaml:"auto_generated"` }
Column holds information about a database column. Types are Go types, converted by TranslateColumnType.
func FilterColumnsByAuto ¶
FilterColumnsByAuto generates the list of columns that have autogenerated values
func FilterColumnsByDefault ¶
FilterColumnsByDefault generates the list of columns that have default values
func FilterColumnsByEnum ¶
FilterColumnsByEnum generates the list of columns that are enum values.
type ColumnList ¶
type ColumnList []Column
func (ColumnList) NonNulls ¶
func (cl ColumnList) NonNulls() ColumnList
func (ColumnList) Uniques ¶
func (cl ColumnList) Uniques() ColumnList
type Config ¶
type Config struct { DBName string `json:"dbname" yaml:"dbname"` Host string `json:"host" yaml:"host"` Port string `json:"port" yaml:"port"` User string `json:"user" yaml:"user"` Pass string `json:"pass" yaml:"pass"` Schema string `json:"schema" yaml:"schema"` SSLMode string `json:"sslmode" yaml:"sslmode"` Whitelist []string `json:"whitelist" yaml:"whitelist"` Blacklist []string `json:"blacklist" yaml:"blacklist"` }
type ConfigMap ¶
type ConfigMap map[string]interface{}
ConfigMap is a map with helper functions
func (ConfigMap) DefaultInt ¶
DefaultInt retrieves a non-zero int or the default value provided.
func (ConfigMap) DefaultString ¶
DefaultString retrieves a non-empty string or the default value provided.
func (ConfigMap) Int ¶
Int retrieves an int, the bool says if it exists, is of the appropriate type, and is non-zero. Coerces float64 to int because JSON and Javascript kinda suck.
func (ConfigMap) MustInt ¶
MustInt retrieves a string that must exist and must be an int, and it must not be 0
func (ConfigMap) MustString ¶
MustString retrieves a string that must exist and must be a string, it must also not be the empty string
type Constraint ¶
type Constraint struct { Name string `json:"name"` ConstraintType string `json:"constraint_type"` ColumnName string `json:"column_name"` Type string `json:"type"` Nullable bool `json:"nullable"` Unique bool `json:"unique"` Sequenced bool `json:"sequenced"` AutoGenerated bool `json:"auto_generated"` ColumnDefault *string `json:"column_default"` }
Constraint represents a constraint in a database
type ConstraintList ¶
type ConstraintList []Constraint
func (ConstraintList) AllIndexed ¶
func (cl ConstraintList) AllIndexed() ConstraintList
func (ConstraintList) AllRequired ¶
func (cl ConstraintList) AllRequired() ConstraintList
func (ConstraintList) Checks ¶
func (cl ConstraintList) Checks() ConstraintList
func (ConstraintList) Constraint ¶
func (cl ConstraintList) Constraint(columnName string) (Constraint, bool)
Constraint takes a column name and attempts to retrieve the constraint from the tables constraints If no constraint is found it will return an empty Constraint and false indicating one hasn't been found
func (ConstraintList) PrimaryKey ¶
func (cl ConstraintList) PrimaryKey() *PrimaryKey
func (ConstraintList) Uniques ¶
func (cl ConstraintList) Uniques() ConstraintList
type Constructor ¶
type Constructor interface { TableNames(schema string, whitelist, blacklist []string) ([]string, error) Columns(schema, tableName string, whitelist, blacklist []string) (ColumnList, error) PrimaryKeyInfo(schema, tableName string) (*PrimaryKey, error) Constraints(schema, tableName string) (ConstraintList, error) ForeignKeyInfo(schema, tableName string) ([]ForeignKey, error) // TranslateColumnType takes a Database column type and returns a go column type. TranslateColumnType(Column) Column }
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 DBInfo ¶
type DBInfo struct { Schema string `json:"schema"` Tables []Table `json:"tables"` Dialect Dialect `json:"dialect"` }
DBInfo is the database's table data and dialect.
type Dialect ¶
type Dialect struct { LQ rune `json:"lq"` RQ rune `json:"rq"` UseIndexPlaceholders bool `json:"use_index_placeholders"` UseLastInsertID bool `json:"use_last_insert_id"` UseSchema bool `json:"use_schema"` UseDefaultKeyword bool `json:"use_default_keyword"` // The following is mostly for T-SQL/MSSQL, what a show UseAutoColumns bool `json:"use_auto_columns"` UseTopClause bool `json:"use_top_clause"` UseOutputClause bool `json:"use_output_clause"` UseCaseWhenExistsClause bool `json:"use_case_when_exists_clause"` }
Dialect describes the databases requirements in terms of which features it speaks and what kind of quoting mechanisms it uses.
WARNING: When updating this struct there is a copy of it inside the boil_queries template that is used for users to create queries without having to figure out what their dialect is.
type ForeignKey ¶
type ForeignKey struct { Table string `json:"table"` Name string `json:"name"` Column string `json:"column"` Nullable bool `json:"nullable"` Unique bool `json:"unique"` ForeignTable string `json:"foreign_table"` ForeignColumn string `json:"foreign_column"` ForeignColumnNullable bool `json:"foreign_column_nullable"` ForeignColumnUnique bool `json:"foreign_column_unique"` }
ForeignKey represents a foreign key constraint in a database
type Interface ¶
type Interface interface { // Assemble the database information into a nice struct Assemble(config ConfigMap) (*DBInfo, error) // Templates to add/replace for generation Templates() (map[string]string, error) // Imports to merge for generation Imports() (importers.Collection, error) }
Interface abstracts either a side-effect imported driver or a binary that is called in order to produce the data required for generation.
type PrimaryKey ¶
type PrimaryKey struct { Name string `json:"name"` Columns []string `json:"columns"` Sequenced bool `json:"sequenced"` }
PrimaryKey represents a primary key constraint in a database
type SQLColumnDef ¶
SQLColumnDef formats a column name and type like an SQL column definition.
type SQLColumnDefs ¶
type SQLColumnDefs []SQLColumnDef
SQLColumnDefs has small helper functions
func SQLColDefinitions ¶
func SQLColDefinitions(cols []Column, names []string) SQLColumnDefs
SQLColDefinitions creates a definition in sql format for a column
type Table ¶
type Table struct { Name string `json:"name"` // For dbs with real schemas, like Postgres. // Example value: "schema_name"."table_name" SchemaName string `json:"schema_name"` Columns ColumnList `json:"columns"` PKey *PrimaryKey `json:"p_key"` Constraints ConstraintList `json:"constraints"` FKeys []ForeignKey `json:"f_keys"` IsJoinTable bool `json:"is_join_table"` ToOneRelationships []ToOneRelationship `json:"to_one_relationships"` ToManyRelationships []ToManyRelationship `json:"to_many_relationships"` }
Table metadata from the database schema.
func Tables ¶
func Tables(c Constructor, schema string, whitelist, blacklist []string) ([]Table, error)
Tables returns the metadata for all tables, minus the tables specified in the blacklist.
func (Table) CanLastInsertID ¶
CanLastInsertID checks the following: 1. Is there only one primary key? 2. Does the primary key column have a default value? 3. Is the primary key column type one of uintX/intX? If the above is all true, this table can use LastInsertId
func (Table) CanSoftDelete ¶
type ToManyRelationship ¶
type ToManyRelationship struct { Name string `json:"name"` Table string `json:"table"` Column string `json:"column"` Nullable bool `json:"nullable"` Unique bool `json:"unique"` ForeignTable string `json:"foreign_table"` ForeignColumn string `json:"foreign_column"` ForeignColumnNullable bool `json:"foreign_column_nullable"` ForeignColumnUnique bool `json:"foreign_column_unique"` ToJoinTable bool `json:"to_join_table"` JoinTable string `json:"join_table"` JoinLocalFKeyName string `json:"join_local_fkey_name"` JoinLocalColumn string `json:"join_local_column"` JoinLocalColumnNullable bool `json:"join_local_column_nullable"` JoinLocalColumnUnique bool `json:"join_local_column_unique"` JoinForeignFKeyName string `json:"join_foreign_fkey_name"` JoinForeignColumn string `json:"join_foreign_column"` JoinForeignColumnNullable bool `json:"join_foreign_column_nullable"` JoinForeignColumnUnique bool `json:"join_foreign_column_unique"` }
ToManyRelationship describes a relationship between two tables where the local table has no id, and the foreign table has an id that matches a column in the local table.
func ToManyRelationships ¶
func ToManyRelationships(table string, tables []Table) []ToManyRelationship
ToManyRelationships relationship lookups Input should be the sql name of a table like: videos
type ToOneRelationship ¶
type ToOneRelationship struct { Name string `json:"name"` Table string `json:"table"` Column string `json:"column"` Nullable bool `json:"nullable"` Unique bool `json:"unique"` ForeignTable string `json:"foreign_table"` ForeignColumn string `json:"foreign_column"` ForeignColumnNullable bool `json:"foreign_column_nullable"` ForeignColumnUnique bool `json:"foreign_column_unique"` }
ToOneRelationship describes a relationship between two tables where the local table has no id, and the foreign table has an id that matches a column in the local table, that column can also be unique which changes the dynamic into a one-to-one style, not a to-many.
func ToOneRelationships ¶
func ToOneRelationships(table string, tables []Table) []ToOneRelationship
ToOneRelationships relationship lookups Input should be the sql name of a table like: videos