sqlbuilder

package
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2019 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

View Source
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 CreateIndex

func CreateIndex(e Engine) *CreateIndexStmt

CreateIndex 声明一条 CrateIndexStmt 语句

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

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

ExecContext 执行 SQL 语句

func (*CreateIndexStmt) Name

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

Name 指定约束名

func (*CreateIndexStmt) Reset

func (stmt *CreateIndexStmt) Reset()

Reset 重置

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 Delete

func Delete(e Engine) *DeleteStmt

Delete 声明一条删除语句

func (*DeleteStmt) And

func (stmt *DeleteStmt) And(cond string, args ...interface{}) *DeleteStmt

And 指定 where ... AND ... 语句

func (*DeleteStmt) Exec

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

Exec 执行 SQL 语句

func (*DeleteStmt) ExecContext

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

ExecContext 执行 SQL 语句

func (*DeleteStmt) Or

func (stmt *DeleteStmt) Or(cond string, args ...interface{}) *DeleteStmt

Or 指定 where ... OR ... 语句

func (*DeleteStmt) Prepare

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

Prepare 预编译

func (*DeleteStmt) PrepareContext

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

PrepareContext 预编译

func (*DeleteStmt) Reset

func (stmt *DeleteStmt) Reset()

Reset 重置语句

func (*DeleteStmt) SQL

func (stmt *DeleteStmt) SQL() (string, []interface{}, error)

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

func (*DeleteStmt) Table

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

Table 指定表名

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 DropTable

func DropTable(e Engine) *DropTableStmt

DropTable 声明一条删除表的语句

func (*DropTableStmt) Exec

func (stmt *DropTableStmt) Exec() (sql.Result, error)

Exec 执行 SQL 语句

func (*DropTableStmt) ExecContext

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

ExecContext 执行 SQL 语句

func (*DropTableStmt) Reset

func (stmt *DropTableStmt) Reset()

Reset 重置

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 Insert

func Insert(e Engine, d Dialect) *InsertStmt

Insert 声明一条插入语句

func (*InsertStmt) Columns

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

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

func (*InsertStmt) Exec

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

Exec 执行 SQL 语句

func (*InsertStmt) ExecContext

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

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

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

Prepare 预编译

func (*InsertStmt) PrepareContext

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

PrepareContext 预编译

func (*InsertStmt) Reset

func (stmt *InsertStmt) Reset()

Reset 重置语句

func (*InsertStmt) SQL

func (stmt *InsertStmt) SQL() (string, []interface{}, error)

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

func (*InsertStmt) Table

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

Table 指定表名

func (*InsertStmt) Values

func (stmt *InsertStmt) Values(vals ...interface{}) *InsertStmt

Values 指定需要插入的值

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

type SQLBuilder

type SQLBuilder bytes.Buffer

SQLBuilder 对 bytes.Buffer 的一个简单封装。 当 Write* 系列函数出错时,直接 panic。

func New

func New(str string) *SQLBuilder

New 声明一个新的 SQLBuilder 实例

func (*SQLBuilder) Bytes

func (b *SQLBuilder) Bytes() []byte

Bytes 获取表示的字符串

func (*SQLBuilder) Len

func (b *SQLBuilder) Len() int

Len 获取长度

func (*SQLBuilder) Reset

func (b *SQLBuilder) Reset() *SQLBuilder

Reset 重置内容

func (*SQLBuilder) String

func (b *SQLBuilder) String() string

String 获取表示的字符串

func (*SQLBuilder) TruncateLast

func (b *SQLBuilder) TruncateLast(n int) *SQLBuilder

TruncateLast 去掉最后几个字符

func (*SQLBuilder) WriteByte

func (b *SQLBuilder) WriteByte(c byte) *SQLBuilder

WriteByte 写入一字符

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 Select

func Select(e Engine, d Dialect) *SelectStmt

Select 声明一条 Select 语句

func (*SelectStmt) And

func (stmt *SelectStmt) And(cond string, args ...interface{}) *SelectStmt

And 指定 where ... AND ... 语句

func (*SelectStmt) Asc

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

Asc 正序查询

func (*SelectStmt) Count

func (stmt *SelectStmt) Count(expr string) *SelectStmt

Count 指定 Count 表示式,如果指定了 count 表达式,则会造成 limit 失效。

传递空的 expr 参数,表示去除 count 表达式。

func (*SelectStmt) Desc

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

Desc 倒序查询

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) *SelectStmt

From 指定表名

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

func (stmt *SelectStmt) Prepare() (*sql.Stmt, error)

Prepare 预编译

func (*SelectStmt) PrepareContext

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

PrepareContext 预编译

func (*SelectStmt) Query

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

Query 查询

func (*SelectStmt) QueryContext

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

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

func (stmt *SelectStmt) Reset()

Reset 重置语句

func (*SelectStmt) SQL

func (stmt *SelectStmt) SQL() (string, []interface{}, error)

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

func (*SelectStmt) Select

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

Select 指定列名

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 Update

func Update(e Engine) *UpdateStmt

Update 声明一条 UPDATE 的 SQL 语句

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

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

Exec 执行 SQL 语句

func (*UpdateStmt) ExecContext

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

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

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

Prepare 预编译

func (*UpdateStmt) PrepareContext

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

PrepareContext 预编译

func (*UpdateStmt) Reset

func (stmt *UpdateStmt) Reset()

Reset 重置语句

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

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

Table 指定表名

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 部分

func Where

func Where() *WhereStmt

Where 生成一条 Where 语句

func (*WhereStmt) And

func (stmt *WhereStmt) And(cond string, args ...interface{}) *WhereStmt

And 添加一条 and 语句

func (*WhereStmt) AndWhere

func (stmt *WhereStmt) AndWhere(w *WhereStmt) *WhereStmt

AndWhere 开始一个子条件语句

func (*WhereStmt) Or

func (stmt *WhereStmt) Or(cond string, args ...interface{}) *WhereStmt

Or 添加一条 OR 语句

func (*WhereStmt) OrWhere

func (stmt *WhereStmt) OrWhere(w *WhereStmt) *WhereStmt

OrWhere 开始一个子条件语句

func (*WhereStmt) Reset

func (stmt *WhereStmt) Reset()

Reset 重置内容

func (*WhereStmt) SQL

func (stmt *WhereStmt) SQL() (string, []interface{}, error)

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

type WhereStmter

type WhereStmter interface {
	WhereStmt() *WhereStmt
}

WhereStmter 带 Where 语句的 SQL

Jump to

Keyboard shortcuts

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