builder

package
v2.21.1 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2022 License: MIT Imports: 14 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ToggleMultiTable    = "MultiTable"
	ToggleNeedAutoAlias = "NeedAlias"
	ToggleUseValues     = "UseValues"
)
View Source
var (
	UpdateNeedLimitByWhere = errors.New("no where limit for update")
)

Functions

func Alias

func Alias(expr SqlExpr, name string) *exAlias

func Comment

func Comment(c string) *comment

func ContextWithToggles

func ContextWithToggles(ctx context.Context, toggles Toggles) context.Context

func CrossJoin

func CrossJoin(table SqlExpr) *join

func Expect

func Expect() *combination

func ForEachStructFieldValue

func ForEachStructFieldValue(ctx context.Context, rv reflect.Value, fn func(*StructField))

func FullJoin

func FullJoin(table SqlExpr) *join

func GetColumnName

func GetColumnName(fieldName, tagValue string) string

func GroupBy

func GroupBy(groups ...SqlExpr) *groupBy

func InnerJoin

func InnerJoin(table SqlExpr) *join

func Intersect

func Intersect() *combination

func IsNilExpr

func IsNilExpr(e SqlExpr) bool

func Join

func Join(table SqlExpr, prefixes ...string) *join

func LeftJoin

func LeftJoin(table SqlExpr) *join

func Limit

func Limit(rowCount int64) *limit

func MultiMayAutoAlias

func MultiMayAutoAlias(columns ...SqlExpr) *exMayAutoAlias

func OnConflict

func OnConflict(columns *Columns) *onConflict

func OrderBy

func OrderBy(orders ...*Order) *orderBy

func RangeNotNilExpr

func RangeNotNilExpr(exprs []SqlExpr, each func(e SqlExpr, i int))

func ResolveIndexNameAndMethod

func ResolveIndexNameAndMethod(n string) (name string, method string)

func RightJoin

func RightJoin(table SqlExpr) *join

func ScanDefToTable

func ScanDefToTable(rv reflect.Value, table *Table)

func TableAliasFromContext

func TableAliasFromContext(ctx context.Context) string

func TableNameFromContext

func TableNameFromContext(ctx context.Context) string

func ToMap

func ToMap(list []string) map[string]bool

func Union

func Union() *combination

func Where

func Where(c SqlCondition) *where

func WithTableAlias

func WithTableAlias(tableName string) func(ctx context.Context) context.Context

func WithTableName

func WithTableName(tableName string) func(ctx context.Context) context.Context

func WriteAdditions

func WriteAdditions(e *Ex, additions ...Addition)

func WriteAssignments

func WriteAssignments(e *Ex, assignments ...*Assignment)

Types

type Addition

type Addition interface {
	SqlExpr
	AdditionType() AdditionType
}

type AdditionType

type AdditionType int
const (
	AdditionJoin AdditionType = iota
	AdditionWhere
	AdditionGroupBy
	AdditionCombination
	AdditionOrderBy
	AdditionLimit
	AdditionOnConflict
	AdditionOther
	AdditionComment
)

type Additions

type Additions []Addition

func (Additions) Len

func (additions Additions) Len() int

func (Additions) Less

func (additions Additions) Less(i, j int) bool

func (Additions) Swap

func (additions Additions) Swap(i, j int)

type Assignment

type Assignment struct {
	SqlAssignmentMarker
	// contains filtered or unexported fields
}

func ColumnsAndValues

func ColumnsAndValues(columnOrColumns SqlExpr, values ...interface{}) *Assignment

func (*Assignment) Ex

func (a *Assignment) Ex(ctx context.Context) *Ex

func (*Assignment) IsNil

func (a *Assignment) IsNil() bool

type Assignments

type Assignments []*Assignment

type BuildSubQuery

type BuildSubQuery func(table *Table) SqlExpr

type Column

type Column struct {
	Name      string
	FieldName string
	Table     *Table

	Description []string
	Relation    []string

	*ColumnType
	// contains filtered or unexported fields
}

func Col

func Col(name string) *Column

func (*Column) Between

func (c *Column) Between(leftValue interface{}, rightValue interface{}) SqlCondition

func (*Column) Desc

func (c *Column) Desc(d int) SqlExpr

func (*Column) Eq

func (c *Column) Eq(v interface{}) SqlCondition

func (*Column) Ex

func (c *Column) Ex(ctx context.Context) *Ex

func (*Column) Expr

func (c *Column) Expr(query string, args ...interface{}) *Ex

func (Column) Field

func (c Column) Field(fieldName string) *Column

func (Column) Full

func (c Column) Full() *Column

func (*Column) Gt

func (c *Column) Gt(v interface{}) SqlCondition

func (*Column) Gte

func (c *Column) Gte(v interface{}) SqlCondition

func (*Column) In

func (c *Column) In(args ...interface{}) SqlCondition

func (*Column) Incr

func (c *Column) Incr(d int) SqlExpr

func (*Column) IsNil

func (c *Column) IsNil() bool

func (*Column) IsNotNull

func (c *Column) IsNotNull() SqlCondition

func (*Column) IsNull

func (c *Column) IsNull() SqlCondition

func (*Column) LeftLike

func (c *Column) LeftLike(v string) SqlCondition

func (*Column) Like

func (c *Column) Like(v string) SqlCondition

func (*Column) Lt

func (c *Column) Lt(v interface{}) SqlCondition

func (*Column) Lte

func (c *Column) Lte(v interface{}) SqlCondition

func (*Column) Neq

func (c *Column) Neq(v interface{}) SqlCondition

func (*Column) NotBetween

func (c *Column) NotBetween(leftValue interface{}, rightValue interface{}) SqlCondition

func (*Column) NotIn

func (c *Column) NotIn(args ...interface{}) SqlCondition

func (*Column) NotLike

func (c *Column) NotLike(v string) SqlCondition

func (*Column) Of

func (c *Column) Of(table *Table) *Column

func (Column) On

func (c Column) On(table *Table) *Column

func (*Column) RightLike

func (c *Column) RightLike(v string) SqlCondition

func (*Column) T

func (c *Column) T() *Table

func (Column) Type

func (c Column) Type(v interface{}, tagValue string) *Column

func (*Column) ValueBy

func (c *Column) ValueBy(v interface{}) *Assignment

type ColumnType

type ColumnType struct {
	Type        reflect.Type
	GetDataType func(engine string) string

	Length  uint64
	Decimal uint64

	Default  *string
	OnUpdate *string

	Null          bool
	AutoIncrement bool

	Comment string

	DeprecatedActions *DeprecatedActions
}

func ColumnTypeFromTypeAndTag

func ColumnTypeFromTypeAndTag(typ reflect.Type, nameAndFlags string) *ColumnType

type Columns

type Columns struct {
	// contains filtered or unexported fields
}

func Cols

func Cols(names ...string) *Columns

func (*Columns) Add

func (cols *Columns) Add(columns ...*Column)

func (*Columns) AutoIncrement

func (cols *Columns) AutoIncrement() (col *Column)

func (*Columns) Clone

func (cols *Columns) Clone() *Columns

func (*Columns) Col

func (cols *Columns) Col(columnName string) (col *Column)

func (*Columns) Cols

func (cols *Columns) Cols(colNames ...string) (*Columns, error)

func (*Columns) Ex

func (cols *Columns) Ex(ctx context.Context) *Ex

func (*Columns) F

func (cols *Columns) F(fileName string) (col *Column)

func (*Columns) FieldNames

func (cols *Columns) FieldNames() []string

func (*Columns) Fields

func (cols *Columns) Fields(fieldNames ...string) (*Columns, error)

func (*Columns) IsNil

func (cols *Columns) IsNil() bool

func (*Columns) Len

func (cols *Columns) Len() int

func (*Columns) List

func (cols *Columns) List() (l []*Column)

func (*Columns) MustFields

func (cols *Columns) MustFields(fieldNames ...string) *Columns

func (*Columns) Range

func (cols *Columns) Range(cb func(col *Column, idx int))

func (*Columns) Remove

func (cols *Columns) Remove(name string)

type CombinationAddition

type CombinationAddition struct {
}

func (CombinationAddition) AdditionType

func (CombinationAddition) AdditionType() AdditionType

type CommentAddition

type CommentAddition struct {
}

func (CommentAddition) AdditionType

func (CommentAddition) AdditionType() AdditionType

type ComposedCondition

type ComposedCondition struct {
	SqlConditionMarker
	// contains filtered or unexported fields
}

func (*ComposedCondition) And

func (*ComposedCondition) Ex

func (c *ComposedCondition) Ex(ctx context.Context) *Ex

func (*ComposedCondition) IsNil

func (c *ComposedCondition) IsNil() bool

func (*ComposedCondition) Or

func (*ComposedCondition) Xor

type Condition

type Condition struct {
	SqlConditionMarker
	// contains filtered or unexported fields
}

func AsCond

func AsCond(ex SqlExpr) *Condition

func (*Condition) And

func (c *Condition) And(cond SqlCondition) SqlCondition

func (*Condition) Ex

func (c *Condition) Ex(ctx context.Context) *Ex

func (*Condition) IsNil

func (c *Condition) IsNil() bool

func (*Condition) Or

func (c *Condition) Or(cond SqlCondition) SqlCondition

func (*Condition) Xor

func (c *Condition) Xor(cond SqlCondition) SqlCondition

type DataTypeDescriber

type DataTypeDescriber interface {
	DataType(driverName string) string
}

type DeprecatedActions

type DeprecatedActions struct {
	RenameTo string `name:"rename"`
}

type Dialect

type Dialect interface {
	DriverName() string
	PrimaryKeyName() string
	IsErrorUnknownDatabase(err error) bool
	IsErrorConflict(err error) bool
	CreateDatabase(dbName string) SqlExpr
	CreateSchema(schemaName string) SqlExpr
	DropDatabase(dbName string) SqlExpr
	CreateTableIsNotExists(t *Table) []SqlExpr
	DropTable(t *Table) SqlExpr
	TruncateTable(t *Table) SqlExpr
	AddColumn(col *Column) SqlExpr
	RenameColumn(col *Column, target *Column) SqlExpr
	ModifyColumn(col *Column, prev *Column) SqlExpr
	DropColumn(col *Column) SqlExpr
	AddIndex(key *Key) SqlExpr
	DropIndex(key *Key) SqlExpr
	DataType(columnType *ColumnType) SqlExpr
}

type Ex

type Ex struct {
	*bytes.Buffer
	// contains filtered or unexported fields
}

func ColumnsByStruct

func ColumnsByStruct(v interface{}) *Ex

func Expr

func Expr(query string, args ...interface{}) *Ex

func ResolveExpr

func ResolveExpr(v interface{}) *Ex

func ResolveExprContext

func ResolveExprContext(ctx context.Context, v interface{}) *Ex

func (*Ex) AppendArgs

func (e *Ex) AppendArgs(args ...interface{})

func (*Ex) Args

func (e *Ex) Args() []interface{}

func (*Ex) ArgsLen

func (e *Ex) ArgsLen() int

func (*Ex) Err

func (e *Ex) Err() error

func (Ex) Ex

func (e Ex) Ex(ctx context.Context) *Ex

func (*Ex) IsNil

func (e *Ex) IsNil() bool

func (*Ex) Query

func (e *Ex) Query() string

func (*Ex) WhiteComments

func (e *Ex) WhiteComments(comments []byte)

func (*Ex) WriteEnd

func (e *Ex) WriteEnd()

func (*Ex) WriteExpr

func (e *Ex) WriteExpr(expr SqlExpr)

func (*Ex) WriteGroup

func (e *Ex) WriteGroup(fn func(e *Ex))

func (*Ex) WriteHolder

func (e *Ex) WriteHolder(idx int)

type FieldValues

type FieldValues map[string]interface{}

func FieldValuesFromStructBy

func FieldValuesFromStructBy(structValue interface{}, fieldNames []string) (fieldValues FieldValues)

func FieldValuesFromStructByNonZero

func FieldValuesFromStructByNonZero(structValue interface{}, excludes ...string) (fieldValues FieldValues)

type Function

type Function struct {
	// contains filtered or unexported fields
}

func Avg

func Avg(sqlExprs ...SqlExpr) *Function

func Count

func Count(sqlExprs ...SqlExpr) *Function

func Distinct

func Distinct(sqlExprs ...SqlExpr) *Function

func First

func First(sqlExprs ...SqlExpr) *Function

func Func

func Func(name string, sqlExprs ...SqlExpr) *Function

func Last

func Last(sqlExprs ...SqlExpr) *Function

func Max

func Max(sqlExprs ...SqlExpr) *Function

func Min

func Min(sqlExprs ...SqlExpr) *Function

func Sum

func Sum(sqlExprs ...SqlExpr) *Function

func (*Function) Ex

func (f *Function) Ex(ctx context.Context) *Ex

func (*Function) IsNil

func (f *Function) IsNil() bool

type GroupByAddition

type GroupByAddition struct {
}

func (GroupByAddition) AdditionType

func (GroupByAddition) AdditionType() AdditionType

type Indexes

type Indexes map[string][]string

type JoinAddition

type JoinAddition struct{}

func (JoinAddition) AdditionType

func (JoinAddition) AdditionType() AdditionType

type Key

type Key struct {
	Columns *Columns
	Table   *Table

	Name     string
	IsUnique bool
	Method   string
}

func Index

func Index(name string, columns *Columns) *Key

func PrimaryKey

func PrimaryKey(columns *Columns) *Key

func UniqueIndex

func UniqueIndex(name string, columns *Columns) *Key

func (*Key) IsPrimary

func (key *Key) IsPrimary() bool

func (Key) On

func (key Key) On(table *Table) *Key

func (*Key) T

func (key *Key) T() *Table

func (Key) Using

func (key Key) Using(method string) *Key

type Keys

type Keys struct {
	// contains filtered or unexported fields
}

func (*Keys) Add

func (keys *Keys) Add(nextKeys ...*Key)

func (*Keys) Clone

func (keys *Keys) Clone() *Keys

func (*Keys) IsEmpty

func (keys *Keys) IsEmpty() bool

func (*Keys) Key

func (keys *Keys) Key(keyName string) (key *Key)

func (*Keys) Len

func (keys *Keys) Len() int

func (*Keys) Range

func (keys *Keys) Range(cb func(key *Key, idx int))

func (*Keys) Remove

func (keys *Keys) Remove(name string)

type LimitAddition

type LimitAddition struct {
}

func (LimitAddition) AdditionType

func (LimitAddition) AdditionType() AdditionType

type Model

type Model interface {
	TableName() string
}

type OnConflictAddition

type OnConflictAddition struct {
}

func (OnConflictAddition) AdditionType

func (OnConflictAddition) AdditionType() AdditionType

type Order

type Order struct {
	// contains filtered or unexported fields
}

func AscOrder

func AscOrder(target SqlExpr) *Order

func DescOrder

func DescOrder(target SqlExpr) *Order

func (*Order) Ex

func (o *Order) Ex(ctx context.Context) *Ex

func (*Order) IsNil

func (o *Order) IsNil() bool

type OrderByAddition

type OrderByAddition struct {
}

func (OrderByAddition) AdditionType

func (OrderByAddition) AdditionType() AdditionType

type OtherAddition

type OtherAddition struct {
	SqlExpr
}

func AsAddition

func AsAddition(expr SqlExpr) *OtherAddition

func ForUpdate

func ForUpdate() *OtherAddition

func OnDuplicateKeyUpdate

func OnDuplicateKeyUpdate(assignments ...*Assignment) *OtherAddition

func Returning

func Returning(expr SqlExpr) *OtherAddition

func (OtherAddition) AdditionType

func (OtherAddition) AdditionType() AdditionType

func (*OtherAddition) IsNil

func (a *OtherAddition) IsNil() bool

type SelectStatement

type SelectStatement interface {
	SqlExpr
	// contains filtered or unexported methods
}

type SqlAssignment

type SqlAssignment interface {
	SqlExpr
	SqlAssignmentMarker
}

type SqlAssignmentMarker

type SqlAssignmentMarker interface {
	// contains filtered or unexported methods
}

type SqlCondition

type SqlCondition interface {
	SqlExpr
	SqlConditionMarker

	And(cond SqlCondition) SqlCondition
	Or(cond SqlCondition) SqlCondition
	Xor(cond SqlCondition) SqlCondition
}

func And

func And(conditions ...SqlCondition) SqlCondition

func EmptyCond

func EmptyCond() SqlCondition

func Or

func Or(conditions ...SqlCondition) SqlCondition

func Xor

func Xor(conditions ...SqlCondition) SqlCondition

type SqlConditionMarker

type SqlConditionMarker interface {
	// contains filtered or unexported methods
}

type SqlExpr

type SqlExpr interface {
	IsNil() bool
	Ex(ctx context.Context) *Ex
}

func ExprBy

func ExprBy(build func(ctx context.Context) *Ex) SqlExpr

func Multi

func Multi(exprs ...SqlExpr) SqlExpr

func MultiWith

func MultiWith(connector string, exprs ...SqlExpr) SqlExpr

type StmtDelete

type StmtDelete struct {
	// contains filtered or unexported fields
}

func Delete

func Delete() *StmtDelete

func (*StmtDelete) Ex

func (s *StmtDelete) Ex(ctx context.Context) *Ex

func (StmtDelete) From

func (s StmtDelete) From(table *Table, additions ...Addition) *StmtDelete

func (*StmtDelete) IsNil

func (s *StmtDelete) IsNil() bool

type StmtInsert

type StmtInsert struct {
	// contains filtered or unexported fields
}

https://dev.mysql.com/doc/refman/5.6/en/insert.html

func Insert

func Insert(modifiers ...string) *StmtInsert

func (*StmtInsert) Ex

func (s *StmtInsert) Ex(ctx context.Context) *Ex

func (StmtInsert) Into

func (s StmtInsert) Into(table *Table, additions ...Addition) *StmtInsert

func (*StmtInsert) IsNil

func (s *StmtInsert) IsNil() bool

func (StmtInsert) Values

func (s StmtInsert) Values(cols *Columns, values ...interface{}) *StmtInsert

type StmtSelect

type StmtSelect struct {
	SelectStatement
	// contains filtered or unexported fields
}

func Select

func Select(sqlExpr SqlExpr, modifiers ...string) *StmtSelect

func (*StmtSelect) Ex

func (s *StmtSelect) Ex(ctx context.Context) *Ex

func (StmtSelect) From

func (s StmtSelect) From(table *Table, additions ...Addition) *StmtSelect

func (*StmtSelect) IsNil

func (s *StmtSelect) IsNil() bool

type StmtUpdate

type StmtUpdate struct {
	// contains filtered or unexported fields
}

func Update

func Update(table *Table, modifiers ...string) *StmtUpdate

func (*StmtUpdate) Ex

func (s *StmtUpdate) Ex(ctx context.Context) *Ex

func (*StmtUpdate) IsNil

func (s *StmtUpdate) IsNil() bool

func (StmtUpdate) Set

func (s StmtUpdate) Set(assignments ...*Assignment) *StmtUpdate

func (StmtUpdate) Where

func (s StmtUpdate) Where(c SqlCondition, additions ...Addition) *StmtUpdate

type StructField

type StructField struct {
	Value      reflect.Value
	Field      reflect.StructField
	TableName  string
	ColumnName string
	TagValue   string
}

type Table

type Table struct {
	Name        string
	Description []string

	Schema    string
	ModelName string
	Model     Model

	Columns
	Keys
}

func T

func T(tableName string, tableDefinitions ...TableDefinition) *Table

func TableFromModel

func TableFromModel(model Model) *Table

func (*Table) AddCol

func (t *Table) AddCol(d *Column)

func (*Table) AddKey

func (t *Table) AddKey(key *Key)

func (*Table) AssignmentsByFieldValues

func (t *Table) AssignmentsByFieldValues(fieldValues FieldValues) (assignments Assignments)

func (*Table) ColumnsAndValuesByFieldValues

func (t *Table) ColumnsAndValuesByFieldValues(fieldValues FieldValues) (columns *Columns, args []interface{})

func (*Table) Diff

func (t *Table) Diff(prevTable *Table, dialect Dialect) (exprList []SqlExpr)

func (*Table) Ex

func (t *Table) Ex(ctx context.Context) *Ex

func (*Table) Expr

func (t *Table) Expr(query string, args ...interface{}) *Ex

func (*Table) IsNil

func (t *Table) IsNil() bool

func (*Table) TableName

func (t *Table) TableName() string

func (Table) WithSchema

func (t Table) WithSchema(schema string) *Table

type TableDefinition

type TableDefinition interface {
	T() *Table
}

type Tables

type Tables struct {
	// contains filtered or unexported fields
}

func (*Tables) Add

func (tables *Tables) Add(tabs ...*Table)

func (*Tables) Model

func (tables *Tables) Model(structName string) *Table

func (*Tables) Range

func (tables *Tables) Range(cb func(tab *Table, idx int))

func (*Tables) Remove

func (tables *Tables) Remove(name string)

func (*Tables) Table

func (tables *Tables) Table(tableName string) *Table

func (*Tables) TableNames

func (tables *Tables) TableNames() (names []string)

type Toggles

type Toggles map[string]bool

func TogglesFromContext

func TogglesFromContext(ctx context.Context) Toggles

func (Toggles) Is

func (toggles Toggles) Is(key string) bool

func (Toggles) Merge

func (toggles Toggles) Merge(next Toggles) Toggles

type ValuerExpr

type ValuerExpr interface {
	ValueEx() string
}

replace ? as some query snippet

examples: ? => ST_GeomFromText(?)

type WhereAddition

type WhereAddition struct{}

func (WhereAddition) AdditionType

func (WhereAddition) AdditionType() AdditionType

type WithColDescriptions

type WithColDescriptions interface {
	ColDescriptions() map[string][]string
}

type WithComments

type WithComments interface {
	Comments() map[string]string
}

type WithIndexes

type WithIndexes interface {
	Indexes() Indexes
}

type WithPrimaryKey

type WithPrimaryKey interface {
	PrimaryKey() []string
}

type WithRelations

type WithRelations interface {
	ColRelations() map[string][]string
}

type WithStmt

type WithStmt struct {
	// contains filtered or unexported fields
}

func With

func With(t *Table, build BuildSubQuery, modifiers ...string) *WithStmt

func WithRecursive

func WithRecursive(t *Table, build BuildSubQuery) *WithStmt

func (*WithStmt) Ex

func (w *WithStmt) Ex(ctx context.Context) *Ex

func (WithStmt) Exec

func (w WithStmt) Exec(statement func(tables ...*Table) SqlExpr) *WithStmt

func (*WithStmt) IsNil

func (w *WithStmt) IsNil() bool

func (WithStmt) With

func (w WithStmt) With(t *Table, build BuildSubQuery) *WithStmt

type WithTableDescription

type WithTableDescription interface {
	TableDescription() []string
}

type WithUniqueIndexes

type WithUniqueIndexes interface {
	UniqueIndexes() Indexes
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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