builder

package
v3.0.0-...-e2af5f4 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsExpression

func IsExpression(obj any) (b bool)

func ToSlice

func ToSlice(arg any) []any

Types

type Column

type Column struct {
	Name  string
	Alias string // 可选别名
	IsRaw bool   // 是否是原生SQL片段
	Binds []any  // 绑定数据
}

Column 表示SELECT语句中的列信息。

type Context

type Context struct {
	TableClause       TableClause
	SelectClause      SelectClause
	JoinClause        JoinClause
	WhereClause       WhereClause
	GroupClause       GroupClause
	HavingClause      HavingClause
	OrderByClause     OrderByClause
	LimitOffsetClause LimitOffsetClause
	UnionClause       UnionClause

	PessimisticLocking string
	Prefix             string
}

func NewContext

func NewContext(prefix string) *Context

func (*Context) AddSelect

func (db *Context) AddSelect(columns ...string) *Context

func (*Context) CrossJoin

func (db *Context) CrossJoin(table any, argOrFn ...any) *Context

func (*Context) GroupBy

func (db *Context) GroupBy(columns ...string) *Context

func (*Context) GroupByRaw

func (db *Context) GroupByRaw(columns ...string) *Context

func (*Context) Having

func (db *Context) Having(column any, argsOrClosure ...any) *Context

func (*Context) HavingRaw

func (db *Context) HavingRaw(raw string, argsOrClosure ...any) *Context

func (*Context) Join

func (db *Context) Join(table any, argOrFn ...any) *Context

func (*Context) LeftJoin

func (db *Context) LeftJoin(table any, argOrFn ...any) *Context

func (*Context) Limit

func (db *Context) Limit(limit int) *Context

func (*Context) LockForUpdate

func (db *Context) LockForUpdate() *Context

func (*Context) Offset

func (db *Context) Offset(offset int) *Context

func (*Context) OrHaving

func (db *Context) OrHaving(column any, argsOrClosure ...any) *Context

func (*Context) OrHavingRaw

func (db *Context) OrHavingRaw(raw string, argsOrClosure ...any) *Context

func (*Context) OrWhere

func (db *Context) OrWhere(column any, argsOrclosure ...any) *Context

func (*Context) OrWhereBuilder

func (db *Context) OrWhereBuilder(column string, operation string, sub IBuilder) *Context

func (*Context) OrWhereNested

func (db *Context) OrWhereNested(handler WhereNestedHandler) *Context

func (*Context) OrWhereRaw

func (db *Context) OrWhereRaw(raw string, bindings ...any) *Context

func (*Context) OrWhereSub

func (db *Context) OrWhereSub(column string, operation string, sub WhereSubHandler) *Context

func (*Context) OrderBy

func (db *Context) OrderBy(column string, directions ...string) *Context

func (*Context) OrderByRaw

func (db *Context) OrderByRaw(column string) *Context

func (*Context) Page

func (db *Context) Page(num int) *Context

func (*Context) RightJoin

func (db *Context) RightJoin(table any, argOrFn ...any) *Context

func (*Context) Select

func (db *Context) Select(columns ...string) *Context

func (*Context) SelectRaw

func (db *Context) SelectRaw(raw string, binds ...any) *Context

func (*Context) SharedLock

func (db *Context) SharedLock() *Context

func (*Context) Table

func (db *Context) Table(table any, alias ...string) *Context

func (*Context) Where

func (db *Context) Where(column any, argsOrclosure ...any) *Context

func (*Context) WhereBuilder

func (db *Context) WhereBuilder(column string, operation string, sub IBuilder) *Context

func (*Context) WhereNested

func (db *Context) WhereNested(handler WhereNestedHandler) *Context

func (*Context) WhereRaw

func (db *Context) WhereRaw(raw string, bindings ...any) *Context

func (*Context) WhereSub

func (db *Context) WhereSub(column string, operation string, sub WhereSubHandler) *Context

type GroupClause

type GroupClause struct {
	Groups []TypeGroupItem
}

func (*GroupClause) GroupBy

func (db *GroupClause) GroupBy(columns ...string)

GroupBy 添加 GROUP BY 子句

func (*GroupClause) GroupByRaw

func (db *GroupClause) GroupByRaw(columns ...string)

type HavingClause

type HavingClause struct {
	WhereClause
}

HavingClause 类似于WhereClause,但应用于HAVING子句。

type IBuilder

type IBuilder interface {
	ToSql() (sql4prepare string, binds []any, err error)
	ToSqlSelect() (sql4prepare string, binds []any)
	ToSqlTable() (sql4prepare string, values []any, err error)
	ToSqlJoin() (sql4prepare string, binds []any, err error)
	ToSqlWhere() (sql4prepare string, values []any, err error)
	ToSqlOrderBy() (sql4prepare string)
	ToSqlLimitOffset() (sqlSegment string, binds []any)
	ToSqlInsert(obj any, args ...TypeToSqlInsertCase) (sqlSegment string, binds []any, err error)
	ToSqlDelete(obj any, mustColumn ...string) (sqlSegment string, binds []any, err error)
	ToSqlUpdate(obj any, mustColumn ...string) (sqlSegment string, binds []any, err error)
	ToSqlIncDec(symbol string, data map[string]any) (sql4prepare string, values []any, err error)
}

type IJoinOn

type IJoinOn interface {
	On(column string, args ...string) IJoinOn
	OrOn(column string, args ...string) IJoinOn
}

type IWhere

type IWhere interface {
	Where(column any, args ...any) IWhere
	OrWhere(column any, args ...any) IWhere
	WhereRaw(raw string, bindingsAndBoolean ...any) IWhere
	OrWhereRaw(sqlSeg string, bindingsAndBoolean ...any) IWhere
	WhereBetween(column string, values any) IWhere
	OrWhereBetween(column string, values any) IWhere
	WhereNotBetween(column string, values any) IWhere
	OrWhereNotBetween(column string, values any) IWhere
	WhereIn(column string, values any) IWhere
	OrWhereIn(column string, values any) IWhere
	WhereNotIn(column string, values any) IWhere
	OrWhereNotIn(column string, values any) IWhere
	WhereNull(column string) IWhere
	OrWhereNull(column string) IWhere
	WhereNotNull(column string) IWhere
	OrWhereNotNull(column string) IWhere
	WhereLike(column string, value string) IWhere
	OrWhereLike(column string, value string) IWhere
	WhereNotLike(column string, value string) IWhere
	OrWhereNotLike(column string, value string) IWhere

	WhereBuilder(column string, operation string, build IBuilder) IWhere
	OrWhereBuilder(column string, operation string, sub IBuilder) IWhere
	WhereSub(column string, operation string, sub WhereSubHandler) IWhere
	OrWhereSub(column string, operation string, sub WhereSubHandler) IWhere
	WhereNested(handler WhereNestedHandler) IWhere
	OrWhereNested(handler WhereNestedHandler) IWhere

	WhereExists(clause IBuilder) IWhere
	WhereNotExists(clause IBuilder) IWhere
}

type JoinClause

type JoinClause struct {
	JoinItems []any
	Err       error
}

JoinClause 描述JOIN操作

func (*JoinClause) CrossJoin

func (db *JoinClause) CrossJoin(table any, argOrFn ...any) *JoinClause

CrossJoin 描述CROSS JOIN操作

func (*JoinClause) Join

func (db *JoinClause) Join(table any, argOrFn ...any) *JoinClause

func (*JoinClause) JoinOn

func (db *JoinClause) JoinOn(table any, fn func(on IJoinOn)) *JoinClause

func (*JoinClause) LeftJoin

func (db *JoinClause) LeftJoin(table any, argOrFn ...any) *JoinClause

LeftJoin 描述LEFT JOIN操作

func (*JoinClause) RightJoin

func (db *JoinClause) RightJoin(table any, argOrFn ...any) *JoinClause

RightJoin 描述RIGHT JOIN操作

type LimitOffsetClause

type LimitOffsetClause struct {
	Limit  int
	Offset int
	Page   int
}

LimitOffsetClause 存储LIMIT和OFFSET信息。

type OrderByClause

type OrderByClause struct {
	Columns []OrderByItem
}

OrderByClause 存储排序信息。

func (*OrderByClause) OrderBy

func (db *OrderByClause) OrderBy(column string, directions ...string)

OrderBy adds an ORDER BY clause to the query.

func (*OrderByClause) OrderByRaw

func (db *OrderByClause) OrderByRaw(column string)

OrderByRaw adds a Raw ORDER BY clause to the query.

type OrderByItem

type OrderByItem struct {
	Column    string
	Direction string // "asc" 或 "desc"
	IsRaw     bool
}

type SelectClause

type SelectClause struct {
	Columns  []Column
	Distinct bool
}

SelectClause 存储SELECT子句相关信息。

func (*SelectClause) AddSelect

func (db *SelectClause) AddSelect(columns ...string) *SelectClause

func (*SelectClause) Select

func (db *SelectClause) Select(columns ...string) *SelectClause

Select specifies the columns to retrieve. Select("a","b") Select("a.id as aid","b.id bid") Select("id,nickname name")

func (*SelectClause) SelectRaw

func (db *SelectClause) SelectRaw(raw string, binds ...any) *SelectClause

SelectRaw 允许直接在查询中插入原始SQL片段作为选择列。

type TableClause

type TableClause struct {
	Tables any // table name or struct(slice) or subQuery
	Alias  string
}

TableClause table clause

func (*TableClause) Table

func (db *TableClause) Table(table any, alias ...string)

Table sets the table name for the query.

type TypeGroupItem

type TypeGroupItem struct {
	Column string
	IsRaw  bool
}

type TypeJoinOn

type TypeJoinOn struct {
	TableClause
	OnClause func(IJoinOn)
	Type     string // JOIN类型(INNER, LEFT, RIGHT等)
}

type TypeJoinOnCondition

type TypeJoinOnCondition struct {
	Conditions []TypeJoinOnConditionItem
}

func (*TypeJoinOnCondition) On

func (jc *TypeJoinOnCondition) On(column1 string, operatorOrColumn2 ...string) IJoinOn

func (*TypeJoinOnCondition) OrOn

func (jc *TypeJoinOnCondition) OrOn(column1 string, operatorOrColumn2 ...string) IJoinOn

type TypeJoinOnConditionItem

type TypeJoinOnConditionItem struct {
	Relation string // and/or
	Column1  string
	Operator string
	Column2  string
}

type TypeJoinStandard

type TypeJoinStandard struct {
	TableClause
	Type     string // JOIN类型(INNER, LEFT, RIGHT等)
	Column1  string
	Operator string
	Column2  string
}

type TypeJoinSub

type TypeJoinSub struct {
	IBuilder
}

TypeJoinSub 描述JOIN操作

type TypeToSqlIncDecCase

type TypeToSqlIncDecCase struct {
	Symbol string
	Data   map[string]any
}

type TypeToSqlInsertCase

type TypeToSqlInsertCase struct {
	IsReplace       bool
	IsIgnoreCase    bool
	OnDuplicateKeys []string
	UpdateFields    []string
	MustColumn      []string
}

type TypeToSqlUpdateCase

type TypeToSqlUpdateCase struct {
	BindOrData any
	MustColumn []string
}

type TypeWhereBetween

type TypeWhereBetween struct {
	LogicalOp string
	Column    string
	Operator  string
	Value     any
}

type TypeWhereExists

type TypeWhereExists struct {
	IBuilder
	Not bool
}

type TypeWhereIn

type TypeWhereIn struct {
	LogicalOp string
	Column    string
	Operator  string
	Value     any
}

type TypeWhereNested

type TypeWhereNested struct {
	LogicalOp   string
	WhereNested WhereNestedHandler
}

type TypeWhereRaw

type TypeWhereRaw struct {
	LogicalOp string
	Column    string
	Bindings  []any
}

type TypeWhereStandard

type TypeWhereStandard struct {
	LogicalOp string
	Column    string
	Operator  string
	Value     any
}

type TypeWhereSubHandler

type TypeWhereSubHandler struct {
	LogicalOp string
	Column    string
	Operator  string
	Sub       WhereSubHandler
}

type TypeWhereSubQuery

type TypeWhereSubQuery struct {
	LogicalOp string
	Column    string
	Operator  string
	SubQuery  IBuilder
}

type UnionClause

type UnionClause struct {
	Unions []UnionItem
}

func (*UnionClause) Union

func (u *UnionClause) Union(b ...IBuilder) *UnionClause

func (*UnionClause) UnionAll

func (u *UnionClause) UnionAll(b ...IBuilder) *UnionClause

type UnionItem

type UnionItem struct {
	IBuilder
	IsUnionAll bool
}

type WhereClause

type WhereClause struct {
	Conditions []any
	Not        bool
	Err        error
}

WhereClause 存储所有WHERE条件 ///////////////////start

func (*WhereClause) OrWhere

func (w *WhereClause) OrWhere(column any, args ...any) IWhere

OrWhere clause

func (*WhereClause) OrWhereBetween

func (w *WhereClause) OrWhereBetween(column string, values any) IWhere

func (*WhereClause) OrWhereBuilder

func (w *WhereClause) OrWhereBuilder(column string, operation string, sub IBuilder) IWhere

func (*WhereClause) OrWhereIn

func (w *WhereClause) OrWhereIn(column string, values any) IWhere

func (*WhereClause) OrWhereLike

func (w *WhereClause) OrWhereLike(column string, value string) IWhere

func (*WhereClause) OrWhereNested

func (w *WhereClause) OrWhereNested(handler WhereNestedHandler) IWhere

func (*WhereClause) OrWhereNot

func (w *WhereClause) OrWhereNot(column any, args ...any) IWhere

func (*WhereClause) OrWhereNotBetween

func (w *WhereClause) OrWhereNotBetween(column string, values any) IWhere

func (*WhereClause) OrWhereNotIn

func (w *WhereClause) OrWhereNotIn(column string, values any) IWhere

func (*WhereClause) OrWhereNotLike

func (w *WhereClause) OrWhereNotLike(column string, value string) IWhere

func (*WhereClause) OrWhereNotNull

func (w *WhereClause) OrWhereNotNull(column string) IWhere

func (*WhereClause) OrWhereNull

func (w *WhereClause) OrWhereNull(column string) IWhere

func (*WhereClause) OrWhereRaw

func (w *WhereClause) OrWhereRaw(sqlSeg string, bindingsAndBoolean ...any) IWhere

OrWhereRaw clause

func (*WhereClause) OrWhereSub

func (w *WhereClause) OrWhereSub(column string, operation string, sub WhereSubHandler) IWhere

func (*WhereClause) Where

func (w *WhereClause) Where(column any, args ...any) IWhere

Where Add a basic where clause to the query.

where($column, $operator = null, $value = null, $boolean = 'and')

Parameters:

array|Closure|Expression|string $column
mixed $operator
mixed $value
string $boolean

Returns:

iface.WhereClause

Examples:

Where("id=1")
Where("id=?",1)
Where("id",1)
Where("id","=",1)
Where("id","=",1,"AND")
Where("id","=",(select id from table limit 1))
Where("id","in",(select id from table), "AND")
Where(func(wh iface.WhereClause){wh.Where().OrWhere().WhereRaw()...})
Where(["id=1"])
Where(["id","=",1])
Where(["id",1])
Where([ ["id",1],["name","=","John"],["age",">",3] ])

func (*WhereClause) WhereBetween

func (w *WhereClause) WhereBetween(column string, values any) IWhere

WhereBetween 在指定列的值位于给定范围内时添加一个"where"条件。

relation: and/or column: 列名。 values: 区间范围数组。 not: 是否取反,默认为 false。

func (*WhereClause) WhereBuilder

func (w *WhereClause) WhereBuilder(column string, operation string, build IBuilder) IWhere

func (*WhereClause) WhereExists

func (w *WhereClause) WhereExists(clause IBuilder) IWhere

WhereExists 使用WHERE EXISTS子查询条件。

clause: Database 语句,或者实现了 IBuilder.ToSql() 接口的对象

func (*WhereClause) WhereIn

func (w *WhereClause) WhereIn(column string, values any) IWhere

WhereIn 在指定列的值存在于给定的集合内时添加一个"where"条件。

relation: and/or column: 要检查的列名。 values: 集合值。 not: 是否取反,默认为 false。

func (*WhereClause) WhereLike

func (w *WhereClause) WhereLike(column string, value string) IWhere

WhereLike 在指定列进行模糊匹配时添加一个"where"条件。

relation: and/or column: 要进行模糊匹配的列名。 value: 包含通配符(%)的匹配字符串。

func (*WhereClause) WhereNested

func (w *WhereClause) WhereNested(handler WhereNestedHandler) IWhere

func (*WhereClause) WhereNot

func (w *WhereClause) WhereNot(column any, args ...any) IWhere

func (*WhereClause) WhereNotBetween

func (w *WhereClause) WhereNotBetween(column string, values any) IWhere

func (*WhereClause) WhereNotExists

func (w *WhereClause) WhereNotExists(clause IBuilder) IWhere

func (*WhereClause) WhereNotIn

func (w *WhereClause) WhereNotIn(column string, values any) IWhere

func (*WhereClause) WhereNotLike

func (w *WhereClause) WhereNotLike(column string, value string) IWhere

func (*WhereClause) WhereNotNull

func (w *WhereClause) WhereNotNull(column string) IWhere

func (*WhereClause) WhereNull

func (w *WhereClause) WhereNull(column string) IWhere

WhereNull 指定列的值为 NULL 时添加一个"where"条件。

relation: and/or column: 列名。

func (*WhereClause) WhereRaw

func (w *WhereClause) WhereRaw(raw string, bindingsAndBoolean ...any) IWhere

WhereRaw Add a raw where clause to the query.

whereRaw($sql, $bindings = [], $boolean = 'and')

Parameters:

string $sql
mixed $bindings
string $boolean

Returns:

SubQuery

Laravel api

func (*WhereClause) WhereSub

func (w *WhereClause) WhereSub(column string, operation string, sub WhereSubHandler) IWhere

type WhereNestedHandler

type WhereNestedHandler func(where IWhere)

type WhereSubHandler

type WhereSubHandler func(tx *Context)

Jump to

Keyboard shortcuts

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