database

package
v0.0.0-...-fc4761b Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

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

Index

Constants

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

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 DefaultEnv

func DefaultEnv(key, def string) string

DefaultEnv grabs a value from the environment or a default. This is shared by drivers to get config for testing.

func DriverExists

func DriverExists(name string) bool

GetDriver retrieves the driver by name

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

func RegisterFromInit(name string, driver Interface)

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

func TablesFromList(list []string) []string

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

func FilterColumnsByAuto(auto bool, columns []Column) []Column

FilterColumnsByAuto generates the list of columns that have autogenerated values

func FilterColumnsByDefault

func FilterColumnsByDefault(defaults bool, columns []Column) []Column

FilterColumnsByDefault generates the list of columns that have default values

func FilterColumnsByEnum

func FilterColumnsByEnum(columns []Column) []Column

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"`
}

func (*Config) ToMap

func (p *Config) ToMap() map[string]interface{}

type ConfigMap

type ConfigMap map[string]interface{}

ConfigMap is a map with helper functions

func (ConfigMap) DefaultInt

func (c ConfigMap) DefaultInt(key string, def int) int

DefaultInt retrieves a non-zero int or the default value provided.

func (ConfigMap) DefaultString

func (c ConfigMap) DefaultString(key, def string) string

DefaultString retrieves a non-empty string or the default value provided.

func (ConfigMap) Int

func (c ConfigMap) Int(key string) (int, bool)

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

func (c ConfigMap) MustInt(key string) int

MustInt retrieves a string that must exist and must be an int, and it must not be 0

func (ConfigMap) MustString

func (c ConfigMap) MustString(key string) string

MustString retrieves a string that must exist and must be a string, it must also not be the empty string

func (ConfigMap) String

func (c ConfigMap) String(key string) (string, bool)

String retrieves a non-empty string, the bool says if it exists, is of appropriate type, and has a non-zero length or not.

func (ConfigMap) StringSlice

func (c ConfigMap) StringSlice(key string) ([]string, bool)

StringSlice retrieves an string slice, the bool says if it exists, is of the appropriate type, is non-nil and non-zero length

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.

func GetDriver

func GetDriver(name string) Interface

GetDriver retrieves the driver by name

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

type SQLColumnDef struct {
	Name string
	Type string
}

SQLColumnDef formats a column name and type like an SQL column definition.

func (SQLColumnDef) String

func (s SQLColumnDef) String() string

String for fmt.Stringer

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

func (SQLColumnDefs) Names

func (s SQLColumnDefs) Names() []string

Names returns all the names

func (SQLColumnDefs) Types

func (s SQLColumnDefs) Types() []string

Types returns all the types

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 GetTable

func GetTable(tables []Table, name string) (tbl Table)

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

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

func (t Table) CanLastInsertID() bool

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

func (t Table) CanSoftDelete() bool

func (Table) GetColumn

func (t Table) GetColumn(name string) (col Column)

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

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

Directories

Path Synopsis
driver
Package driver implements an sqlboiler driver.
Package driver implements an sqlboiler driver.

Jump to

Keyboard shortcuts

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