drivers

package
v3.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2018 License: BSD-3-Clause Imports: 12 Imported by: 10

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

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 Config

type Config map[string]interface{}

Config is a map with helper functions

func (Config) DefaultInt

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

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

func (Config) DefaultString

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

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

func (Config) Int

func (c Config) 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 (Config) MustInt

func (c Config) MustInt(key string) int

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

func (Config) MustString

func (c Config) 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 (Config) String

func (c Config) 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 (Config) StringSlice

func (c Config) 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 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.

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

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    []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 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) 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