dbal

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2022 License: Apache-2.0 Imports: 6 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BindingKeys = []string{
	"select", "from", "join", "where",
	"groupBy", "having",
	"order",
	"union", "unionOrder",
	"sql",
}

BindingKeys the binding key orders

View Source
var Grammars = map[string]Grammar{}

Grammars loaded grammar driver

View Source
var Operators = []string{
	"=", "<", ">", "<=", ">=", "<>", "!=", "<=>",
	"like", "like binary", "not like", "ilike",
	"&", "|", "^", "<<", ">>",
	"rlike", "not rlike", "regexp", "not regexp",
	"~", "~*", "!~", "!~*", "similar to",
	"not similar to", "not ilike", "~~*", "!~~*",
}

Operators the default Operators

Functions

func IsExpression added in v0.5.1

func IsExpression(value interface{}) bool

IsExpression Determine if the given value is a raw expression.

func Register added in v0.0.3

func Register(name string, grammar Grammar)

Register register the grammar driver

Types

type Aggregate added in v0.5.1

type Aggregate struct {
	Func    string        // AVG, COUNT, MIN, MAX, SUM
	Columns []interface{} // The columns for Aggregate
}

Aggregate An aggregate function and column to be run.

type Column added in v0.0.3

type Column struct {
	DBName                   string      `db:"db_name"`
	TableName                string      `db:"table_name"`
	Name                     string      `db:"name"`
	Position                 int         `db:"position"`
	Default                  interface{} `db:"default"`
	DefaultRaw               string      `db:"default_raw"`
	Nullable                 bool        `db:"nullable"`
	IsUnsigned               bool        `db:"unsigned"`
	Type                     string      `db:"type"`
	Length                   *int        `db:"length"`
	OctetLength              *int        `db:"octet_length"`
	Precision                *int        `db:"precision"`
	Scale                    *int        `db:"scale"`
	DateTimePrecision        *int        `db:"datetime_precision"`
	Charset                  *string     `db:"charset"`
	Collation                *string     `db:"collation"`
	Key                      *string     `db:"key"`
	Extra                    *string     `db:"extra"`
	Comment                  *string     `db:"comment"`
	Primary                  bool        `db:"primary"`
	TypeName                 string      `db:"type_name"`
	MaxLength                int
	DefaultLength            int
	MaxPrecision             int
	DefaultPrecision         int
	MaxScale                 int
	DefaultScale             int
	MaxDateTimePrecision     int
	DefaultDateTimePrecision int
	Option                   []string
	Table                    *Table
	Indexes                  []*Index
	Constraint               *Constraint
}

Column the table Column

type Command added in v0.0.3

type Command struct {
	Name    string        // The command name
	Params  []interface{} // The command parameters
	Success func()        // Success callback function
	Fail    func()        // Fail callback function
}

Command The Command that should be run for the table.

func (*Command) Callback added in v0.0.3

func (command *Command) Callback(err error)

Callback run the callback code

type Config

type Config struct {
	Driver   string `json:"driver"`        // The driver name. mysql,pgsql,sqlite3,oci,sqlsrv
	DSN      string `json:"dsn,omitempty"` // The driver wrapper. sqlite:///:memory:, mysql://localhost:4486/foo?charset=UTF8
	Name     string `json:"name,omitempty"`
	ReadOnly bool   `json:"readonly,omitempty"`
}

Config the Connection configuration

type Connection added in v0.0.3

type Connection struct {
	DB      *sqlx.DB
	Config  *Config
	Option  *Option
	Version *Version
}

Connection the database connection

type Constraint added in v0.0.3

type Constraint struct {
	SchemaName string
	TableName  string
	ColumnName string
	Type       string
	Args       []string
	Table      *Table
}

Constraint the table constraint

func NewConstraint added in v0.0.3

func NewConstraint(schemaName string, tableName string, columnName string) *Constraint

NewConstraint make a new constraint intstance

type Expression added in v0.5.1

type Expression struct {
	Value interface{}
}

Expression the raw query expression.

func NewExpression added in v0.5.1

func NewExpression(value interface{}) Expression

NewExpression make a new expression instance

func Raw added in v0.5.1

func Raw(value interface{}) Expression

Raw make a new expression instance

func (Expression) GetValue added in v0.5.1

func (expression Expression) GetValue() string

GetValue Get the value of the expression.

type From added in v0.5.1

type From struct {
	Type   string
	Name   interface{}
	Alias  string
	Offset int
	SQL    string
}

From the from query

func (From) IsEmpty added in v0.5.1

func (from From) IsEmpty() bool

IsEmpty determine if the from value is empty

type Grammar added in v0.0.3

type Grammar interface {
	NewWith(db *sqlx.DB, config *Config, option *Option) (Grammar, error)
	NewWithRead(write *sqlx.DB, writeConfig *Config, read *sqlx.DB, readConfig *Config, option *Option) (Grammar, error)

	Wrap(value interface{}) string
	WrapTable(value interface{}) string

	OnConnected() error

	GetVersion() (*Version, error)
	GetDatabase() string
	GetSchema() string
	GetOperators() []string

	// Grammar for migrating
	GetTables() ([]string, error)

	TableExists(name string) (bool, error)
	GetTable(name string) (*Table, error)
	CreateTable(table *Table) error
	AlterTable(table *Table) error
	DropTable(name string) error
	DropTableIfExists(name string) error
	RenameTable(old string, new string) error
	GetColumnListing(dbName string, tableName string) ([]*Column, error)

	// Grammar for querying
	CompileInsert(query *Query, columns []interface{}, values [][]interface{}) (string, []interface{})
	CompileInsertOrIgnore(query *Query, columns []interface{}, values [][]interface{}) (string, []interface{})
	CompileInsertGetID(query *Query, columns []interface{}, values [][]interface{}, sequence string) (string, []interface{})
	CompileInsertUsing(query *Query, columns []interface{}, sql string) string
	CompileUpsert(query *Query, columns []interface{}, values [][]interface{}, uniqueBy []interface{}, updateValues interface{}) (string, []interface{})
	CompileUpdate(query *Query, values map[string]interface{}) (string, []interface{})
	CompileDelete(query *Query) (string, []interface{})
	CompileTruncate(query *Query) ([]string, [][]interface{})
	CompileSelect(query *Query) string
	CompileSelectOffset(query *Query, offset *int) string
	CompileExists(query *Query) string

	ProcessInsertGetID(sql string, bindings []interface{}, sequence string) (int64, error)
}

Grammar the SQL Grammar inteface

type Having added in v0.5.1

type Having struct {
	Type     string
	Column   interface{}
	Operator string
	Value    interface{}
	Boolean  string
	Offset   int
	Values   []interface{}
	Not      bool
	SQL      string
}

Having The having constraints for the query.

type Index added in v0.0.3

type Index struct {
	DBName       string  `db:"db_name"`
	TableName    string  `db:"table_name"`
	ColumnName   string  `db:"column_name"`
	Name         string  `db:"index_name"`
	SEQ          int     `db:"seq_in_index"`
	SeqColumn    int     `db:"seq_in_column"`
	Collation    string  `db:"collation"`
	Nullable     bool    `db:"nullable"`
	Unique       bool    `db:"unique"`
	Primary      bool    `db:"primary"`
	SubPart      int     `db:"sub_part"`
	Type         string  `db:"type"`
	IndexType    string  `db:"index_type"`
	Comment      *string `db:"comment"`
	IndexComment *string `db:"index_comment"`
	Table        *Table
	Columns      []*Column
}

Index the talbe index

func (*Index) AddColumn added in v0.0.3

func (index *Index) AddColumn(column *Column)

AddColumn add column to index

type Join added in v0.5.1

type Join struct {
	Type   string      // inner, left, right, cross
	Name   interface{} // The table the join clause is joining to.
	Query  *Query
	Alias  string
	SQL    interface{}
	Offset int
}

Join the join clause for the query

type Name added in v0.5.1

type Name struct {
	Prefix string
	Name   string
	Alias  string
}

Name the from attribute ( table_name as t1, column_name as c1...)

func NewName added in v0.5.1

func NewName(fullname string, prefix ...string) Name

NewName make a new Name instance

func (Name) As added in v0.5.1

func (name Name) As() string

As get the alias of name

func (Name) Fullname added in v0.5.1

func (name Name) Fullname() string

Fullname get the name name with prefix

type Option added in v0.0.3

type Option struct {
	Prefix    string `json:"prefix,omitempty"` // Table prifix
	Collation string `json:"collation,omitempty"`
	Charset   string `json:"charset,omitempty"`
}

Option the database configuration

type Order added in v0.5.1

type Order struct {
	Type      string
	Column    interface{}
	Direction string
	Offset    int
	SQL       string
}

Order the order query

type Primary added in v0.0.3

type Primary struct {
	DBName    string `db:"db_name"`
	TableName string `db:"table_name"`
	Name      string `db:"primary_name"`
	Table     *Table
	Columns   []*Column
}

Primary the table primary key

type Query added in v0.5.1

type Query struct {
	UseWriteConnection bool                     // Whether to use write connection for the select. default is false
	Lock               interface{}              //  Indicates whether row locking is being used.
	From               From                     // The table which the query is targeting.
	Columns            []interface{}            // The columns that should be returned. (Name or Expression)
	Aggregate          Aggregate                // An aggregate function and column to be run.
	Wheres             []Where                  // The where constraints for the query.
	Joins              []Join                   // The table joins for the query.
	Unions             []Union                  // The query union statements.
	UnionLimit         int                      // The maximum number of union records to return.
	UnionOffset        int                      // The number of union records to skip.
	UnionOrders        []Order                  // The orderings for the union query.
	Orders             []Order                  // The orderings for the query.
	Limit              int                      // The maximum number of records to return.
	Offset             int                      // The number of records to skip.
	Groups             []interface{}            // The groupings for the query.
	Havings            []Having                 // The having constraints for the query.
	Bindings           map[string][]interface{} // The current query value bindings.
	Distinct           bool                     // Indicates if the query returns distinct results. Occasionally contains the columns that should be distinct. default is false
	DistinctColumns    []interface{}            // Indicates if the query returns distinct results. Occasionally contains the columns that should be distinct.
	IsJoinClause       bool                     // Determine if the query is a join clause.
	BindingOffset      int                      // The Binding offset before select
	SQL                string                   // The SQL STMT
}

Query the query builder

func NewQuery added in v0.5.1

func NewQuery() *Query

NewQuery make a new Query instance

func (*Query) AddBinding added in v0.5.1

func (query *Query) AddBinding(typ string, value interface{})

AddBinding Add a binding to the query.

func (*Query) AddColumn added in v0.5.1

func (query *Query) AddColumn(column interface{}) *Query

AddColumn add a column to query

func (*Query) Clone added in v0.5.1

func (query *Query) Clone() *Query

Clone clone the query instance

func (*Query) CopyAggregate added in v0.5.1

func (query *Query) CopyAggregate() Aggregate

CopyAggregate copy Aggregate

func (*Query) CopyBindings added in v0.5.1

func (query *Query) CopyBindings() map[string][]interface{}

CopyBindings copy Bindings

func (*Query) CopyColumns added in v0.5.1

func (query *Query) CopyColumns() []interface{}

CopyColumns copy columns

func (*Query) CopyDistinctColumns added in v0.5.1

func (query *Query) CopyDistinctColumns() []interface{}

CopyDistinctColumns copy DistinctColumns

func (*Query) CopyFrom added in v0.5.1

func (query *Query) CopyFrom() From

CopyFrom copy from

func (*Query) CopyGroups added in v0.5.1

func (query *Query) CopyGroups() []interface{}

CopyGroups copy Groups

func (*Query) CopyHavings added in v0.5.1

func (query *Query) CopyHavings() []Having

CopyHavings copy Havings

func (*Query) CopyJoins added in v0.5.1

func (query *Query) CopyJoins() []Join

CopyJoins copy joins

func (*Query) CopyLock added in v0.5.1

func (query *Query) CopyLock() interface{}

CopyLock copy Lock

func (*Query) CopyOrders added in v0.5.1

func (query *Query) CopyOrders() []Order

CopyOrders copy Orders

func (*Query) CopyUnionOrders added in v0.5.1

func (query *Query) CopyUnionOrders() []Order

CopyUnionOrders copy UnionOrders

func (*Query) CopyUnions added in v0.5.1

func (query *Query) CopyUnions() []Union

CopyUnions copy unions

func (*Query) CopyWheres added in v0.5.1

func (query *Query) CopyWheres() []Where

CopyWheres copy wheres

func (*Query) GetBindings added in v0.5.1

func (query *Query) GetBindings(key ...string) []interface{}

GetBindings Get the current query value bindings in a flattened array.

type Quoter added in v0.0.3

type Quoter interface {
	Bind(db *sqlx.DB, prefix string, dbRead ...*sqlx.DB) Quoter
	ID(value string) string
	VAL(value interface{}) string // operates on both string and []byte and int or other types.
	Wrap(value interface{}) string
	WrapTable(value interface{}) string
	WrapUnion(sql string) string
	IsExpression(value interface{}) bool
	Parameter(value interface{}, num int) string
	Parameterize(values []interface{}, offset int) string
	Columnize(columns []interface{}) string
}

Quoter the database quoting query text intrface

type Select added in v0.5.1

type Select struct {
	Type   string
	Name   interface{}
	Alias  string
	Offset int
	SQL    string
}

Select the from query

type Table added in v0.0.3

type Table struct {
	DBName        string    `db:"db_name"`
	SchemaName    string    `db:"schema_name"`
	TableName     string    `db:"table_name"`
	Comment       string    `db:"table_comment"`
	Type          string    `db:"table_type"`
	Engine        string    `db:"engine"`
	CreateTime    time.Time `db:"create_time"`
	CreateOptions string    `db:"create_options"`
	Collation     string    `db:"collation"`
	Charset       string    `db:"charset"`
	Rows          int       `db:"table_rows"`
	RowLength     int       `db:"avg_row_length"`
	IndexLength   int       `db:"index_length"`
	AutoIncrement int       `db:"auto_increment"`
	Primary       *Primary
	ColumnMap     map[string]*Column
	IndexMap      map[string]*Index
	Columns       []*Column
	Indexes       []*Index
	Commands      []*Command
}

Table the table struct

func NewTable added in v0.0.3

func NewTable(name string, schemaName string, dbName string) *Table

NewTable make a grammar table

func (*Table) AddCommand added in v0.0.3

func (table *Table) AddCommand(name string, success func(), fail func(), params ...interface{})

AddCommand Add a new command to the table.

The commands must be:

AddColumn(column *Column)    for adding a column
ModifyColumn(column *Column) for modifying a colu
RenameColumn(old string,new string)  for renaming a column
DropColumn(name string)  for dropping a column
CreateIndex(index *Index) for creating a index
DropIndex( name string) for  dropping a index
RenameIndex(old string,new string)  for renaming a index

func (*Table) GetColumn added in v0.0.3

func (table *Table) GetColumn(name string) *Column

GetColumn get the given name column instance

func (*Table) GetIndex added in v0.0.3

func (table *Table) GetIndex(name string) *Index

GetIndex get the given name index instance

func (*Table) GetName added in v0.0.3

func (table *Table) GetName() string

GetName get the table name

func (*Table) GetPrimary added in v0.0.3

func (table *Table) GetPrimary(name string, columns ...*Column) *Primary

GetPrimary get the primary key instance

func (*Table) HasColumn added in v0.0.3

func (table *Table) HasColumn(name string) bool

HasColumn checking if the given name column exists

func (*Table) HasIndex added in v0.0.3

func (table *Table) HasIndex(name string) bool

HasIndex checking if the given name index exists

func (*Table) NewColumn added in v0.0.3

func (table *Table) NewColumn(name string) *Column

NewColumn create a new column intstance

func (*Table) NewIndex added in v0.0.3

func (table *Table) NewIndex(name string, columns ...*Column) *Index

NewIndex create a new index intstance

func (*Table) NewPrimary added in v0.0.3

func (table *Table) NewPrimary(name string, columns ...*Column) *Primary

NewPrimary create a new primary intstance

func (*Table) PushColumn added in v0.0.3

func (table *Table) PushColumn(column *Column) *Table

PushColumn push a column instance to the table columns

func (*Table) PushIndex added in v0.0.3

func (table *Table) PushIndex(index *Index) *Table

PushIndex push an index instance to the table indexes

type Union added in v0.5.1

type Union struct {
	All   bool // Union all
	Query *Query
}

Union the query union statement

type Version added in v0.0.3

type Version struct {
	Driver string
	semver.Version
}

Version the database version

type Where added in v0.5.1

type Where struct {
	Type     string // basic, nested, sub, null, notnull ...
	Column   interface{}
	First    interface{} // for comparing the two columns
	Second   interface{} // for comparing the two columns
	SQL      string
	Operator string
	Boolean  string
	Wheres   []Where
	Query    *Query
	Value    interface{}
	Values   []interface{}
	ValuesIn interface{}
	Not      bool
	Offset   int
}

Where The where constraint for the query.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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