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 DriverMain(driver Interface)
- func RegisterBinary(name, path string)
- func RegisterFromInit(name string, driver Interface)
- func TablesFromList(list []string) []string
- type Column
- type Config
- func (c Config) DefaultInt(key string, def int) int
- func (c Config) DefaultString(key, def string) string
- func (c Config) Int(key string) (int, bool)
- func (c Config) MustInt(key string) int
- func (c Config) MustString(key string) string
- func (c Config) String(key string) (string, bool)
- func (c Config) StringSlice(key string) ([]string, bool)
- 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"` Type string `json:"type" toml:"type"` DBType string `json:"db_type" toml:"db_type"` Default string `json:"default" toml:"default"` Nullable bool `json:"nullable" toml:"nullable"` Unique bool `json:"unique" toml:"unique"` Validated bool `json:"validated" toml:"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"` UDTName string `json:"udt_name" toml:"udt_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"` // 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"` }
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 Config ¶
type Config map[string]interface{}
Config is a map with helper functions
func (Config) DefaultInt ¶
DefaultInt retrieves a non-zero int or the default value provided.
func (Config) DefaultString ¶
DefaultString retrieves a non-empty string or the default value provided.
func (Config) 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 (Config) MustInt ¶
MustInt retrieves a string that must exist and must be an int, and it must not be 0
func (Config) MustString ¶
MustString retrieves a string that must exist and must be a string, it must also not be the empty string
type Constructor ¶
type Constructor interface { TableNames(schema string, whitelist, blacklist []string) ([]string, error) Columns(schema, tableName string, whitelist, blacklist []string) ([]Column, error) PrimaryKeyInfo(schema, tableName string) (*PrimaryKey, 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 Config) (*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 ¶
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 []Column `json:"columns"` PKey *PrimaryKey `json:"p_key"` 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
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