dialect

package
v0.0.0-...-7203340 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2018 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Mysql    = "mysql"
	Postgres = "postgres"
	Sqlite3  = "sqlite3"
)

Constants that are names of the different supported dialects

Variables

This section is empty.

Functions

func ContextOrBackground

func ContextOrBackground(ctx context.Context) context.Context

ContextOrBackground returns background context if the given context is nil, otherwise it returns the context itself.

func ErrConvert

func ErrConvert(field string, index int, value interface{}, to string) error

ErrConvert returns a conversion error when processing sql values

func ParseBool

func ParseBool(s []byte) bool

ParseBool parses a byte slice to bool

func ParseFloat

func ParseFloat(s []byte) float64

ParseFloat parses a byte slice to float64

func ParseInt

func ParseInt(s []byte) int64

ParseInt parses a byte slice to int64

func ParseTime

func ParseTime(b []byte, precision int) time.Time

ParseTime parses time from mysql database zero time is a special case, since it's month is 00, which causes month out of range error.

func ParseUInt

func ParseUInt(s []byte) uint64

ParseUInt parses a byte slice to uint64

func RelationTables

func RelationTables(gr *graph.Graph) map[string]Table

RelationTables returns all the relation tables that belong to a type.

Relation tables are tables that hold relations between one table and another table, that could not be expressed as a column in one of the tables.

func Values

func Values(r sql.Rows) []driver.Value

Values is a hack to the sql.Rows struct since the rows struct does not expose it's lastcols values, or a way to give a custom scanner to the Scan method. See issue https://github.com/golang/go/issues/22544

Types

type API

type API interface {
	// Name returns the name of the dialect
	Name() string
	// Create returns the SQL CREATE statement and arguments according to the given parameters
	Create(orm.Conn, *CreateParams) ([]string, error)
	// Insert returns the SQL INSERT statement and arguments according to the given parameters
	Insert(*InsertParams) (string, []interface{})
	// Select returns the SQL SELECT statement and arguments according to the given parameters
	Select(*SelectParams) (string, []interface{})
	// Delete returns the SQL DELETE statement and arguments according to the given parameters
	Delete(*DeleteParams) (string, []interface{})
	// Update returns the SQL UPDATE statement and arguments according to the given parameters
	Update(*UpdateParams) (string, []interface{})
	// Drop returns the SQL DROP statement and arguments according to the given parameters
	Drop(*DropParams) (string, []interface{})
}

API is an interface to represent an SQL dialect Objects that implement this interface, can convert query params, such as SelectParams or UpdateParams, and convert them to an SQL statement and a list of arguments, which can be used to invoke SQL Exec or Query functions.

func All

func All() []API

All returns all available dialects

func Get

func Get(name string) API

Get returns a dialect by name

type Assignment

type Assignment struct {
	Column        string
	ColumnValue   interface{}
	OriginalValue interface{}
}

Assignment is a struct that is used for INSERT and UPDATE/SET operations It holds column name and the value to assign.

type Assignments

type Assignments []Assignment

Assignments is a list of Assignment

func (*Assignments) Add

func (a *Assignments) Add(name string, columnValue interface{}, originalValue interface{})

Add adds an assignment to the list

func (Assignments) Args

func (a Assignments) Args() []interface{}

Args are the list of values of the Assignment list

type Column

type Column struct {
	Name    string
	GoType  string
	SQLType string
	Options []string
}

Column describes an SQL table

type CreateParams

type CreateParams struct {
	Table string
	// MarshaledTable is a json string representing a table
	// The representation is of type Table, and created by the function Marshal
	// of that type.
	// It is given from the auto generated code.
	MarshaledTable string
	// MarshaledRelationTables is a map between a relation table name and
	// a json string representing a type Table, generated with the function Table.Marshal.
	// It is given from the auto generated code.
	MarshaledRelationTables map[string]string
	// IfNotExists determines to create the table only if it does not exists
	IfNotExists bool
	// AutoMigrate perform auto-migration of table scheme
	AutoMigrate bool
	// Relations makes the create operation create relation tables and not
	// the type actual table
	Relations bool
	// Ctx is a context parameter for the query
	// even though it is not recommended to store context in a struct, here the struct
	// actually represents an arguments list, passed to a function.
	Ctx context.Context
}

CreateParams holds parameters for an SQL CREATE statement

type DeleteParams

type DeleteParams struct {
	Table string
	Where Where
	// Ctx is a context parameter for the query
	// even though it is not recommended to store context in a struct, here the struct
	// actually represents an arguments list, passed to a function.
	Ctx context.Context
}

DeleteParams holds parameters for an SQL DELETE statement

type Dialect

type Dialect interface {
	// Name returns the name of the dialect
	Name() string
	// GoTypeToColumnType gets a string that represents a go basic type
	// and returns an SQL type for a createColumn for a field of that type.
	GoTypeToColumnType(goType string, autoIncrement bool) *sqltypes.Type
	// Translate gets a MySQL statement and returns a corresponding statement
	// in a specific dialect
	Translate(string) string
	// ConvertValueCode returns code for converting a value for a field with
	// a given SQL type.
	ConvertValueCode(*load.Field) string
	// Quote returns the quoted form of an SQL variable
	Quote(string) string
	// ReplaceVars replaces question marks from sql query to the right variable of the dialect
	Var(int) string
}

Dialect is an interface for a dialect for generating ORM code

type DropParams

type DropParams struct {
	Table    string
	IfExists bool
	// Ctx is a context parameter for the query
	// even though it is not recommended to store context in a struct, here the struct
	// actually represents an arguments list, passed to a function.
	Ctx context.Context
}

DropParams holds parameters for an SQL DROP statement

type ForeignKey

type ForeignKey struct {
	Columns        []string
	Table          string
	ForeignColumns []string
}

ForeignKey describes an SQL foreign key

type Group

type Group struct {
	Column string
}

Group is a struct that is used for GROUP BY operations

type Groups

type Groups []Group

Groups is a list of Group

func (*Groups) Add

func (g *Groups) Add(name string)

Add adds a column to a group

type InsertParams

type InsertParams struct {
	Table string
	// Assignments are values to store in the new row
	Assignments Assignments
	// RetColumns are columns to return
	// used in postgres dialect
	RetColumns []string
	// Ctx is a context parameter for the query
	// even though it is not recommended to store context in a struct, here the struct
	// actually represents an arguments list, passed to a function.
	Ctx context.Context
}

InsertParams holds parameters for an SQL INSERT statement

type JoinParams

type JoinParams struct {
	SelectParams
	Pairings []Pairing
}

JoinParams are parameters to perform a join operation Name SelectParams is used to perform select operations on the join struct's field. Pairings describe the relation between the join's fields

func (*JoinParams) TableName

func (j *JoinParams) TableName(parentTable string) string

TableName creates a table name for a join operation this is useful in case several fields referencing the same table

type Order

type Order struct {
	Column string
	Dir    orm.OrderDir
}

Order is a struct that is used for ORDER BY operations. It holds the column name and the order direction.

type Orders

type Orders []Order

Orders is a list of Order

func (*Orders) Add

func (g *Orders) Add(name string, dir orm.OrderDir)

Add adds an order of a column and direction

type Page

type Page struct {
	Limit  int64
	Offset int64
}

Page is a struct that is used for LIMIT/OFFSET operations

type Pairing

type Pairing struct {
	// Column is the column in the current table for the JOIN statement
	Column string
	// JoinedColumn is the column in the referenced table for the JOIN statement
	JoinedColumn string
}

Pairing describe a join relation

type SelectParams

type SelectParams struct {
	Table string
	// Columns are the selected columns for the query
	Columns map[string]bool
	// Joins are recursive join arguments for the query
	Joins []JoinParams
	// Count indicates weather a COUNT(*) column should be added to the query
	Count bool
	// Where is the WHERE part of the query
	Where Where
	// Groups are is the GROUP BY conditions for the query
	Groups Groups
	// Orders are the ORDER BY conditions for the query
	Orders Orders
	// Page is for query pagination
	Page Page
	// OrderedColumns store all the columns of a table
	// they are defined in a specific order so the parsing of returned values from
	// the SQL query will be easy.
	OrderedColumns []string
	// Ctx is a context parameter for the query
	// even though it is not recommended to store context in a struct, here the struct
	// actually represents an arguments list, passed to a function.
	Ctx context.Context
}

SelectParams holds parameters for an SQL SELECT statement

func (*SelectParams) SelectAll

func (p *SelectParams) SelectAll() bool

SelectAll indicates if the SelectParams should select all columns in the query string

func (*SelectParams) SelectedColumns

func (p *SelectParams) SelectedColumns() []string

SelectedColumns returns a list of selected columns for a query

type Table

type Table struct {
	Columns     []Column
	PrimaryKeys []string
	ForeignKeys []ForeignKey
}

Table represents an SQL table for marshaling to go code

func NewTable

func NewTable(gr *graph.Graph) Table

NewTable returns table structure to be used for generated code

func (*Table) Diff

func (t *Table) Diff(want *Table) (*Table, error)

Diff calculate difference between a 'got' table and a 'want' table and returns a table containing the columns, primary keys and foreign keys that we need to add the that 'got' table. It returns an error in case that there is a change that can't be applied.

func (Table) Marshal

func (t Table) Marshal() string

Marshal returns a string of Table

func (*Table) UnMarshal

func (t *Table) UnMarshal(s string) error

UnMarshal takes a string and set the table content

type TableNamer

type TableNamer interface {
	TableName() string
}

TableNamer is an interface for a model to change it's table name a struct that implements this interface will set it's SQL table name by the return value from TableName function.

type UpdateParams

type UpdateParams struct {
	Table       string
	Assignments Assignments
	Where       Where
	// Ctx is a context parameter for the query
	// even though it is not recommended to store context in a struct, here the struct
	// actually represents an arguments list, passed to a function.
	Ctx context.Context
}

UpdateParams holds parameters for an SQL UPDATE statement

type Where

type Where interface {
	Build(table string, b *builder.Builder)
	// Or applies OR condition with another Where statement
	Or(Where) Where
	// And applies AND condition with another Where statement
	And(Where) Where
}

Where is an API for creating WHERE statements

func And

func And(w ...Where) Where

And apply an AND condition between WHERE statement

func NewWhere

func NewWhere(op orm.Op, variable string, value interface{}) Where

NewWhere returns a new WHERE statement

func NewWhereBetween

func NewWhereBetween(variable string, low, high interface{}) Where

NewWhereBetween returns a new 'WHERE variable BETWEEN low AND high' statement

func NewWhereIn

func NewWhereIn(variable string, values ...interface{}) Where

NewWhereIn returns a new 'WHERE variable IN (...)' statement

func Or

func Or(w ...Where) Where

Or apply an AND condition between WHERE statement

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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