drivers

package
v0.0.0-...-f8ae61f Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2022 License: MIT Imports: 8 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 BuildRelationships

func BuildRelationships(tables []Table) map[string][]Relationship

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 IsJoinTable

func IsJoinTable(t Table) bool

A composite primary key involving two columns Both primary key columns are also foreign keys

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"`
	Comment   string `json:"comment" toml:"comment"`
	Nullable  bool   `json:"nullable" toml:"nullable"`
	Unique    bool   `json:"unique" toml:"unique"`
	Generated bool   `json:"generated" toml:"generated"`

	// OptionalType is a type that implements interface{IsSet() bool}
	// Used in the OptionalXXX type
	OptionalType string `json:"optional_type" toml:"optional_type"`
	// The type for where comparisons. This should be the non-nullable version
	// of the Type
	CompareType string `json:"compare_type" toml:"compare_type"`

	// For Nullable columns, a method or property to retrieve the
	// non null type from the nullable one
	GetNonNull string `json:"get_non_null" toml:"get_non_null"`

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

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

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, includes, excludes []string) ColumnFilter

This takes a list of table names with the includes and excludes

type Constraint

type Constraint struct {
	Name    string   `json:"name"`
	Columns []string `json:"columns"`
}

Constraint represents a primary key constraint in a database

type Constructor

type Constructor interface {
	// Load all constaints in the database, keyed by table
	Constraints(ColumnFilter) (DBConstraints, error)

	// For tables
	TableNames(Filter) ([]string, error)
	TableColumns(tableName string, filter ColumnFilter) ([]Column, error)

	// For views
	ViewNames(Filter) ([]string, error)
	ViewColumns(tableName string, filter ColumnFilter) ([]Column, 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 DBConstraints

type DBConstraints struct {
	PKs     map[string]*PrimaryKey
	FKs     map[string][]ForeignKey
	Uniques map[string][]Constraint
}

type DBInfo

type DBInfo struct {
	Schema string  `json:"schema"`
	Tables []Table `json:"tables"`
	Enums  []Enum  `json:"enums"`
}

DBInfo is the database's table data and dialect.

type Enum

type Enum struct {
	Name   string
	Type   string
	Values []string
}

type Filter

type Filter struct {
	Include []string
	Exclude []string
}

type ForeignKey

type ForeignKey struct {
	Constraint
	ForeignTable   string   `json:"foreign_table"`
	ForeignColumns []string `json:"foreign_columns"`
}

ForeignKey represents a foreign key constraint in a database

type Interface

type Interface interface {
	// Assemble the database information into a nice struct
	Assemble() (*DBInfo, 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 = Constraint

PrimaryKey represents a primary key constraint in a database

type RelType

type RelType string
const (
	ToOne  RelType = "to_one"
	ToMany RelType = "to_many"
)

type Relationship

type Relationship struct {
	// The type of relationship. ToOne or ToMany
	// To know if it needs a single model or a slice
	Type RelType `json:"type"`
	orm.Relationship
}

func (Relationship) Foreign

func (r Relationship) Foreign() string

func (Relationship) Local

func (r Relationship) Local() string

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:"foreign_keys"`
	Uniques []Constraint `json:"unique"`

	IsJoinTable bool `json:"is_join_table"`

	Relationships []Relationship `json:"relationship"`

	// For views
	IsView bool `json:"is_view"`
}

Table metadata from the database schema.

func GetTable

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

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

func Tables

func Tables(c Constructor, concurrency int, includes, excludes []string) ([]Table, error)

TablesConcurrently is a concurrent version of Tables. It returns the metadata for all tables, minus the tables specified in the excludes.

func (Table) CanSoftDelete

func (t Table) CanSoftDelete(deleteColumn string) bool

func (Table) GetColumn

func (t Table) GetColumn(name string) Column

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

type TableColumnTypeTranslator

type TableColumnTypeTranslator interface {
	// TranslateTableColumnType takes a Database column type and table name and returns a go column type.
	TranslateTableColumnType(c Column, tableName string) Column
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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