sqlbuilder

package
v2.0.0-rc04 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2025 License: MIT Imports: 12 Imported by: 0

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 "}
	OpExist     = Op{Symbol: "EXIST", Text: " EXIST "}
	OpIsNull    = Op{Symbol: "IS NULL", Text: " IS NULL"}
	OpIsNotNull = Op{Symbol: "IS NOT NULL", Text: " IS NOT NULL"}
)

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) Alias

func (c Column) Alias(alias string) Column

Alias 设置字段别名

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) IsNotNull

func (c Column) IsNotNull() Column

IsNotNull -> IS NOT NULL

func (Column) IsNull

func (c Column) IsNull() Column

IsNull -> IS NULL

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

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

Exec 执行更新语句

func (*Deleter) ExecContext

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

ExecContext 执行更新语句

func (*Deleter) Execer

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

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

Field 表更新字段

func F

func F(col string) Field

F 创建更新字段

func (Field) Incr

func (f Field) Incr(n int64) Field

Incr 设置字段增加值

func (Field) Use

func (f Field) Use(use bool) Field

Use 是否启用

func (Field) Val

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

func (ins *Inserter) Exec() (sql.Result, error)

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

func (*Inserter) ExecContext

func (ins *Inserter) ExecContext(ctx context.Context) (sql.Result, error)

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

func (*Inserter) Execer

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

Execer 设置 execer

func (*Inserter) Fields

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

func (ins *Inserter) NamedExec(model any) (sql.Result, error)

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

func (*Inserter) NamedExecContext

func (ins *Inserter) NamedExecContext(ctx context.Context, model any) (sql.Result, error)

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

func (*Inserter) OnDuplicateKeyUpdate

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

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

func (OrderBy) Alias

func (o OrderBy) Alias(as 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) As

func (s *Selector) As(alias string) *Selector

As 设置表的别名

func (*Selector) ColumnAlias

func (s *Selector) ColumnAlias(alias string, columns ...string) *Selector

ColumnAlias 用于添加列及其别名

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) FullJoin

func (s *Selector) FullJoin(table, alias string, on string) *Selector

FullJoin 添加一个 FULL JOIN 子句

func (*Selector) GetCount

func (s *Selector) GetCount() (int64, error)

GetCount 查询总记录数

func (*Selector) GetCountContext

func (s *Selector) GetCountContext(ctx context.Context) (int64, error)

GetCountContext 查询总记录数

func (*Selector) GroupBy

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

GroupBy group by

func (*Selector) IfNullVal

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

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

func (*Selector) IfNullVals

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

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

func (*Selector) InnerJoin

func (s *Selector) InnerJoin(table, alias string, on string) *Selector

InnerJoin 添加一个 INNER JOIN 子句

func (*Selector) LeftJoin

func (s *Selector) LeftJoin(table, alias string, on string) *Selector

LeftJoin 添加一个 LEFT JOIN 子句

func (*Selector) Limit

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

Limit 分页 limit

func (*Selector) List

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

List 查询多条数据

func (*Selector) ListContext

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

ListContext 查询多条数据

func (*Selector) Offset

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

Offset 分页 offset

func (*Selector) One

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

One 查询单条数据

func (*Selector) OneContext

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

OneContext 查询单条数据

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

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

Queryer 设置查询器

func (*Selector) RightJoin

func (s *Selector) RightJoin(table, alias string, on string) *Selector

RightJoin 添加一个 RIGHT JOIN 子句

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) 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() 方法创建

func (*Selector) WhereC

func (s *Selector) WhereC(cols ...Column) *Selector

WhereC 查询条件,使用 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

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

Exec 执行更新语句

func (*Updater) ExecContext

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

ExecContext 执行更新语句

func (*Updater) Execer

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

Execer 设置Execer

func (*Updater) Fields

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

Fields 设置字段值

func (*Updater) Incr

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

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

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

func (*Updater) NamedExecContext

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