expr

package
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: May 6, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package expr package 用于生成sql语句

Index

Constants

This section is empty.

Variables

View Source
var (
	All          = &RawExpr{Value: "*"}
	NULL         = &ConstantExpr{Value: nil}
	LeftBracket  = &RawExpr{Value: "("}
	RightBracket = &RawExpr{Value: ")"}

	//N = Name
	N = Name
	//V = Value
	V = Var
	//R = Raw
	R = Raw
	F = Fn
	C = Const
)
View Source
var (
	Count    = &FuncExpr{Name: keywords.Count, Args: []Expr{Raw(1)}}
	CountAll = &FuncExpr{Name: keywords.Count, Args: []Expr{Raw("*")}}
)

Functions

func AllToAnd

func AllToAnd(exp Expr)

AllToAnd 将所有的条件转换为and连接

func AllToOr

func AllToOr(exp Expr)

AllToOr 将所有的条件转换为or连接

func AutoFuzzy

func AutoFuzzy(exp Expr)

AutoFuzzy 自动模糊查询,对于字符类型且包含%.?*等字符的,自动转换为like查询

func WithCount

func WithCount(exp Expr)

Types

type AliasExpr

type AliasExpr struct {
	Expr  Expr
	Alias string
}

AliasExpr 别名表达式

func Alias

func Alias(expr Expr, alias string) *AliasExpr

func (*AliasExpr) Format

func (a *AliasExpr) Format(buffer *TracedBuffer)

type AroundExpr

type AroundExpr struct {
	Prefix Expr
	Expr   Expr
	Suffix Expr
}

func Paren

func Paren(expr Expr) *AroundExpr

Paren 括号

func (*AroundExpr) Format

func (s *AroundExpr) Format(buffer *TracedBuffer)

type BetweenExpr

type BetweenExpr struct {
	Left  Expr
	Start Expr
	End   Expr
}

func Between

func Between(left Expr, start Expr, end Expr) *BetweenExpr

func (*BetweenExpr) Format

func (b *BetweenExpr) Format(buffer *TracedBuffer)

type BinaryExpr

type BinaryExpr struct {
	Left     Expr
	Operator string
	Space    string
	Right    Expr
}

BinaryExpr 二元表达式

func Binary

func Binary(left Expr, op string, right any) *BinaryExpr

func Eq

func Eq(left Expr, right any) *BinaryExpr

func Ge

func Ge(left Expr, right any) *BinaryExpr

func Gt

func Gt(left Expr, right any) *BinaryExpr

func In

func In(left Expr, name string, values ...any) *BinaryExpr

func InValues

func InValues(left Expr, values ...Expr) *BinaryExpr

func Le

func Le(left Expr, right any) *BinaryExpr

func Like

func Like(left Expr, right any) *BinaryExpr

func Lt

func Lt(left Expr, right any) *BinaryExpr

func Ne

func Ne(left Expr, right any) *BinaryExpr

func NotIn

func NotIn(left Expr, name string, values ...any) *BinaryExpr

func NotInValues

func NotInValues(left Expr, values ...Expr) *BinaryExpr

func (*BinaryExpr) Format

func (b *BinaryExpr) Format(buffer *TracedBuffer)

func (*BinaryExpr) UseSpace

func (b *BinaryExpr) UseSpace() *BinaryExpr

UseSpace 使用空格

type ConstantExpr

type ConstantExpr struct {
	Value any
}

func Const

func Const(value any) *ConstantExpr

Const 常量, 例如:Const(1), Const("a"),和Raw不同的是,Const会自动将值转换为SQL语句中的常量,例如:Const(1)会被格式化为:1,Const("a")会被格式化为:'a'

func (*ConstantExpr) Format

func (c *ConstantExpr) Format(buffer *TracedBuffer)

type DeleteExpr

type DeleteExpr struct {
	Table     Expr
	WhereExpr Expr
}

func Delete

func Delete(table Expr) *DeleteExpr

func (*DeleteExpr) Delete

func (d *DeleteExpr) Delete(table Expr) *DeleteExpr

func (*DeleteExpr) Format

func (d *DeleteExpr) Format(buf *TracedBuffer)

func (*DeleteExpr) Where

func (d *DeleteExpr) Where(exp Expr) *DeleteExpr

type DeleteExprFn

type DeleteExprFn func(*DeleteExpr)

func UseDeleteCondition

func UseDeleteCondition(exp Expr) DeleteExprFn

type DeleteFilterFn

type DeleteFilterFn func(s *DeleteExpr)

type Expr

type Expr interface {
	Format(buffer *TracedBuffer)
}

func Asc

func Asc(exp Expr) Expr

func AutoNamedValues

func AutoNamedValues(name string, values ...any) []Expr

func Desc

func Desc(exp Expr) Expr

func Sort

func Sort(exp Expr, direction string) Expr

func Sorts

func Sorts(direction string, exps ...Expr) Expr

type FilterFn

type FilterFn func(Expr)

func DeleteFilter

func DeleteFilter(fn DeleteFilterFn) FilterFn

func InsertFilter

func InsertFilter(fn InsertFilterFn) FilterFn

func SelectFilter

func SelectFilter(fn SelectFilterFn) FilterFn

func Set

func Set(exps ...Expr) FilterFn

func UpdateFilter

func UpdateFilter(fn UpdateFilterFn) FilterFn

func UseCondition

func UseCondition(exp Expr) FilterFn

UseCondition 使用条件

func UseLimit

func UseLimit(limit int) FilterFn

func UseLimits

func UseLimits(limit int, offset int) FilterFn

func UseOffset

func UseOffset(offset int) FilterFn

func UseOrderBy

func UseOrderBy(exp Expr) FilterFn

func UseSort

func UseSort(direct string, exprs ...Expr) FilterFn

type Filters

type Filters []FilterFn

func (*Filters) Append

func (f *Filters) Append(filters ...FilterFn) *Filters

func (*Filters) Apply

func (f *Filters) Apply(exp Expr)

func (*Filters) ApplyAll

func (f *Filters) ApplyAll(exps []Expr)

func (*Filters) Delete

func (f *Filters) Delete(filters ...FilterFn) *Filters

func (*Filters) Select

func (f *Filters) Select(filters ...FilterFn) *Filters

func (*Filters) Update

func (f *Filters) Update(filters ...FilterFn)

type FuncExpr

type FuncExpr struct {
	Name string
	Args []Expr
}

func Fn

func Fn(name string, args ...Expr) *FuncExpr

func (*FuncExpr) Format

func (f *FuncExpr) Format(buffer *TracedBuffer)

type InsertExpr

type InsertExpr struct {
	Table      Expr
	ValueExprs []*BinaryExpr
}

InsertExpr is a struct for insert expression

func InsertInto

func InsertInto(table Expr, values ...*BinaryExpr) *InsertExpr

InsertInto 创建一个InsertExpr并设置表名

func (*InsertExpr) Format

func (i *InsertExpr) Format(buf *TracedBuffer)

func (*InsertExpr) Into

func (i *InsertExpr) Into(table Expr) *InsertExpr

Into is a function to set table

func (*InsertExpr) Set

func (i *InsertExpr) Set(colName string, value any) *InsertExpr

func (*InsertExpr) SetExpr

func (i *InsertExpr) SetExpr(name Expr, value Expr) *InsertExpr

func (*InsertExpr) SetMap

func (i *InsertExpr) SetMap(data map[string]any) *InsertExpr

func (*InsertExpr) Values

func (i *InsertExpr) Values(values ...*BinaryExpr) *InsertExpr

type InsertFilterFn

type InsertFilterFn func(s *InsertExpr)

type ListExpr

type ListExpr struct {
	Prefix      Expr
	Separator   string
	Placeholder string
	ExprList    []Expr
	Suffix      Expr
}

func And

func And(exprList ...Expr) *ListExpr

And 与

func List

func List(operator string, exprList ...Expr) *ListExpr

func Or

func Or(exprList ...Expr) *ListExpr

func (*ListExpr) Format

func (l *ListExpr) Format(buffer *TracedBuffer)

type NameExpr

type NameExpr struct {
	Qualifier []string
	Name      string
}

func Name

func Name(name string, qualifiers ...string) *NameExpr

Name 字段名、表名、别名等 例如:Name("id"):mysql 驱动下则会被格式化为: `id` 例如:如果有限定名称Name("id", "user"),则会被格式化为: `user`.`id`,如果有多个限定名称Name("id", "user", "t"),则会被格式化为: `user`.`t`.`id`

func (*NameExpr) Between

func (n *NameExpr) Between(min, max any) *BetweenExpr

func (*NameExpr) Eq

func (n *NameExpr) Eq(value any) *BinaryExpr

func (*NameExpr) Format

func (n *NameExpr) Format(buffer *TracedBuffer)

func (*NameExpr) Ge

func (n *NameExpr) Ge(value any) *BinaryExpr

func (*NameExpr) Gt

func (n *NameExpr) Gt(value any) *BinaryExpr

func (*NameExpr) In

func (n *NameExpr) In(values ...any) *BinaryExpr

func (*NameExpr) Le

func (n *NameExpr) Le(value any) *BinaryExpr

func (*NameExpr) Like

func (n *NameExpr) Like(value any) *BinaryExpr

func (*NameExpr) Lt

func (n *NameExpr) Lt(value any) *BinaryExpr

func (*NameExpr) Ne

func (n *NameExpr) Ne(value any) *BinaryExpr

func (*NameExpr) NotIn

func (n *NameExpr) NotIn(values ...any) *BinaryExpr

type RawExpr

type RawExpr struct {
	Value any
}

func Raw

func Raw(value any) *RawExpr

func (*RawExpr) Format

func (r *RawExpr) Format(buffer *TracedBuffer)

type SelectExpr

type SelectExpr struct {
	Columns     Expr
	FromExpr    Expr
	WhereExpr   Expr
	GroupByExpr Expr
	HavingExpr  Expr
	OrderByExpr Expr
	// contains filtered or unexported fields
}

func Select

func Select(columns ...Expr) *SelectExpr

func (*SelectExpr) BuildCountExpr

func (s *SelectExpr) BuildCountExpr() *SelectExpr

func (*SelectExpr) Format

func (s *SelectExpr) Format(buffer *TracedBuffer)

func (*SelectExpr) From

func (s *SelectExpr) From(from Expr) *SelectExpr

func (*SelectExpr) GroupBy

func (s *SelectExpr) GroupBy(exp Expr) *SelectExpr

func (*SelectExpr) Having

func (s *SelectExpr) Having(exp Expr) *SelectExpr

func (*SelectExpr) Limit

func (s *SelectExpr) Limit(limit int) *SelectExpr

func (*SelectExpr) Offset

func (s *SelectExpr) Offset(offset int) *SelectExpr

func (*SelectExpr) OrderBy

func (s *SelectExpr) OrderBy(exps ...Expr) *SelectExpr

func (*SelectExpr) Select

func (s *SelectExpr) Select(columns ...Expr) *SelectExpr

func (*SelectExpr) UseCount

func (s *SelectExpr) UseCount() bool

func (*SelectExpr) Where

func (s *SelectExpr) Where(exp Expr) *SelectExpr

func (*SelectExpr) WithCount

func (s *SelectExpr) WithCount() *SelectExpr

func (*SelectExpr) WithoutCount

func (s *SelectExpr) WithoutCount() *SelectExpr

type SelectFilterFn

type SelectFilterFn func(s *SelectExpr)

type TracedBuffer

type TracedBuffer struct {
	NamedVar bool
	*dialect.Driver
	strings.Builder
	// contains filtered or unexported fields
}

TracedBuffer is a buffer that can be used to trace the

func NewTracedBuffer

func NewTracedBuffer(driver *dialect.Driver) *TracedBuffer

func (*TracedBuffer) Append

func (t *TracedBuffer) Append(buf []byte) *TracedBuffer

func (*TracedBuffer) AppendArg

func (t *TracedBuffer) AppendArg(value any) *TracedBuffer

func (*TracedBuffer) AppendExprs

func (t *TracedBuffer) AppendExprs(exprs ...Expr) *TracedBuffer

func (*TracedBuffer) AppendKeyword

func (t *TracedBuffer) AppendKeyword(keyword string) *TracedBuffer

func (*TracedBuffer) AppendKeywordWithSpace

func (t *TracedBuffer) AppendKeywordWithSpace(keyword string) *TracedBuffer

func (*TracedBuffer) AppendNamedArg

func (t *TracedBuffer) AppendNamedArg(name string, value any) *TracedBuffer

func (*TracedBuffer) AppendString

func (t *TracedBuffer) AppendString(s string) *TracedBuffer

func (*TracedBuffer) Build

func (t *TracedBuffer) Build(exp Expr) (string, []any, error)

func (*TracedBuffer) BuildNamed

func (t *TracedBuffer) BuildNamed(exp Expr) (string, map[string]any, error)

func (*TracedBuffer) NewLine

func (t *TracedBuffer) NewLine() *TracedBuffer

type Tuple

type Tuple struct {
	Name  Expr
	Value Expr
}

type UnaryExpr

type UnaryExpr struct {
	Operator string
	Expr     Expr
}

UnaryExpr 一元表达式

func Not

func Not(expr Expr) *UnaryExpr

Not 非, 例如:Not(In(Name("id"), 1,2))

func Unary

func Unary(operator string, expr Expr) *UnaryExpr

func (*UnaryExpr) Format

func (u *UnaryExpr) Format(buffer *TracedBuffer)

type UpdateExpr

type UpdateExpr struct {
	Table     Expr
	Values    []Expr
	WhereExpr Expr
}

func Update

func Update(table Expr) *UpdateExpr

func (*UpdateExpr) Format

func (u *UpdateExpr) Format(buf *TracedBuffer)

func (*UpdateExpr) Set

func (u *UpdateExpr) Set(values ...Expr) *UpdateExpr

func (*UpdateExpr) Update

func (u *UpdateExpr) Update(table Expr) *UpdateExpr

func (*UpdateExpr) Where

func (u *UpdateExpr) Where(exp Expr) *UpdateExpr

type UpdateFilterFn

type UpdateFilterFn func(s *UpdateExpr)

type ValueExpr

type ValueExpr struct {
	Name  string
	Value any
}

ValueExpr 值表达式

func Var

func Var(name string, value any) *ValueExpr

func (*ValueExpr) Format

func (n *ValueExpr) Format(buffer *TracedBuffer)

Format 格式化, 如果是命名参数, 则使用命名参数,否则使用占位符

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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