sqls

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSQLDataType 不合法的插入或更新数据类型错误
	ErrSQLDataType = errors.New("invaild data type, expects: struct, *struct, map[string]any")

	// ErrSQLBatchDataType 不合法的批量插入数据类型错误
	ErrSQLBatchDataType = errors.New("invaild data type, expects: []struct, []*struct, []map[string]any")
)

Functions

This section is empty.

Types

type Clause

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

Clause SQL语句

func Expr

func Expr(query string, binds ...any) *Clause

Expr 生成一个语句表达式,例如:contrib.Expr("price * ? + ?", 2, 100)

type Option

type Option func(w *wrapper)

Option SQL查询选项

func CrossJoin

func CrossJoin(table string) Option

CrossJoin 指定 `CROSS JOIN` 语句

func Distinct

func Distinct(columns ...string) Option

Distinct 指定 `DISTINCT` 子句

func FullJoin

func FullJoin(table, on string) Option

FullJoin 指定 `FULL JOIN` 子句

func GroupBy

func GroupBy(columns ...string) Option

GroupBy 指定 `GROUP BY` 子句

func Having

func Having(query string, binds ...any) Option

Having 指定 `HAVING` 子句

func Join

func Join(table, on string) Option

Join 指定 `INNER JOIN` 子句

func LeftJoin

func LeftJoin(table, on string) Option

LeftJoin 指定 `LEFT JOIN` 子句

func Limit

func Limit(n int) Option

Limit 指定 `LIMIT` 子句

func Offset

func Offset(n int) Option

Offset 指定 `OFFSET` 子句

func OrderBy

func OrderBy(columns ...string) Option

OrderBy 指定 `ORDER BY` 子句

func Returning

func Returning(columns ...string) Option

Returning 指定 `RETURNING` 子句; 用于 PostgresSQL 和 SQLite(3.35.0) `INSERT` 语句

func RightJoin

func RightJoin(table, on string) Option

RightJoin 指定 `RIGHT JOIN` 子句

func Select

func Select(columns ...string) Option

Select 指定查询字段名

func Table

func Table(name string) Option

Table 指定查询表名称

func Union

func Union(ws ...Wrapper) Option

Union 指定 `UNION` 子句

func UnionAll

func UnionAll(ws ...Wrapper) Option

UnionAll 指定 `UNION ALL` 子句

func Where

func Where(query string, binds ...any) Option

Where 指定 `WHERE` 子句

func WhereIn

func WhereIn(query string, binds ...any) Option

WhereIn 指定 `IN` 子句

type SQLBuilder

type SQLBuilder interface {
	TXBuilder
	// Transaction 启用事务
	Transaction(ctx context.Context, f func(ctx context.Context, tx TXBuilder) error) error
}

SQLBuilder SQL构造器

func New

func New(db *sqlx.DB, logFn func(ctx context.Context, query string, args ...any)) SQLBuilder

New 生成SQL构造器

type TXBuilder

type TXBuilder interface {
	// Wrap 包装查询选项
	Wrap(opts ...Option) Wrapper
	// contains filtered or unexported methods
}

TXBuilder 事务构造器

type Wrapper

type Wrapper interface {
	// One 查询一条数据
	One(ctx context.Context, data any) error
	// All 查询多条数据
	All(ctx context.Context, data any) error
	// Insert 插入数据 (数据类型:`struct`, `*struct`, `map[string]any`)
	Insert(ctx context.Context, data any) (sql.Result, error)
	// BatchInsert 批量插入数据 (数据类型:`[]struct`, `[]*struct`, `[]map[string]any`)
	BatchInsert(ctx context.Context, data any) (sql.Result, error)
	// Update 更新数据 (数据类型:`struct`, `*struct`, `map[string]any`)
	Update(ctx context.Context, data any) (sql.Result, error)
	// Delete 删除数据
	Delete(ctx context.Context) (sql.Result, error)
	// Truncate 清空表
	Truncate(ctx context.Context) (sql.Result, error)
}

Wrapper SQL包装器

Jump to

Keyboard shortcuts

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