sqlbuilder

package
v6.0.0-beta.5 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2025 License: MIT Imports: 10 Imported by: 7

Documentation

Overview

Package sqlbuilder 提供一套通过字符串拼接来构成 SQL 语句的工具

sqlbuilder 提供了部分 *Hooker 的接口, 用于处理大部分数据都有标准实现而只有某个数据库采用了非标准模式的。

Index

Constants

This section is empty.

Variables

View Source
var ErrNoData = errors.New("不存在符合和条件的数据")

ErrNoData 在 [Select.QueryInt] 等函数中, 如果没有符合条件的数据,则返回此错误。

Functions

func SyntaxError

func SyntaxError(typ string, msg any) error

SyntaxError 返回语法错误的信息

typ 表示语句的类型,比如 SELECT、UPDATE 等; msg 为具体的错误信息;

func Version

func Version(e core.Engine) (version string, err error)

Version 查询数据库服务器的版本信息

Types

type AddColumnStmt

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

AddColumnStmt 添加列

func AddColumn

func AddColumn(e core.Engine) *AddColumnStmt

AddColumn 声明一条添加列的语句

func (*AddColumnStmt) Column

func (stmt *AddColumnStmt) Column(name string, p core.PrimitiveType, ai, nullable, hasDefault bool, def any, length ...int) *AddColumnStmt

Column 添加列

参数信息可参考 CreateTableStmt.Column

func (*AddColumnStmt) DDLSQL

func (stmt *AddColumnStmt) DDLSQL() ([]string, error)

DDLSQL 获取 SQL 语句以及对应的参数

func (AddColumnStmt) Exec

func (stmt AddColumnStmt) Exec() error

func (AddColumnStmt) ExecContext

func (stmt AddColumnStmt) ExecContext(ctx context.Context) error

func (*AddColumnStmt) Reset

func (stmt *AddColumnStmt) Reset() *AddColumnStmt

Reset 重置

func (*AddColumnStmt) Table

func (stmt *AddColumnStmt) Table(table string) *AddColumnStmt

Table 指定表名

NOTE: 重复指定,会覆盖之前的。

type AddConstraintStmt

type AddConstraintStmt struct {
	TableName string
	Name      string
	Type      core.ConstraintType

	// 约束的值,根据 Type 的不同,略有不同:
	// check 下表示的 check 表达式,仅有一个元素;
	// fk 下最多可以有 5 个值,第 1 个元素为关联的列,2、3 元素引用的表和列,
	//  4,5 元素为 UPDATE 和 DELETE 的规则定义;
	// 其它模式下为该约束关联的列名称。
	Data []string
	// contains filtered or unexported fields
}

AddConstraintStmt 添加约束

func AddConstraint

func AddConstraint(e core.Engine) *AddConstraintStmt

AddConstraint 声明添加约束的语句

func (*AddConstraintStmt) Check

func (stmt *AddConstraintStmt) Check(name, expr string) *AddConstraintStmt

Check Check 约束

func (*AddConstraintStmt) DDLSQL

func (stmt *AddConstraintStmt) DDLSQL() ([]string, error)

DDLSQL 生成 SQL 语句

func (AddConstraintStmt) Exec

func (stmt AddConstraintStmt) Exec() error

func (AddConstraintStmt) ExecContext

func (stmt AddConstraintStmt) ExecContext(ctx context.Context) error

func (*AddConstraintStmt) FK

func (stmt *AddConstraintStmt) FK(name, col, refTable, refColumn, updateRule, deleteRule string) *AddConstraintStmt

FK 外键约束

func (*AddConstraintStmt) PK

func (stmt *AddConstraintStmt) PK(name string, col ...string) *AddConstraintStmt

PK 指定主键约束

func (*AddConstraintStmt) Reset

func (stmt *AddConstraintStmt) Reset() *AddConstraintStmt

Reset 重置内容

func (*AddConstraintStmt) Table

func (stmt *AddConstraintStmt) Table(t string) *AddConstraintStmt

Table 指定表名

func (*AddConstraintStmt) Unique

func (stmt *AddConstraintStmt) Unique(name string, col ...string) *AddConstraintStmt

Unique 指定唯一约束

type AddConstraintStmtHooker

type AddConstraintStmtHooker interface {
	AddConstraintStmtHook(*AddConstraintStmt) ([]string, error)
}

AddConstraintStmtHooker AddConstraintStmt.DDLSQL 的钩子函数

type CreateIndexStmt

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

CreateIndexStmt 创建索引的语句

func CreateIndex

func CreateIndex(e core.Engine) *CreateIndexStmt

CreateIndex 声明一条 CreateIndexStmt 语句

func (*CreateIndexStmt) Columns

func (stmt *CreateIndexStmt) Columns(col ...string) *CreateIndexStmt

Columns 列名

func (*CreateIndexStmt) DDLSQL

func (stmt *CreateIndexStmt) DDLSQL() ([]string, error)

DDLSQL 生成 SQL 语句

func (CreateIndexStmt) Exec

func (stmt CreateIndexStmt) Exec() error

func (CreateIndexStmt) ExecContext

func (stmt CreateIndexStmt) ExecContext(ctx context.Context) error

func (*CreateIndexStmt) Name

func (stmt *CreateIndexStmt) Name(index string) *CreateIndexStmt

Name 指定索引名

func (*CreateIndexStmt) Reset

func (stmt *CreateIndexStmt) Reset() *CreateIndexStmt

Reset 重置

func (*CreateIndexStmt) Table

func (stmt *CreateIndexStmt) Table(tbl string) *CreateIndexStmt

Table 指定表名

func (*CreateIndexStmt) Type

Type 指定索引类型

type CreateTableStmt

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

CreateTableStmt 创建表的语句

func CreateTable

func CreateTable(e core.Engine) *CreateTableStmt

CreateTable 创建表的语句

执行创建表操作,可能包含了创建索引等多个语句, 如果 e 是一个事务类型,且 e.Dialect() 是支持事务 DDL 的, 那么在执行时,会当作一个事务处理,否则为多个语句依次执行。

func (*CreateTableStmt) AutoIncrement

func (stmt *CreateTableStmt) AutoIncrement(col string, p core.PrimitiveType) *CreateTableStmt

AutoIncrement 指定自增列

自增列必定是主键。如果指定了自增,则主键必定不启作用。 功能与 Column() 中将 ai 设置 true 是一样的。

col 列名;

func (*CreateTableStmt) Check

func (stmt *CreateTableStmt) Check(name string, expr string) *CreateTableStmt

Check 指定 check 约束

func (*CreateTableStmt) Column

func (stmt *CreateTableStmt) Column(name string, p core.PrimitiveType, ai, nullable, hasDefault bool, def any, length ...int) *CreateTableStmt

Column 添加列

name 列的名称; p Go 中的类型,该类型会被转换成相应的数据库类型; ai 是否自增列; nullable 表示该列是否可以为 NULL; hasDefault 表示是否拥有默认值,如果为 true,则 v 同时会被当作默认值; def 默认值; length 表示长度信息。

func (*CreateTableStmt) Columns

func (stmt *CreateTableStmt) Columns(col ...*core.Column) *CreateTableStmt

Columns 添加列

func (*CreateTableStmt) DDLSQL

func (stmt *CreateTableStmt) DDLSQL() ([]string, error)

DDLSQL 获取 SQL 的语句及参数部分

func (CreateTableStmt) Exec

func (stmt CreateTableStmt) Exec() error

func (CreateTableStmt) ExecContext

func (stmt CreateTableStmt) ExecContext(ctx context.Context) error

func (*CreateTableStmt) ForeignKey

func (stmt *CreateTableStmt) ForeignKey(name, col, refTable, refCol, updateRule, deleteRule string) *CreateTableStmt

ForeignKey 指定外键

func (*CreateTableStmt) Index

func (stmt *CreateTableStmt) Index(typ core.IndexType, name string, col ...string) *CreateTableStmt

Index 添加索引

func (*CreateTableStmt) Options

func (stmt *CreateTableStmt) Options(k string, v ...string) *CreateTableStmt

Options 指定创建表的额外选项

k 为选项的名称,如果特定于数据库,需要加上数据库名作为前缀,比如 mysql_charset; v 为选项值;

func (*CreateTableStmt) PK

func (stmt *CreateTableStmt) PK(name string, col ...string) *CreateTableStmt

PK 指定主键约束

name 为约束名,部分数据会忽略约束名,比如 mysql;

func (*CreateTableStmt) Reset

func (stmt *CreateTableStmt) Reset() *CreateTableStmt

func (*CreateTableStmt) Table

func (stmt *CreateTableStmt) Table(t string) *CreateTableStmt

Table 指定表名

func (*CreateTableStmt) Unique

func (stmt *CreateTableStmt) Unique(name string, col ...string) *CreateTableStmt

Unique 添加唯一约束

type CreateViewStmt

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

CreateViewStmt 创建视图的语句

func CreateView

func CreateView(e core.Engine) *CreateViewStmt

CreateView 创建视图

func (*CreateViewStmt) Column

func (stmt *CreateViewStmt) Column(col ...string) *CreateViewStmt

Column 指定视图的列,如果未指定,则会直接采用 Select 中的列信息

func (*CreateViewStmt) DDLSQL

func (stmt *CreateViewStmt) DDLSQL() ([]string, error)

DDLSQL 返回创建视图的 SQL 语句

func (CreateViewStmt) Exec

func (stmt CreateViewStmt) Exec() error

func (CreateViewStmt) ExecContext

func (stmt CreateViewStmt) ExecContext(ctx context.Context) error

func (*CreateViewStmt) From

func (stmt *CreateViewStmt) From(sel *SelectStmt) *CreateViewStmt

From 指定 Select 语句

func (*CreateViewStmt) FromQuery

func (stmt *CreateViewStmt) FromQuery(query string) *CreateViewStmt

FromQuery 指定查询语句

FromQuery 和 From 会相互覆盖。

func (*CreateViewStmt) Name

func (stmt *CreateViewStmt) Name(name string) *CreateViewStmt

Name 指定视图名称

func (*CreateViewStmt) Replace

func (stmt *CreateViewStmt) Replace() *CreateViewStmt

Replace 如果已经存在,则更新视图内容

func (*CreateViewStmt) Reset

func (stmt *CreateViewStmt) Reset() *CreateViewStmt

Reset 重置对象

func (*CreateViewStmt) Temporary

func (stmt *CreateViewStmt) Temporary() *CreateViewStmt

Temporary 临时视图

type DDLSQLer

type DDLSQLer interface {
	DDLSQL() ([]string, error)
}

DDLSQLer SQL 中 DDL 语句的基本接口

大部分数据的 DDL 操作是有多条语句组成,比如 CREATE TABLE 可能包含了额外的定义信息。

func MergeDDL

func MergeDDL(ddl ...DDLSQLer) DDLSQLer

MergeDDL 合并多个 DDLSQLer 对象

type DDLStmt

type DDLStmt interface {
	DDLSQLer
	Exec() error
	ExecContext(ctx context.Context) error
}

type DeleteStmt

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

DeleteStmt 表示删除操作的 SQL 语句

func Delete

func Delete(e core.Engine) *DeleteStmt

Delete 声明一条删除语句

func (DeleteStmt) CombineSQL

func (stmt DeleteStmt) CombineSQL() (query string, err error)

CombineSQL 合并 [SQLer.SQL] 返回的 query 和 args 参数

func (DeleteStmt) Exec

func (stmt DeleteStmt) Exec() (sql.Result, error)

func (DeleteStmt) ExecContext

func (stmt DeleteStmt) ExecContext(ctx context.Context) (sql.Result, error)

func (DeleteStmt) Prepare

func (stmt DeleteStmt) Prepare() (*core.Stmt, error)

Prepare 预编译语句

预编译语句,参数最好采用 sql.NamedArg 类型。 在生成语句时,参数顺序会发生变化,如果采用 ? 的形式, 用户需要自己处理参数顺序问题,而 sql.NamedArg 没有这些问题。

func (DeleteStmt) PrepareContext

func (stmt DeleteStmt) PrepareContext(ctx context.Context) (*core.Stmt, error)

func (*DeleteStmt) Reset

func (stmt *DeleteStmt) Reset() *DeleteStmt

Reset 重置语句

func (*DeleteStmt) SQL

func (stmt *DeleteStmt) SQL() (string, []any, error)

SQL 获取 SQL 语句,以及其参数对应的具体值

func (*DeleteStmt) Table

func (stmt *DeleteStmt) Table(table string) *DeleteStmt

Table 指定表名

type DropColumnStmt

type DropColumnStmt struct {
	TableName  string
	ColumnName string
	// contains filtered or unexported fields
}

DropColumnStmt 删除列

func DropColumn

func DropColumn(e core.Engine) *DropColumnStmt

DropColumn 声明一条删除列的语句

func (*DropColumnStmt) Column

func (stmt *DropColumnStmt) Column(col string) *DropColumnStmt

Column 指定需要删除的列 重复指定,会覆盖之前的。

func (*DropColumnStmt) DDLSQL

func (stmt *DropColumnStmt) DDLSQL() ([]string, error)

DDLSQL 获取 SQL 语句以及对应的参数

func (DropColumnStmt) Exec

func (stmt DropColumnStmt) Exec() error

func (DropColumnStmt) ExecContext

func (stmt DropColumnStmt) ExecContext(ctx context.Context) error

func (*DropColumnStmt) Reset

func (stmt *DropColumnStmt) Reset() *DropColumnStmt

Reset 重置

func (*DropColumnStmt) Table

func (stmt *DropColumnStmt) Table(table string) *DropColumnStmt

Table 指定表名。 重复指定,会覆盖之前的。

type DropColumnStmtHooker

type DropColumnStmtHooker interface {
	DropColumnStmtHook(*DropColumnStmt) ([]string, error)
}

DropColumnStmtHooker DropColumnStmt.DDLSQL 的钩子函数

type DropConstraintStmt

type DropConstraintStmt struct {
	TableName string
	Name      string
	IsPK      bool
	// contains filtered or unexported fields
}

DropConstraintStmt 删除约束

func DropConstraint

func DropConstraint(e core.Engine) *DropConstraintStmt

DropConstraint 声明一条删除表约束的语句

func (*DropConstraintStmt) Constraint

func (stmt *DropConstraintStmt) Constraint(name string) *DropConstraintStmt

Constraint 指定需要删除的约束名

如果需要删除主键,请使用 PK 代替。

func (*DropConstraintStmt) DDLSQL

func (stmt *DropConstraintStmt) DDLSQL() ([]string, error)

func (DropConstraintStmt) Exec

func (stmt DropConstraintStmt) Exec() error

func (DropConstraintStmt) ExecContext

func (stmt DropConstraintStmt) ExecContext(ctx context.Context) error

func (*DropConstraintStmt) PK

PK 删除主键约束

mysql 没有主键名称,所以才有此方法专门用于删除主键约束。

func (*DropConstraintStmt) Reset

func (stmt *DropConstraintStmt) Reset() *DropConstraintStmt

Reset 重置

func (*DropConstraintStmt) Table

func (stmt *DropConstraintStmt) Table(table string) *DropConstraintStmt

Table 指定表名

重复指定,会覆盖之前的。

type DropConstraintStmtHooker

type DropConstraintStmtHooker interface {
	DropConstraintStmtHook(*DropConstraintStmt) ([]string, error)
}

type DropIndexStmt

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

DropIndexStmt 删除索引

func DropIndex

func DropIndex(e core.Engine) *DropIndexStmt

DropIndex 声明一条 DropIndexStmt 语句

func (*DropIndexStmt) DDLSQL

func (stmt *DropIndexStmt) DDLSQL() ([]string, error)

DDLSQL 生成 SQL 语句

func (DropIndexStmt) Exec

func (stmt DropIndexStmt) Exec() error

func (DropIndexStmt) ExecContext

func (stmt DropIndexStmt) ExecContext(ctx context.Context) error

func (*DropIndexStmt) Name

func (stmt *DropIndexStmt) Name(col string) *DropIndexStmt

Name 指定索引名

func (*DropIndexStmt) Reset

func (stmt *DropIndexStmt) Reset() *DropIndexStmt

Reset 重置

func (*DropIndexStmt) Table

func (stmt *DropIndexStmt) Table(tbl string) *DropIndexStmt

Table 指定表名

type DropTableStmt

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

DropTableStmt 删除表语句

func DropTable

func DropTable(e core.Engine) *DropTableStmt

DropTable 声明一条删除表的语句

func (*DropTableStmt) DDLSQL

func (stmt *DropTableStmt) DDLSQL() ([]string, error)

func (DropTableStmt) Exec

func (stmt DropTableStmt) Exec() error

func (DropTableStmt) ExecContext

func (stmt DropTableStmt) ExecContext(ctx context.Context) error

func (*DropTableStmt) Reset

func (stmt *DropTableStmt) Reset() *DropTableStmt

func (*DropTableStmt) Table

func (stmt *DropTableStmt) Table(table ...string) *DropTableStmt

Table 指定表名

多次指定,则会删除多个表

type DropViewStmt

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

DropViewStmt 删除视图

func DropView

func DropView(e core.Engine) *DropViewStmt

DropView 创建视图

func (*DropViewStmt) DDLSQL

func (stmt *DropViewStmt) DDLSQL() ([]string, error)

DDLSQL 返回删除视图的 SQL 语句

func (DropViewStmt) Exec

func (stmt DropViewStmt) Exec() error

func (DropViewStmt) ExecContext

func (stmt DropViewStmt) ExecContext(ctx context.Context) error

func (*DropViewStmt) Name

func (stmt *DropViewStmt) Name(name string) *DropViewStmt

Name 指定需要删除的视图名称

func (*DropViewStmt) Reset

func (stmt *DropViewStmt) Reset() *DropViewStmt

Reset 重置对象

type ExecStmt

type ExecStmt interface {
	SQLer
	Prepare() (*core.Stmt, error)
	PrepareContext(ctx context.Context) (*core.Stmt, error)
	Exec() (sql.Result, error)
	ExecContext(ctx context.Context) (sql.Result, error)
}

type InsertDefaultValueHooker

type InsertDefaultValueHooker interface {
	InsertDefaultValueHook(tableName string) (string, []any, error)
}

InsertDefaultValueHooker 插入值全部为默认值时的钩子处理函数

type InsertStmt

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

InsertStmt 表示插入操作的 SQL 语句

func Insert

func Insert(e core.Engine) *InsertStmt

Insert 声明一条插入语句

func (*InsertStmt) Columns

func (stmt *InsertStmt) Columns(cols ...string) *InsertStmt

Columns 指定插入的列,多次指定,之前的会被覆盖。

func (InsertStmt) CombineSQL

func (stmt InsertStmt) CombineSQL() (query string, err error)

CombineSQL 合并 [SQLer.SQL] 返回的 query 和 args 参数

func (InsertStmt) Exec

func (stmt InsertStmt) Exec() (sql.Result, error)

func (InsertStmt) ExecContext

func (stmt InsertStmt) ExecContext(ctx context.Context) (sql.Result, error)

func (*InsertStmt) KeyValue

func (stmt *InsertStmt) KeyValue(col string, val any) *InsertStmt

KeyValue 指定键值对

当通过 Values() 指定多行数据时,再使用 KeyValue 会出错

func (*InsertStmt) LastInsertID

func (stmt *InsertStmt) LastInsertID(col string) (int64, error)

LastInsertID 执行 SQL 语句

并根据表名和自增列 ID 返回当前行的自增 ID 值。

NOTE: 对于指定了自增值的,其结果是未知的。

func (*InsertStmt) LastInsertIDContext

func (stmt *InsertStmt) LastInsertIDContext(ctx context.Context, col string) (id int64, err error)

LastInsertIDContext 执行 SQL 语句

并根据表名和自增列 ID 返回当前行的自增 ID 值。

func (InsertStmt) Prepare

func (stmt InsertStmt) Prepare() (*core.Stmt, error)

Prepare 预编译语句

预编译语句,参数最好采用 sql.NamedArg 类型。 在生成语句时,参数顺序会发生变化,如果采用 ? 的形式, 用户需要自己处理参数顺序问题,而 sql.NamedArg 没有这些问题。

func (InsertStmt) PrepareContext

func (stmt InsertStmt) PrepareContext(ctx context.Context) (*core.Stmt, error)

func (*InsertStmt) Reset

func (stmt *InsertStmt) Reset() *InsertStmt

Reset 重置语句

func (*InsertStmt) SQL

func (stmt *InsertStmt) SQL() (string, []any, error)

SQL 获取 SQL 的语句及参数部分

func (*InsertStmt) Select

func (stmt *InsertStmt) Select(sel *SelectStmt) *InsertStmt

Select 当前插入数据从 Select 中获取

构建 insert into (...) select .... 语句

func (*InsertStmt) Table

func (stmt *InsertStmt) Table(table string) *InsertStmt

Table 指定表名

func (*InsertStmt) Values

func (stmt *InsertStmt) Values(vals ...any) *InsertStmt

Values 指定需要插入的值

NOTE: vals 传入时,并不会被解压

type SQLBuilder

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

SQLBuilder 提供了 sqlbuilder 下的各类语句的创建方法

func New

func New(e core.Engine) *SQLBuilder

New 声明 SQLBuilder 实例

tablePrefix 表名前缀;

func (*SQLBuilder) AddColumn

func (sql *SQLBuilder) AddColumn() *AddColumnStmt

AddColumn 声明一条添加列的语句

func (*SQLBuilder) AddConstraint

func (sql *SQLBuilder) AddConstraint() *AddConstraintStmt

AddConstraint 声明添加约束的语句

func (*SQLBuilder) CreateIndex

func (sql *SQLBuilder) CreateIndex() *CreateIndexStmt

CreateIndex 生成创建索引的语句

func (*SQLBuilder) CreateTable

func (sql *SQLBuilder) CreateTable() *CreateTableStmt

CreateTable 生成创建表的语句

func (*SQLBuilder) CreateView

func (sql *SQLBuilder) CreateView() *CreateViewStmt

CreateView 创建视图

func (*SQLBuilder) Delete

func (sql *SQLBuilder) Delete() *DeleteStmt

Delete 生成删除语句

func (*SQLBuilder) DropColumn

func (sql *SQLBuilder) DropColumn() *DropColumnStmt

DropColumn 声明一条删除列的语句

func (*SQLBuilder) DropConstraint

func (sql *SQLBuilder) DropConstraint() *DropConstraintStmt

DropConstraint 声明一条删除表约束的语句

func (*SQLBuilder) DropIndex

func (sql *SQLBuilder) DropIndex() *DropIndexStmt

DropIndex 生成删除索引的语句

func (*SQLBuilder) DropTable

func (sql *SQLBuilder) DropTable() *DropTableStmt

DropTable 生成删除表的语句

func (*SQLBuilder) DropView

func (sql *SQLBuilder) DropView() *DropViewStmt

func (*SQLBuilder) Insert

func (sql *SQLBuilder) Insert() *InsertStmt

Insert 生成插入语句

func (*SQLBuilder) Select

func (sql *SQLBuilder) Select() *SelectStmt

Select 生成插入语句

func (*SQLBuilder) TableExists

func (sql *SQLBuilder) TableExists() *TableExistsStmt

func (*SQLBuilder) TruncateTable

func (sql *SQLBuilder) TruncateTable() *TruncateTableStmt

TruncateTable 生成清空表的语句,同时重置 AI 计算

func (*SQLBuilder) Update

func (sql *SQLBuilder) Update() *UpdateStmt

Update 生成更新语句

func (*SQLBuilder) ViewExists

func (sql *SQLBuilder) ViewExists() *ViewExistsStmt

func (*SQLBuilder) Where

func (sql *SQLBuilder) Where() *WhereStmt

Where 生成 Where 语句

type SQLer

type SQLer interface {
	// SQL 将当前实例转换成 SQL 语句返回
	//
	// query 表示 SQL 语句,而 args 表示语句各个参数占位符对应的参数值。
	SQL() (query string, args []any, err error)
}

SQLer 定义 SQL 语句的基本接口

type SelectQuery

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

SelectQuery 预编译之后的查询语句

func (*SelectQuery) Close

func (stmt *SelectQuery) Close() error

func (*SelectQuery) QueryFloat

func (stmt *SelectQuery) QueryFloat(colName string, arg ...any) (float64, error)

QueryFloat 查询指定列的第一行数据,并将其转换成 float64

func (*SelectQuery) QueryInt

func (stmt *SelectQuery) QueryInt(colName string, arg ...any) (int64, error)

QueryInt 查询指定列的第一行数据,并将其转换成 int64

func (*SelectQuery) QueryObject

func (stmt *SelectQuery) QueryObject(strict bool, objs any, arg ...any) (size int, err error)

QueryObject 将符合当前条件的所有记录依次写入 objs 中。

关于 objs 的值类型,可以参考 github.com/issue9/orm/fetch.Object 函数的相关介绍。

func (*SelectQuery) QueryString

func (stmt *SelectQuery) QueryString(colName string, arg ...any) (string, error)

QueryString 查询指定列的第一行数据,并将其转换成 string

type SelectStmt

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

SelectStmt 查询语句

func Select

func Select(e core.Engine) *SelectStmt

Select 声明一条 SELECT 语句

func (*SelectStmt) Asc

func (stmt *SelectStmt) Asc(col ...string) *SelectStmt

Asc 正序查询

col 为分组的列名,格式可以是单纯的列名,或是带表名的列:

col
table.col

table 和 col 都可以是关键字,系统会自动处理。

func (*SelectStmt) Column

func (stmt *SelectStmt) Column(col string) *SelectStmt

Column 指定列

一次只能指定一列,当未指定任何列时,默认会采用 *。

col 表示列名,可以是以下形式:

*
col
table.col
table.*
sum({table}.{col}) as col1

如果列名是关键字,可以使用 {} 包含。如果包含了表名,则需要自行添加表名前缀。

func (*SelectStmt) Columns

func (stmt *SelectStmt) Columns(cols ...string) *SelectStmt

Columns 指定列名

相当于按参数顺序依次调用 [Select.Column],如果存在别名, 可以使用 col AS alias 的方式指定每一个参数。

如果列名是关键字,可以使用 {} 包含。

func (SelectStmt) CombineSQL

func (stmt SelectStmt) CombineSQL() (query string, err error)

CombineSQL 将 [SQLer.SQL] 中返回的参数替换掉 query 中的占位符, 形成一条完整的查询语句。

func (*SelectStmt) Count

func (stmt *SelectStmt) Count(expr string, args ...any) *SelectStmt

Count 指定 Count 表达式

如果指定了 count 表达式,则会造成 limit 失效, 如果设置为空值,则取消 count,恢复普通的 select 。

stmt := NewSelectStmt()
stmt.Count("count(*) as cnt")
stmt.Count("count(CASE WHEN xx) as cnt1, count(CASE WHEN yy) as cnt2")

func (*SelectStmt) Desc

func (stmt *SelectStmt) Desc(col ...string) *SelectStmt

Desc 倒序查询

col 为分组的列名,格式可以是单纯的列名,或是带表名的列:

col
table.col

table 和 col 都可以是关键字,系统会自动处理。

func (*SelectStmt) Distinct

func (stmt *SelectStmt) Distinct() *SelectStmt

Distinct 声明一条 Select 语句的 Distinct

若指定了此值,则 Select() 所指定的列,均为 Distinct 之后的列。

func (*SelectStmt) ForUpdate

func (stmt *SelectStmt) ForUpdate() *SelectStmt

ForUpdate 添加 FOR UPDATE 语句部分

func (*SelectStmt) From

func (stmt *SelectStmt) From(table string, alias ...string) *SelectStmt

From 指定表名

table 为表名,如果需要指定别名,可以通过 alias 指定。

func (*SelectStmt) Group

func (stmt *SelectStmt) Group(col string) *SelectStmt

Group 添加 GROUP BY 语句

col 为分组的列名,格式可以单纯的列名,或是带表名的列:

col
table.col

table 和 col 都可以是关键字,系统会自动处理。

func (*SelectStmt) Having

func (stmt *SelectStmt) Having(expr string, args ...any) *SelectStmt

Having 指定 having 语句

func (*SelectStmt) Insert

func (stmt *SelectStmt) Insert() *InsertStmt

Insert 将当前查询结果作为 Insert 的值

构建 insert into (...) select .... 语句

func (*SelectStmt) Join

func (stmt *SelectStmt) Join(typ, table, alias, on string) *SelectStmt

Join 添加一条 Join 语句

func (*SelectStmt) Limit

func (stmt *SelectStmt) Limit(limit any, offset ...any) *SelectStmt

Limit 生成 SQL 的 Limit 语句

func (*SelectStmt) Prepare

func (stmt *SelectStmt) Prepare() (*SelectQuery, error)

func (*SelectStmt) PrepareContext

func (stmt *SelectStmt) PrepareContext(ctx context.Context) (*SelectQuery, error)

func (SelectStmt) Query

func (stmt SelectStmt) Query() (*sql.Rows, error)

func (SelectStmt) QueryContext

func (stmt SelectStmt) QueryContext(ctx context.Context) (*sql.Rows, error)

func (*SelectStmt) QueryFloat

func (stmt *SelectStmt) QueryFloat(colName string) (float64, error)

QueryFloat 查询指定列的第一行数据,并将其转换成 float64

func (*SelectStmt) QueryFloatContext

func (stmt *SelectStmt) QueryFloatContext(ctx context.Context, colName string) (float64, error)

func (*SelectStmt) QueryInt

func (stmt *SelectStmt) QueryInt(colName string) (int64, error)

QueryInt 查询指定列的第一行数据,并将其转换成 int64

func (*SelectStmt) QueryIntContext

func (stmt *SelectStmt) QueryIntContext(ctx context.Context, colName string) (int64, error)

func (*SelectStmt) QueryObject

func (stmt *SelectStmt) QueryObject(strict bool, objs any) (size int, err error)

QueryObject 将符合当前条件的所有记录依次写入 objs 中

关于 objs 的类型,可以参考 fetch.Object 函数的相关介绍。

func (*SelectStmt) QueryObjectContext

func (stmt *SelectStmt) QueryObjectContext(ctx context.Context, strict bool, objs any) (size int, err error)

func (*SelectStmt) QueryString

func (stmt *SelectStmt) QueryString(colName string) (v string, err error)

QueryString 查询指定列的第一行数据,并将其转换成 string

func (*SelectStmt) QueryStringContext

func (stmt *SelectStmt) QueryStringContext(ctx context.Context, colName string) (v string, err error)

func (*SelectStmt) Reset

func (stmt *SelectStmt) Reset() *SelectStmt

Reset 重置语句

func (*SelectStmt) SQL

func (stmt *SelectStmt) SQL() (string, []any, error)

SQL 获取 SQL 语句及对应的参数

func (*SelectStmt) Union

func (stmt *SelectStmt) Union(all bool, sel ...*SelectStmt) *SelectStmt

Union 语句

all 表示是否执行 Union all 语法; sel 表示需要进行并接的 Select 语句,传入 sel 之后, 后续对 sel 的操作依赖会影响到语句的最终生成。

func (*SelectStmt) View

func (stmt *SelectStmt) View(name string) *CreateViewStmt

View 将当前查询语句转换为视图

name 为视图名称。

type TableExistsStmt

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

func TableExists

func TableExists(e core.Engine) *TableExistsStmt

func (TableExistsStmt) CombineSQL

func (stmt TableExistsStmt) CombineSQL() (query string, err error)

CombineSQL 将 [SQLer.SQL] 中返回的参数替换掉 query 中的占位符, 形成一条完整的查询语句。

func (*TableExistsStmt) Exists

func (stmt *TableExistsStmt) Exists() (bool, error)

func (TableExistsStmt) Prepare

func (stmt TableExistsStmt) Prepare() (*core.Stmt, error)

func (TableExistsStmt) PrepareContext

func (stmt TableExistsStmt) PrepareContext(ctx context.Context) (*core.Stmt, error)

func (TableExistsStmt) Query

func (stmt TableExistsStmt) Query() (*sql.Rows, error)

func (TableExistsStmt) QueryContext

func (stmt TableExistsStmt) QueryContext(ctx context.Context) (*sql.Rows, error)

func (*TableExistsStmt) Reset

func (stmt *TableExistsStmt) Reset() *TableExistsStmt

func (*TableExistsStmt) SQL

func (stmt *TableExistsStmt) SQL() (string, []any, error)

func (*TableExistsStmt) Table

func (stmt *TableExistsStmt) Table(table string) *TableExistsStmt

type TruncateTableStmt

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

TruncateTableStmt 清空表,并重置 AI

func TruncateTable

func TruncateTable(e core.Engine) *TruncateTableStmt

TruncateTable 生成清空表语句

func (*TruncateTableStmt) DDLSQL

func (stmt *TruncateTableStmt) DDLSQL() ([]string, error)

func (TruncateTableStmt) Exec

func (stmt TruncateTableStmt) Exec() error

func (TruncateTableStmt) ExecContext

func (stmt TruncateTableStmt) ExecContext(ctx context.Context) error

func (*TruncateTableStmt) Reset

func (stmt *TruncateTableStmt) Reset() *TruncateTableStmt

Reset 重置内容

func (*TruncateTableStmt) Table

func (stmt *TruncateTableStmt) Table(t, aiColumn string) *TruncateTableStmt

Table 指定表名

aiColumn 表示自增列;

type UpdateStmt

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

UpdateStmt 更新语句

func Update

func Update(e core.Engine) *UpdateStmt

Update 声明一条 UPDATE 的 SQL 语句

func (UpdateStmt) CombineSQL

func (stmt UpdateStmt) CombineSQL() (query string, err error)

CombineSQL 合并 [SQLer.SQL] 返回的 query 和 args 参数

func (*UpdateStmt) Decrease

func (stmt *UpdateStmt) Decrease(col string, val any) *UpdateStmt

Decrease 给列减少值

func (UpdateStmt) Exec

func (stmt UpdateStmt) Exec() (sql.Result, error)

func (UpdateStmt) ExecContext

func (stmt UpdateStmt) ExecContext(ctx context.Context) (sql.Result, error)

func (*UpdateStmt) Increase

func (stmt *UpdateStmt) Increase(col string, val any) *UpdateStmt

Increase 给列增加值

func (*UpdateStmt) OCC

func (stmt *UpdateStmt) OCC(col string, val any) *UpdateStmt

OCC 指定一个用于乐观锁的字段

val 表示乐观锁原始的值,更新时如果值不等于 val,将更新失败。

func (UpdateStmt) Prepare

func (stmt UpdateStmt) Prepare() (*core.Stmt, error)

Prepare 预编译语句

预编译语句,参数最好采用 sql.NamedArg 类型。 在生成语句时,参数顺序会发生变化,如果采用 ? 的形式, 用户需要自己处理参数顺序问题,而 sql.NamedArg 没有这些问题。

func (UpdateStmt) PrepareContext

func (stmt UpdateStmt) PrepareContext(ctx context.Context) (*core.Stmt, error)

func (*UpdateStmt) Reset

func (stmt *UpdateStmt) Reset() *UpdateStmt

Reset 重置语句

func (*UpdateStmt) SQL

func (stmt *UpdateStmt) SQL() (string, []any, error)

SQL 获取 SQL 语句以及对应的参数

func (*UpdateStmt) Set

func (stmt *UpdateStmt) Set(col string, val any) *UpdateStmt

Set 设置值,若 col 相同,则会覆盖

val 可以是 sql.NamedArg 类型

func (*UpdateStmt) Table

func (stmt *UpdateStmt) Table(table string) *UpdateStmt

Table 指定表名

type ViewExistsStmt

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

func ViewExists

func ViewExists(e core.Engine) *ViewExistsStmt

func (ViewExistsStmt) CombineSQL

func (stmt ViewExistsStmt) CombineSQL() (query string, err error)

CombineSQL 将 [SQLer.SQL] 中返回的参数替换掉 query 中的占位符, 形成一条完整的查询语句。

func (*ViewExistsStmt) Exists

func (stmt *ViewExistsStmt) Exists() (bool, error)

func (ViewExistsStmt) Prepare

func (stmt ViewExistsStmt) Prepare() (*core.Stmt, error)

func (ViewExistsStmt) PrepareContext

func (stmt ViewExistsStmt) PrepareContext(ctx context.Context) (*core.Stmt, error)

func (ViewExistsStmt) Query

func (stmt ViewExistsStmt) Query() (*sql.Rows, error)

func (ViewExistsStmt) QueryContext

func (stmt ViewExistsStmt) QueryContext(ctx context.Context) (*sql.Rows, error)

func (*ViewExistsStmt) Reset

func (stmt *ViewExistsStmt) Reset() *ViewExistsStmt

func (*ViewExistsStmt) SQL

func (stmt *ViewExistsStmt) SQL() (string, []any, error)

func (*ViewExistsStmt) View

func (stmt *ViewExistsStmt) View(table string) *ViewExistsStmt

type WhereStmt

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

WhereStmt SQL 语句的 where 部分

func Where

func Where() *WhereStmt

Where 生成 Where 语句

func (*WhereStmt) And

func (stmt *WhereStmt) And(cond string, args ...any) *WhereStmt

And 添加一条 AND 语句

func (*WhereStmt) AndBetween

func (stmt *WhereStmt) AndBetween(col string, v1, v2 any) *WhereStmt

AndBetween 指定 WHERE ... AND col BETWEEN v1 AND v2

func (*WhereStmt) AndGroup

func (stmt *WhereStmt) AndGroup(f func(*WhereStmt)) *WhereStmt

AndGroup 开始一个子条件语句

func (*WhereStmt) AndIn

func (stmt *WhereStmt) AndIn(col string, v ...any) *WhereStmt

AndIn 指定 WHERE ... AND col IN(v...)

func (*WhereStmt) AndIsNotNull

func (stmt *WhereStmt) AndIsNotNull(col string) *WhereStmt

AndIsNotNull 指定 WHERE ... AND col IS NOT NULL

func (*WhereStmt) AndIsNull

func (stmt *WhereStmt) AndIsNull(col string) *WhereStmt

AndIsNull 指定 WHERE ... AND col IS NULL

func (*WhereStmt) AndLike

func (stmt *WhereStmt) AndLike(col string, content any) *WhereStmt

AndLike 指定 WHERE ... AND col LIKE content

func (*WhereStmt) AndNotBetween

func (stmt *WhereStmt) AndNotBetween(col string, v1, v2 any) *WhereStmt

AndNotBetween 指定 WHERE ... AND col NOT BETWEEN v1 AND v2

func (*WhereStmt) AndNotIn

func (stmt *WhereStmt) AndNotIn(col string, v ...any) *WhereStmt

AndNotIn 指定 WHERE ... AND col NOT IN(v...)

func (*WhereStmt) AndNotLike

func (stmt *WhereStmt) AndNotLike(col string, content any) *WhereStmt

AndNotLike 指定 WHERE ... AND col NOT LIKE content

func (*WhereStmt) Cond

func (stmt *WhereStmt) Cond(expr bool, f func(stmt *WhereStmt)) *WhereStmt

Cond 在 expr 为真时才执行 f 中的内容

expr 为条件表达式,此为 true 时,才会执行 f 函数; f 的原型为 `func(stmt *WhereStmt)` 其中的参数 stmt 即为当前对象的实例;

sql := Where()
sql.Cond(uid > 0, func(sql *WhereStmt) {
    sql.And("uid>?", uid)
});

相当于:

sql := Where()
if uid > 0 {
    sql.And("uid>?", uid)
}

func (*WhereStmt) Delete

func (stmt *WhereStmt) Delete(e core.Engine) *DeleteStmt

Delete 删除指定条件的内容

func (*WhereStmt) Or

func (stmt *WhereStmt) Or(cond string, args ...any) *WhereStmt

Or 添加一条 OR 语句

func (*WhereStmt) OrBetween

func (stmt *WhereStmt) OrBetween(col string, v1, v2 any) *WhereStmt

OrBetween 指定 WHERE ... OR col BETWEEN v1 AND v2

func (*WhereStmt) OrGroup

func (stmt *WhereStmt) OrGroup(f func(*WhereStmt)) *WhereStmt

OrGroup 开始一个子条件语句

func (*WhereStmt) OrIn

func (stmt *WhereStmt) OrIn(col string, v ...any) *WhereStmt

OrIn 指定 WHERE ... OR col IN(v...)

func (*WhereStmt) OrIsNotNull

func (stmt *WhereStmt) OrIsNotNull(col string) *WhereStmt

OrIsNotNull 指定 WHERE ... OR col IS NOT NULL

func (*WhereStmt) OrIsNull

func (stmt *WhereStmt) OrIsNull(col string) *WhereStmt

OrIsNull 指定 WHERE ... OR col IS NULL

func (*WhereStmt) OrLike

func (stmt *WhereStmt) OrLike(col string, content any) *WhereStmt

OrLike 指定 WHERE ... OR col LIKE content

func (*WhereStmt) OrNotBetween

func (stmt *WhereStmt) OrNotBetween(col string, v1, v2 any) *WhereStmt

OrNotBetween 指定 WHERE ... OR col BETWEEN v1 AND v2

func (*WhereStmt) OrNotIn

func (stmt *WhereStmt) OrNotIn(col string, v ...any) *WhereStmt

OrNotIn 指定 WHERE ... OR col IN(v...)

func (*WhereStmt) OrNotLike

func (stmt *WhereStmt) OrNotLike(col string, content any) *WhereStmt

OrNotLike 指定 WHERE ... OR col NOT LIKE content

func (*WhereStmt) Reset

func (stmt *WhereStmt) Reset()

Reset 重置内容

func (*WhereStmt) SQL

func (stmt *WhereStmt) SQL() (string, []any, error)

SQL 生成 SQL 语句和对应的参数返回

func (*WhereStmt) Select

func (stmt *WhereStmt) Select(e core.Engine) *SelectStmt

Select 生成 select 语句

func (*WhereStmt) Update

func (stmt *WhereStmt) Update(e core.Engine) *UpdateStmt

Update 更新指定条件内容

type WhereStmtOf

type WhereStmtOf[T any] struct {
	// contains filtered or unexported fields
}

WhereStmtOf 用于将 WhereStmt 的方法与其它对象组合

func NewWhereStmtOf

func NewWhereStmtOf[T any](t T) *WhereStmtOf[T]

func (*WhereStmtOf[T]) And

func (stmt *WhereStmtOf[T]) And(cond string, args ...any) T

func (*WhereStmtOf[T]) AndBetween

func (stmt *WhereStmtOf[T]) AndBetween(col string, v1, v2 any) T

AndBetween 指定 WHERE ... AND col BETWEEN v1 AND v2

func (*WhereStmtOf[T]) AndGroup

func (stmt *WhereStmtOf[T]) AndGroup(f func(*WhereStmt)) T

AndGroup 开始一个子条件语句

func (*WhereStmtOf[T]) AndIn

func (stmt *WhereStmtOf[T]) AndIn(col string, v ...any) T

AndIn 指定 WHERE ... AND col IN(v...)

func (*WhereStmtOf[T]) AndIsNotNull

func (stmt *WhereStmtOf[T]) AndIsNotNull(col string) T

AndIsNotNull 指定 WHERE ... AND col IS NOT NULL

func (*WhereStmtOf[T]) AndIsNull

func (stmt *WhereStmtOf[T]) AndIsNull(col string) T

AndIsNull 指定 WHERE ... AND col IS NULL

func (*WhereStmtOf[T]) AndLike

func (stmt *WhereStmtOf[T]) AndLike(col string, content any) T

AndLike 指定 WHERE ... AND col LIKE content

func (*WhereStmtOf[T]) AndNotBetween

func (stmt *WhereStmtOf[T]) AndNotBetween(col string, v1, v2 any) T

AndNotBetween 指定 WHERE ... AND col NOT BETWEEN v1 AND v2

func (*WhereStmtOf[T]) AndNotIn

func (stmt *WhereStmtOf[T]) AndNotIn(col string, v ...any) T

AndNotIn 指定 WHERE ... AND col NOT IN(v...)

func (*WhereStmtOf[T]) AndNotLike

func (stmt *WhereStmtOf[T]) AndNotLike(col string, content any) T

AndNotLike 指定 WHERE ... AND col NOT LIKE content

func (*WhereStmtOf[T]) Cond

func (stmt *WhereStmtOf[T]) Cond(expr bool, f func(stmt *WhereStmt)) T

Cond 在 expr 为真时才执行 f 中的内容

func (*WhereStmtOf[T]) Or

func (stmt *WhereStmtOf[T]) Or(cond string, args ...any) T

Or 添加一条 OR 语句

func (*WhereStmtOf[T]) OrBetween

func (stmt *WhereStmtOf[T]) OrBetween(col string, v1, v2 any) T

OrBetween 指定 WHERE ... OR col BETWEEN v1 AND v2

func (*WhereStmtOf[T]) OrGroup

func (stmt *WhereStmtOf[T]) OrGroup(f func(*WhereStmt)) T

OrGroup 开始一个子条件语句

func (*WhereStmtOf[T]) OrIn

func (stmt *WhereStmtOf[T]) OrIn(col string, v ...any) T

OrIn 指定 WHERE ... OR col IN(v...)

func (*WhereStmtOf[T]) OrIsNotNull

func (stmt *WhereStmtOf[T]) OrIsNotNull(col string) T

OrIsNotNull 指定 WHERE ... OR col IS NOT NULL

func (*WhereStmtOf[T]) OrIsNull

func (stmt *WhereStmtOf[T]) OrIsNull(col string) T

OrIsNull 指定 WHERE ... OR col IS NULL

func (*WhereStmtOf[T]) OrLike

func (stmt *WhereStmtOf[T]) OrLike(col string, content any) T

OrLike 指定 WHERE ... OR col LIKE content

func (*WhereStmtOf[T]) OrNotBetween

func (stmt *WhereStmtOf[T]) OrNotBetween(col string, v1, v2 any) T

OrNotBetween 指定 WHERE ... OR col BETWEEN v1 AND v2

func (*WhereStmtOf[T]) OrNotIn

func (stmt *WhereStmtOf[T]) OrNotIn(col string, v ...any) T

OrNotIn 指定 WHERE ... OR col IN(v...)

func (*WhereStmtOf[T]) OrNotLike

func (stmt *WhereStmtOf[T]) OrNotLike(col string, content any) T

OrNotLike 指定 WHERE ... OR col NOT LIKE content

func (*WhereStmtOf[T]) Where

func (stmt *WhereStmtOf[T]) Where(cond string, args ...any) T

func (*WhereStmtOf[T]) WhereStmt

func (stmt *WhereStmtOf[T]) WhereStmt() *WhereStmt

Jump to

Keyboard shortcuts

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