sqlbuilder

package
v1.1.0-rc09 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2024 License: MIT Imports: 12 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTableNameRequire = errors.New("[sqlbuilder] tableName requires")
	ErrUpdateMissWhere  = errors.New("[sqlbuilder] where express requires with update")
	ErrColumnsRequire   = errors.New("[sqlbuilder] columns requires")
	ErrDeleteMissWhere  = errors.New("[sqlbuilder] delete sql miss where")
	ErrExecerNotSet     = errors.New("[sqlbuilder] execer not set")
	ErrQueryerNotSet    = errors.New("[sqlbuilder] queryer not set")
)
View Source
var (
	OpLT      = Op{Symbol: "<", Text: " < "}
	OpLTEQ    = Op{Symbol: "<=", Text: " <= "}
	OpGT      = Op{Symbol: ">", Text: " > "}
	OpGTEQ    = Op{Symbol: ">=", Text: " >= "}
	OpEQ      = Op{Symbol: "=", Text: " = "}
	OpNEQ     = Op{Symbol: "!=", Text: " != "}
	OpAnd     = Op{Symbol: "AND", Text: " AND "}
	OpOr      = Op{Symbol: "OR", Text: " OR "}
	OpIn      = Op{Symbol: "IN", Text: " IN "}
	OpNotIN   = Op{Symbol: "NOT IN", Text: " NOT IN "}
	OpLike    = Op{Symbol: "LIKE", Text: " LIKE "}
	OpNotLike = Op{Symbol: "NOT LIKE", Text: " NOT LIKE "}
)

Functions

func GetColumnsByModel

func GetColumnsByModel(mapper *reflectx.Mapper, model any, omitColumns ...string) []string

GetColumnsByModel 解析 model 所有字段名

func GetColumnsByType

func GetColumnsByType(mapper *reflectx.Mapper, typ reflect.Type, omitColumns ...string) []string

GetColumnsByType 通过字段 tag 解析数据库字段

func GetMapperByTagName

func GetMapperByTagName(tagName string) *reflectx.Mapper

GetMapperByTagName 根据 tag name 返回对应 mapper

Types

type Builder

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

func New

func New(tableName string) *Builder

New 创建 sql builder

func (*Builder) Delete

func (b *Builder) Delete() *Deleter

Delete 创建 delete 语句构造器

func (*Builder) Insert

func (b *Builder) Insert(columns ...string) *Inserter

Insert 创建 insert 语句构造器

func (*Builder) Select

func (b *Builder) Select(columns ...string) *Selector

Select 创建 select 语句构造器

func (*Builder) Update

func (b *Builder) Update(columns ...string) *Updater

Update 创建 update 语句构造器

type Column

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

Column 表字段

func Col

func Col(c string) Column

Col 表字段

func (Column) EQ

func (c Column) EQ(val any) Column

EQ =

func (Column) Express

func (c Column) Express() string

Express 输出 sql 表达式

func (Column) GT

func (c Column) GT(val any) Column

GT >

func (Column) GTEQ

func (c Column) GTEQ(val any) Column

GTEQ >=

func (Column) HasInSQL

func (c Column) HasInSQL() bool

HasInSQL 是否有 in 语句

func (Column) In

func (c Column) In(vals ...any) Column

In -> in ()

func (Column) LT

func (c Column) LT(val any) Column

LT <

func (Column) LTEQ

func (c Column) LTEQ(val any) Column

LTEQ <=

func (Column) Like

func (c Column) Like(val any) Column

Like -> LIKE %XXX

func (Column) NotEQ

func (c Column) NotEQ(val any) Column

NotEQ !=

func (Column) NotIn

func (c Column) NotIn(vals ...any) Column

NotIn -> not in ()

func (Column) NotLike

func (c Column) NotLike(val any) Column

NotLike -> NOT LIKE %XXX 、_x_ 、xx[xx-xx] 、xx[^xx-xx]

func (Column) Use

func (c Column) Use(use bool) Column

Use 是否使用

type Condition

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

Condition 条件构造器实现

func C

func C(cols ...Column) *Condition

C 创建 Condition 条件构造器

func (*Condition) And

func (e *Condition) And(cols ...Column) *Condition

And 增加 and 条件

func (*Condition) Or

func (e *Condition) Or(c Column) *Condition

Or 增加 and 条件

type ConditionBuilder

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

ConditionBuilder 条件构造器

type Deleter

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

Deleter delete 语句构造器

func NewDeleter

func NewDeleter(tableName string) *Deleter

NewDeleter tableName 数据库表名

func (*Deleter) Exec added in v1.1.0

func (d *Deleter) Exec() (int64, error)

Exec 执行更新语句

func (*Deleter) ExecContext added in v1.1.0

func (d *Deleter) ExecContext(ctx context.Context) (int64, error)

ExecContext 执行更新语句

func (*Deleter) Execer added in v1.1.0

func (d *Deleter) Execer(execer engine.Execer) *Deleter

Execer 设置Execer

func (*Deleter) Limit

func (d *Deleter) Limit(limit int) *Deleter

Limit 限制删除数量

func (*Deleter) SQL

func (d *Deleter) SQL() (string, error)

SQL 输出sql语句

func (*Deleter) SQLArgs

func (d *Deleter) SQLArgs() (string, []any, error)

SQLArgs 构造 sql 并返回对应参数

func (*Deleter) Where

func (d *Deleter) Where(where ConditionBuilder) *Deleter

Where 条件 condition 可以通过 sqlbuilder.C() 方法创建

type Field added in v1.1.0

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

Field 表更新字段

func F added in v1.1.0

func F(col string) Field

F 创建更新字段

func (Field) Incr added in v1.1.0

func (f Field) Incr(n int64) Field

Incr 设置字段增加值

func (Field) Use added in v1.1.0

func (f Field) Use(use bool) Field

Use 是否启用

func (Field) Val added in v1.1.0

func (f Field) Val(val any) Field

Val 设置字段值

type Inserter

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

Inserter insert 语句构造器

func NewInserter

func NewInserter(tableName string) *Inserter

NewInserter 创建 insert 语句构造器

func (*Inserter) Columns

func (ins *Inserter) Columns(columns ...string) *Inserter

Columns insert 字段

func (*Inserter) Exec added in v1.1.0

func (ins *Inserter) Exec() (lastID int64, affected int64, err error)

Exec 执行 insert 语句 执行 Exec 方法,需要通过 Fields 方法赋值,否则使用 NamedExec

func (*Inserter) ExecContext added in v1.1.0

func (ins *Inserter) ExecContext(ctx context.Context) (lastID int64, affected int64, err error)

ExecContext 执行更新语句 执行 Exec 方法,需要通过 Fields 方法赋值,否则使用 NamedExec

func (*Inserter) Execer added in v1.1.0

func (ins *Inserter) Execer(execer engine.Execer) *Inserter

Execer 设置 execer

func (*Inserter) Fields added in v1.1.0

func (ins *Inserter) Fields(fields ...Field) *Inserter

Fields 设置 insert 字段和值

func (*Inserter) IsIgnoreInto

func (ins *Inserter) IsIgnoreInto(ignoreInto bool) *Inserter

IsIgnoreInto 是否使用 ignore into

func (*Inserter) IsReplaceInto

func (ins *Inserter) IsReplaceInto(replaceInto bool) *Inserter

IsReplaceInto 是否使用 replace into

func (*Inserter) NameSQL

func (ins *Inserter) NameSQL() (string, error)

NameSQL 返回名称风格的 sql

func (*Inserter) NamedExec added in v1.1.0

func (ins *Inserter) NamedExec(model any) (lastID int64, affected int64, err error)

NamedExec 通过 NameSQL 执行更新语句,参数通过 data 填充

func (*Inserter) NamedExecContext added in v1.1.0

func (ins *Inserter) NamedExecContext(ctx context.Context, model any) (lastID int64, affected int64, err error)

NamedExecContext 通过 NameSQL 执行更新语句,参数通过 data 填充

func (*Inserter) OnDuplicateKeyUpdate added in v1.1.0

func (ins *Inserter) OnDuplicateKeyUpdate(fields ...Field) *Inserter

OnDuplicateKeyUpdate 设置 on duplicate key update 字段

func (*Inserter) OnDuplicateKeyUpdateString

func (ins *Inserter) OnDuplicateKeyUpdateString(updateString string) *Inserter

OnDuplicateKeyUpdateString 设置 on duplicate key update 字段

func (*Inserter) SQL

func (ins *Inserter) SQL() (string, error)

SQL 返回数组参数风格的 sql

func (*Inserter) SQLArgs added in v1.1.0

func (ins *Inserter) SQLArgs() (string, []any, error)

SQLArgs 构造 sql 并返回数组类型参数 需要通过 Fields 方法赋值,否则使用 NameSQL

func (*Inserter) StructColumns

func (ins *Inserter) StructColumns(m any, tagName string, omitColumns ...string) *Inserter

StructColumns 从结构体解析 insert 字段

type Op

type Op struct {
	Symbol string
	Text   string
}

type OrderBy

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

func Asc

func Asc(columns ...string) OrderBy

func Desc

func Desc(columns ...string) OrderBy

type OrderType

type OrderType string
const (
	ASC  OrderType = "ASC"
	DESC OrderType = "DESC"
)

type Predicate

type Predicate struct {
	Op       Op
	Express  string
	Args     []any
	HasInSQL bool
}

Predicate where 断言

type Selector

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

Selector select 语句构造器

func NewSelector

func NewSelector(tableName string) *Selector

NewSelector 创建一个selector

func (*Selector) Columns

func (s *Selector) Columns(columns ...string) *Selector

Columns select 的数据库字段

func (*Selector) CountSQL

func (s *Selector) CountSQL() (string, error)

CountSQL 构造 count 查询 sql

func (*Selector) CountSQLArgs

func (s *Selector) CountSQLArgs() (string, []any, error)

CountSQLArgs 构造 count 查询 sql 并返回对应参数

func (*Selector) Distinct

func (s *Selector) Distinct() *Selector

Distinct select distinct

func (*Selector) ForUpdate

func (s *Selector) ForUpdate(isForUpdate bool) *Selector

ForUpdate select for update

func (*Selector) Get added in v1.1.0

func (s *Selector) Get(dest any) (exist bool, err error)

Get 查询单条数据

func (*Selector) GetContext added in v1.1.0

func (s *Selector) GetContext(ctx context.Context, dest any) (exist bool, err error)

GetContext 查询单条数据

func (*Selector) GroupBy

func (s *Selector) GroupBy(columns ...string) *Selector

GroupBy group by

func (*Selector) IfNullVal added in v1.1.0

func (s *Selector) IfNullVal(col string, val string) *Selector

IfNullVal 设置字段为空时,返回的值

func (*Selector) IfNullVals added in v1.1.0

func (s *Selector) IfNullVals(vals map[string]string) *Selector

IfNullVals 设置字段为空时,返回的值 key 为数据库表字段名 value 为默认值表达式,如:空字符串为 "”"

func (*Selector) Limit

func (s *Selector) Limit(limit int64) *Selector

Limit 分页 limit

func (*Selector) Offset

func (s *Selector) Offset(offset int64) *Selector

Offset 分页 offset

func (*Selector) OrderBy

func (s *Selector) OrderBy(orderBy ...OrderBy) *Selector

OrderBy order by orderBy sqlbuilder.Desc("col")

func (*Selector) QueryString

func (s *Selector) QueryString(queryString string) *Selector

QueryString 自定义select字段,sql原样输出

func (*Selector) Queryer added in v1.1.0

func (s *Selector) Queryer(queryer engine.Queryer) *Selector

Queryer 设置查询器

func (*Selector) SQL

func (s *Selector) SQL() (string, error)

SQL 输出sql语句

func (*Selector) SQLArgs

func (s *Selector) SQLArgs() (string, []any, error)

SQLArgs 构造 sql 并返回对应参数

func (*Selector) Select added in v1.1.0

func (s *Selector) Select(dest any) error

Select 查询多条数据

func (*Selector) SelectContext added in v1.1.0

func (s *Selector) SelectContext(ctx context.Context, dest any) error

SelectContext 查询多条数据

func (*Selector) StructColumns

func (s *Selector) StructColumns(model any, tagName string, omitColumns ...string) *Selector

StructColumns 通过任意model解析出表字段 tagName 解析数据库字段的 tag-name omitColumns 排除哪些字段

func (*Selector) Where

func (s *Selector) Where(where ConditionBuilder) *Selector

Where 条件 condition 可以通过 sqlbuilder.C() 方法创建

type SimpleCondition

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

SimpleCondition 简单 where 条件构造

func SC

func SC() *SimpleCondition

SC 简单 where 条件

func (*SimpleCondition) And

func (c *SimpleCondition) And(express string, args ...any) *SimpleCondition

And and 语句 express where 表达式

func (*SimpleCondition) Or

func (c *SimpleCondition) Or(express string, args ...any) *SimpleCondition

Or or 语句 express where 表达式

func (*SimpleCondition) Predicates

func (c *SimpleCondition) Predicates() []Predicate

type Updater

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

Updater update 语句构造器

func NewUpdater

func NewUpdater(tableName string) *Updater

NewUpdater 创建一个 update 语句构造器

func (*Updater) Columns

func (u *Updater) Columns(columns ...string) *Updater

Columns update 的数据库字段

func (*Updater) Exec added in v1.1.0

func (u *Updater) Exec() (int64, error)

Exec 执行更新语句

func (*Updater) ExecContext added in v1.1.0

func (u *Updater) ExecContext(ctx context.Context) (int64, error)

ExecContext 执行更新语句

func (*Updater) Execer added in v1.1.0

func (u *Updater) Execer(execer engine.Execer) *Updater

Execer 设置Execer

func (*Updater) Fields added in v1.1.0

func (u *Updater) Fields(fields ...Field) *Updater

Fields 设置字段值

func (*Updater) Incr added in v1.1.0

func (u *Updater) Incr(column string, n int64) *Updater

Incr 数值增加,eg: set a = a + 1

func (*Updater) NameSQL

func (u *Updater) NameSQL() (string, error)

func (*Updater) NamedExec added in v1.1.0

func (u *Updater) NamedExec(data any) (int64, error)

NamedExec 通过 NameSQL 执行更新语句,参数通过 data 填充 where 条件也必须是 name 风格

func (*Updater) NamedExecContext added in v1.1.0

func (u *Updater) NamedExecContext(ctx context.Context, data any) (int64, error)

NamedExecContext 通过 NameSQL 执行更新语句,参数通过 data 填充 where 条件也必须是 name 风格

func (*Updater) SQL

func (u *Updater) SQL() (string, error)

SQL 输出sql语句

func (*Updater) SQLArgs

func (u *Updater) SQLArgs() (string, []any, error)

SQLArgs 构造 sql 并返回对应参数

func (*Updater) Set

func (u *Updater) Set(column string, val any) *Updater

Set 设置字段值

func (*Updater) Where

func (u *Updater) Where(where ConditionBuilder) *Updater

Where 条件 condition 可以通过 sqlbuilder.C() 方法创建

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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