Documentation
¶
Overview ¶
Package sqlbuilder 提供一套通过字符串拼接来构成 SQL 语句的工具。
Index ¶
- Variables
- type CreateIndexStmt
- func (stmt *CreateIndexStmt) Columns(col ...string) *CreateIndexStmt
- func (stmt *CreateIndexStmt) Exec() (sql.Result, error)
- func (stmt *CreateIndexStmt) ExecContext(ctx context.Context) (sql.Result, error)
- func (stmt *CreateIndexStmt) Name(col string) *CreateIndexStmt
- func (stmt *CreateIndexStmt) Reset()
- func (stmt *CreateIndexStmt) SQL() (string, []interface{}, error)
- func (stmt *CreateIndexStmt) Table(tbl string) *CreateIndexStmt
- type DeleteStmt
- func (stmt *DeleteStmt) And(cond string, args ...interface{}) *DeleteStmt
- func (stmt *DeleteStmt) Exec() (sql.Result, error)
- func (stmt *DeleteStmt) ExecContext(ctx context.Context) (sql.Result, error)
- func (stmt *DeleteStmt) Or(cond string, args ...interface{}) *DeleteStmt
- func (stmt *DeleteStmt) Prepare() (*sql.Stmt, error)
- func (stmt *DeleteStmt) PrepareContext(ctx context.Context) (*sql.Stmt, error)
- func (stmt *DeleteStmt) Reset()
- func (stmt *DeleteStmt) SQL() (string, []interface{}, error)
- func (stmt *DeleteStmt) Table(table string) *DeleteStmt
- func (stmt *DeleteStmt) Where(cond string, args ...interface{}) *DeleteStmt
- func (stmt *DeleteStmt) WhereStmt() *WhereStmt
- type Dialect
- type DropTableStmt
- type Engine
- type InsertStmt
- func (stmt *InsertStmt) Columns(cols ...string) *InsertStmt
- func (stmt *InsertStmt) Exec() (sql.Result, error)
- func (stmt *InsertStmt) ExecContext(ctx context.Context) (sql.Result, error)
- func (stmt *InsertStmt) KeyValue(col string, val interface{}) *InsertStmt
- func (stmt *InsertStmt) LastInsertID(table, col string) (int64, error)
- func (stmt *InsertStmt) LastInsertIDContext(ctx context.Context, table, col string) (id int64, err error)
- func (stmt *InsertStmt) Prepare() (*sql.Stmt, error)
- func (stmt *InsertStmt) PrepareContext(ctx context.Context) (*sql.Stmt, error)
- func (stmt *InsertStmt) Reset()
- func (stmt *InsertStmt) SQL() (string, []interface{}, error)
- func (stmt *InsertStmt) Table(table string) *InsertStmt
- func (stmt *InsertStmt) Values(vals ...interface{}) *InsertStmt
- type SQLBuilder
- func (b *SQLBuilder) Bytes() []byte
- func (b *SQLBuilder) Len() int
- func (b *SQLBuilder) Reset() *SQLBuilder
- func (b *SQLBuilder) String() string
- func (b *SQLBuilder) TruncateLast(n int) *SQLBuilder
- func (b *SQLBuilder) WriteByte(c byte) *SQLBuilder
- func (b *SQLBuilder) WriteString(str string) *SQLBuilder
- type SQLer
- type SelectStmt
- func (stmt *SelectStmt) And(cond string, args ...interface{}) *SelectStmt
- func (stmt *SelectStmt) Asc(col ...string) *SelectStmt
- func (stmt *SelectStmt) Count(expr string) *SelectStmt
- func (stmt *SelectStmt) Desc(col ...string) *SelectStmt
- func (stmt *SelectStmt) Distinct() *SelectStmt
- func (stmt *SelectStmt) ForUpdate() *SelectStmt
- func (stmt *SelectStmt) From(table string) *SelectStmt
- func (stmt *SelectStmt) Group(col string) *SelectStmt
- func (stmt *SelectStmt) Having(expr string, args ...interface{}) *SelectStmt
- func (stmt *SelectStmt) Join(typ, table, on string) *SelectStmt
- func (stmt *SelectStmt) Limit(limit interface{}, offset ...interface{}) *SelectStmt
- func (stmt *SelectStmt) Or(cond string, args ...interface{}) *SelectStmt
- func (stmt *SelectStmt) Prepare() (*sql.Stmt, error)
- func (stmt *SelectStmt) PrepareContext(ctx context.Context) (*sql.Stmt, error)
- func (stmt *SelectStmt) Query() (*sql.Rows, error)
- func (stmt *SelectStmt) QueryContext(ctx context.Context) (*sql.Rows, error)
- func (stmt *SelectStmt) QueryFloat(colName string) (float64, error)
- func (stmt *SelectStmt) QueryInt(colName string) (int64, error)
- func (stmt *SelectStmt) QueryObject(strict bool, objs interface{}) (int, error)
- func (stmt *SelectStmt) Reset()
- func (stmt *SelectStmt) SQL() (string, []interface{}, error)
- func (stmt *SelectStmt) Select(cols ...string) *SelectStmt
- func (stmt *SelectStmt) Where(cond string, args ...interface{}) *SelectStmt
- func (stmt *SelectStmt) WhereStmt() *WhereStmt
- type UpdateStmt
- func (stmt *UpdateStmt) And(cond string, args ...interface{}) *UpdateStmt
- func (stmt *UpdateStmt) Decrease(col string, val interface{}) *UpdateStmt
- func (stmt *UpdateStmt) Exec() (sql.Result, error)
- func (stmt *UpdateStmt) ExecContext(ctx context.Context) (sql.Result, error)
- func (stmt *UpdateStmt) Increase(col string, val interface{}) *UpdateStmt
- func (stmt *UpdateStmt) OCC(col string, val interface{}) *UpdateStmt
- func (stmt *UpdateStmt) Or(cond string, args ...interface{}) *UpdateStmt
- func (stmt *UpdateStmt) Prepare() (*sql.Stmt, error)
- func (stmt *UpdateStmt) PrepareContext(ctx context.Context) (*sql.Stmt, error)
- func (stmt *UpdateStmt) Reset()
- func (stmt *UpdateStmt) SQL() (string, []interface{}, error)
- func (stmt *UpdateStmt) Set(col string, val interface{}) *UpdateStmt
- func (stmt *UpdateStmt) Table(table string) *UpdateStmt
- func (stmt *UpdateStmt) Where(cond string, args ...interface{}) *UpdateStmt
- func (stmt *UpdateStmt) WhereStmt() *WhereStmt
- type WhereStmt
- func (stmt *WhereStmt) And(cond string, args ...interface{}) *WhereStmt
- func (stmt *WhereStmt) AndWhere(w *WhereStmt) *WhereStmt
- func (stmt *WhereStmt) Or(cond string, args ...interface{}) *WhereStmt
- func (stmt *WhereStmt) OrWhere(w *WhereStmt) *WhereStmt
- func (stmt *WhereStmt) Reset()
- func (stmt *WhereStmt) SQL() (string, []interface{}, error)
- type WhereStmter
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTableIsEmpty 未指定表名,任何 SQL 语句中, // 若未指定表名时,会返回此错误 ErrTableIsEmpty = errors.New("表名为空") // ErrValueIsEmpty 在 Update 和 Insert 语句中, // 若未指定任何值,则返回此错误 ErrValueIsEmpty = errors.New("值为空") // ErrColumnsIsEmpty 在 Insert 和 Select 语句中, // 若未指定任何列表,则返回此错误 ErrColumnsIsEmpty = errors.New("未指定列") // ErrDupColumn 在 Update 中可能存在重复设置的列名。 ErrDupColumn = errors.New("重复的列名") // ErrArgsNotMatch 在生成的 SQL 语句中,传递的参数与语句的占位符数量不匹配。 ErrArgsNotMatch = errors.New("列与值的数量不匹配") )
Functions ¶
This section is empty.
Types ¶
type CreateIndexStmt ¶
type CreateIndexStmt struct {
// contains filtered or unexported fields
}
CreateIndexStmt 创建索引的语句
func (*CreateIndexStmt) Columns ¶
func (stmt *CreateIndexStmt) Columns(col ...string) *CreateIndexStmt
Columns 列名
func (*CreateIndexStmt) Exec ¶
func (stmt *CreateIndexStmt) Exec() (sql.Result, error)
Exec 执行 SQL 语句
func (*CreateIndexStmt) ExecContext ¶
ExecContext 执行 SQL 语句
func (*CreateIndexStmt) Name ¶
func (stmt *CreateIndexStmt) Name(col string) *CreateIndexStmt
Name 指定约束名
func (*CreateIndexStmt) SQL ¶
func (stmt *CreateIndexStmt) SQL() (string, []interface{}, error)
SQL 生成 SQL 语句
func (*CreateIndexStmt) Table ¶
func (stmt *CreateIndexStmt) Table(tbl string) *CreateIndexStmt
Table 指定表名
type DeleteStmt ¶
type DeleteStmt struct {
// contains filtered or unexported fields
}
DeleteStmt 表示删除操作的 SQL 语句
func (*DeleteStmt) And ¶
func (stmt *DeleteStmt) And(cond string, args ...interface{}) *DeleteStmt
And 指定 where ... AND ... 语句
func (*DeleteStmt) ExecContext ¶
ExecContext 执行 SQL 语句
func (*DeleteStmt) Or ¶
func (stmt *DeleteStmt) Or(cond string, args ...interface{}) *DeleteStmt
Or 指定 where ... OR ... 语句
func (*DeleteStmt) PrepareContext ¶
PrepareContext 预编译
func (*DeleteStmt) SQL ¶
func (stmt *DeleteStmt) SQL() (string, []interface{}, error)
SQL 获取 SQL 语句,以及其参数对应的具体值
func (*DeleteStmt) Where ¶
func (stmt *DeleteStmt) Where(cond string, args ...interface{}) *DeleteStmt
Where 指定 where 语句
func (*DeleteStmt) WhereStmt ¶
func (stmt *DeleteStmt) WhereStmt() *WhereStmt
WhereStmt 实现 WhereStmter 接口
type Dialect ¶
type Dialect interface { // 生成 `LIMIT N OFFSET M` 或是相同的语意的语句。 // // offset 值为一个可选参数,若不指定,则表示 `LIMIT N` 语句。 // 返回的是对应数据库的 limit 语句以及语句中占位符对应的值。 // // limit 和 offset 可以是 sql.NamedArg 类型。 LimitSQL(limit interface{}, offset ...interface{}) (string, []interface{}) // 自定义获取 LastInsertID 的获取方式。 // // 类似于 postgresql 等都需要额外定义。 // // 返回参数 sql 表示额外的语句,如果为空,则执行的是标准的 SQL 插入语句; // append 表示在 sql 不为空的情况下,sql 与现有的插入语句的结合方式, // 如果为 true 表示直接添加在插入语句之后,否则为一条新的语句。 LastInsertID(table, col string) (sql string, append bool) }
Dialect 接口用于描述与数据库相关的一些语言特性。
type DropTableStmt ¶
type DropTableStmt struct {
// contains filtered or unexported fields
}
DropTableStmt 删除表语句
func (*DropTableStmt) ExecContext ¶
ExecContext 执行 SQL 语句
func (*DropTableStmt) SQL ¶
func (stmt *DropTableStmt) SQL() (string, []interface{}, error)
SQL 获取 SQL 语句以及对应的参数
func (*DropTableStmt) Table ¶
func (stmt *DropTableStmt) Table(table string) *DropTableStmt
Table 指定表名。 重复指定,会覆盖之前的。
type Engine ¶
type Engine interface { // 执行一条查询语句,并返回相应的 sql.Rows 实例。 // 功能等同于标准库 database/sql 的 DB.Query() // // query 会被作相应的转换。以 mysql 为例,假设当前的 prefix 为 p_ // select * from #user where {group}=1 // // 转换后 // select * from prefix_user where `group`=1 Query(query string, args ...interface{}) (*sql.Rows, error) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) QueryRow(query string, args ...interface{}) *sql.Row QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row Exec(query string, args ...interface{}) (sql.Result, error) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) Prepare(query string) (*sql.Stmt, error) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) }
Engine 数据库执行的基本接口。
type InsertStmt ¶
type InsertStmt struct {
// contains filtered or unexported fields
}
InsertStmt 表示插入操作的 SQL 语句
func (*InsertStmt) Columns ¶
func (stmt *InsertStmt) Columns(cols ...string) *InsertStmt
Columns 指定插入的列,多次指定,之前的会被覆盖。
func (*InsertStmt) ExecContext ¶
ExecContext 执行 SQL 语句
func (*InsertStmt) KeyValue ¶
func (stmt *InsertStmt) KeyValue(col string, val interface{}) *InsertStmt
KeyValue 指定键值对
当通过 Values() 指定多行数据时,再使用 KeyValue 会出错
func (*InsertStmt) LastInsertID ¶
func (stmt *InsertStmt) LastInsertID(table, col string) (int64, error)
LastInsertID 执行 SQL 语句
并根据表名和自增列 ID 返回当前行的自增 ID 值。
NOTE: 对于指定了自增值的,其结果是未知的。
func (*InsertStmt) LastInsertIDContext ¶
func (stmt *InsertStmt) LastInsertIDContext(ctx context.Context, table, col string) (id int64, err error)
LastInsertIDContext 执行 SQL 语句
并根据表名和自增列 ID 返回当前行的自增 ID 值。
func (*InsertStmt) PrepareContext ¶
PrepareContext 预编译
func (*InsertStmt) SQL ¶
func (stmt *InsertStmt) SQL() (string, []interface{}, error)
SQL 获取 SQL 的语句及参数部分
func (*InsertStmt) Values ¶
func (stmt *InsertStmt) Values(vals ...interface{}) *InsertStmt
Values 指定需要插入的值
NOTE: vals 传入时,并不会被解压
type SQLBuilder ¶
SQLBuilder 对 bytes.Buffer 的一个简单封装。 当 Write* 系列函数出错时,直接 panic。
func (*SQLBuilder) TruncateLast ¶
func (b *SQLBuilder) TruncateLast(n int) *SQLBuilder
TruncateLast 去掉最后几个字符
func (*SQLBuilder) WriteString ¶
func (b *SQLBuilder) WriteString(str string) *SQLBuilder
WriteString 写入一字符串
type SQLer ¶
type SQLer interface { // 获取 SQL 语句以及其关联的参数 SQL() (query string, args []interface{}, err error) // 重置整个 SQL 语句。 Reset() }
SQLer 定义 SQL 语句的基本接口
type SelectStmt ¶
type SelectStmt struct {
// contains filtered or unexported fields
}
SelectStmt 查询语句
func (*SelectStmt) And ¶
func (stmt *SelectStmt) And(cond string, args ...interface{}) *SelectStmt
And 指定 where ... AND ... 语句
func (*SelectStmt) Count ¶
func (stmt *SelectStmt) Count(expr string) *SelectStmt
Count 指定 Count 表示式,如果指定了 count 表达式,则会造成 limit 失效。
传递空的 expr 参数,表示去除 count 表达式。
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) Group ¶
func (stmt *SelectStmt) Group(col string) *SelectStmt
Group 添加 GROUP BY 语句
func (*SelectStmt) Having ¶
func (stmt *SelectStmt) Having(expr string, args ...interface{}) *SelectStmt
Having 指定 having 语句
func (*SelectStmt) Join ¶
func (stmt *SelectStmt) Join(typ, table, on string) *SelectStmt
Join 添加一条 Join 语句
func (*SelectStmt) Limit ¶
func (stmt *SelectStmt) Limit(limit interface{}, offset ...interface{}) *SelectStmt
Limit 生成 SQL 的 Limit 语句
func (*SelectStmt) Or ¶
func (stmt *SelectStmt) Or(cond string, args ...interface{}) *SelectStmt
Or 指定 where ... OR ... 语句
func (*SelectStmt) PrepareContext ¶
PrepareContext 预编译
func (*SelectStmt) QueryContext ¶
QueryContext 查询
func (*SelectStmt) QueryFloat ¶
func (stmt *SelectStmt) QueryFloat(colName string) (float64, error)
QueryFloat 查询指定列的第一行数据,并将其转换成 float64
func (*SelectStmt) QueryInt ¶
func (stmt *SelectStmt) QueryInt(colName string) (int64, error)
QueryInt 查询指定列的第一行数据,并将其转换成 int64
func (*SelectStmt) QueryObject ¶
func (stmt *SelectStmt) QueryObject(strict bool, objs interface{}) (int, error)
QueryObject 将符合当前条件的所有记录依次写入 objs 中。
关于 objs 的值类型,可以参考 github.com/issue9/orm/fetch.Object 函数的相关介绍。
func (*SelectStmt) SQL ¶
func (stmt *SelectStmt) SQL() (string, []interface{}, error)
SQL 获取 SQL 语句及对应的参数
func (*SelectStmt) Where ¶
func (stmt *SelectStmt) Where(cond string, args ...interface{}) *SelectStmt
Where 指定 where 语句
func (*SelectStmt) WhereStmt ¶
func (stmt *SelectStmt) WhereStmt() *WhereStmt
WhereStmt 实现 WhereStmter 接口
type UpdateStmt ¶
type UpdateStmt struct {
// contains filtered or unexported fields
}
UpdateStmt 更新语句
func (*UpdateStmt) And ¶
func (stmt *UpdateStmt) And(cond string, args ...interface{}) *UpdateStmt
And 指定 where ... AND ... 语句
func (*UpdateStmt) Decrease ¶
func (stmt *UpdateStmt) Decrease(col string, val interface{}) *UpdateStmt
Decrease 给钱减少值
func (*UpdateStmt) ExecContext ¶
ExecContext 执行 SQL 语句
func (*UpdateStmt) Increase ¶
func (stmt *UpdateStmt) Increase(col string, val interface{}) *UpdateStmt
Increase 给列增加值
func (*UpdateStmt) OCC ¶
func (stmt *UpdateStmt) OCC(col string, val interface{}) *UpdateStmt
OCC 指定一个用于乐观锁的字段。
val 表示乐观锁原始的值。
func (*UpdateStmt) Or ¶
func (stmt *UpdateStmt) Or(cond string, args ...interface{}) *UpdateStmt
Or 指定 where ... OR ... 语句
func (*UpdateStmt) PrepareContext ¶
PrepareContext 预编译
func (*UpdateStmt) SQL ¶
func (stmt *UpdateStmt) SQL() (string, []interface{}, error)
SQL 获取 SQL 语句以及对应的参数
func (*UpdateStmt) Set ¶
func (stmt *UpdateStmt) Set(col string, val interface{}) *UpdateStmt
Set 设置值,若 col 相同,则会覆盖
func (*UpdateStmt) Where ¶
func (stmt *UpdateStmt) Where(cond string, args ...interface{}) *UpdateStmt
Where 指定 where 语句
func (*UpdateStmt) WhereStmt ¶
func (stmt *UpdateStmt) WhereStmt() *WhereStmt
WhereStmt 实现 WhereStmter 接口
type WhereStmt ¶
type WhereStmt struct {
// contains filtered or unexported fields
}
WhereStmt SQL 语句的 where 部分
type WhereStmter ¶
type WhereStmter interface {
WhereStmt() *WhereStmt
}
WhereStmter 带 Where 语句的 SQL