Documentation
¶
Index ¶
- Constants
- func AnsiQuoter(s string) string
- func ColumnsAndValues(model any, omitempty bool) ([]string, []any, error)
- func DollarFormat(idx int) string
- func GroupBy[T any, K comparable](slice []T, fn func(T) K) map[K][]T
- func IndexBy[T any, K comparable](slice []T, fn func(T) K) (map[K]T, error)
- func IsOk(v any) bool
- func Map[T any, R any](slice []T, fn func(T) R) []R
- func MySQLQuoter(s string) string
- func Page[T any](q *SQL, page, size int) ([]T, int64, error)
- func QmarkFormat(_ int) string
- func Scalar[T any](d *SQL) (T, bool, error)
- type BeforeCreate
- type BeforeUpdate
- type Dao
- func (d *Dao[T]) All() ([]T, error)
- func (d *Dao[T]) Batch(entities []T) (int64, error)
- func (d *Dao[T]) Count(where string, args ...any) (int64, error)
- func (d *Dao[T]) CountAll() (int64, error)
- func (d *Dao[T]) Create(data any) (int64, error)
- func (d *Dao[T]) Delete(where string, args ...any) (int64, error)
- func (d *Dao[T]) Exists(where string, args ...any) (bool, error)
- func (d *Dao[T]) Get(where string, args ...any) (*T, error)
- func (d *Dao[T]) GetByID(id any) (*T, error)
- func (d *Dao[T]) List(where string, args ...any) ([]T, error)
- func (d *Dao[T]) PK(pk string) *Dao[T]
- func (d *Dao[T]) Page(page, size int, where string, args ...any) ([]T, int64, error)
- func (d *Dao[T]) RawBatch(entities []T) *SQL
- func (d *Dao[T]) RawCreate(data any) *SQL
- func (d *Dao[T]) RawSelect(where string, args ...any) *SQL
- func (d *Dao[T]) SQL() *SQL
- func (d *Dao[T]) Table(table string) *Dao[T]
- func (d *Dao[T]) Update(data any, where string, args ...any) (int64, error)
- func (d *Dao[T]) Vars(alias string) map[string]Node
- func (d *Dao[T]) WithCtx(ctx context.Context) *Dao[T]
- func (d *Dao[T]) WithTx(tx *SQL) *Dao[T]
- type ExecFunc
- type Formater
- type H
- type Hook
- type Node
- type Pipe
- type Quoter
- type RenderCtx
- type SQL
- func (d *SQL) Add(query string, args ...any) *SQL
- func (d *SQL) AddIf(cond bool, query string, args ...any) *SQL
- func (d *SQL) Batch(rows [][]any) *SQL
- func (d *SQL) BatchInsert(table string, entities []any) *SQL
- func (d *SQL) Begin() (*SQL, error)
- func (d *SQL) Close() error
- func (d *SQL) Commit() error
- func (d *SQL) Delete(table string, where string, args ...any) *SQL
- func (d *SQL) Error() error
- func (d *SQL) Exec() (sql.Result, error)
- func (d *SQL) Formater(formatter Formater) *SQL
- func (d *SQL) Get(dest any) (found bool, err error)
- func (d *SQL) Insert(table string, data any) *SQL
- func (d *SQL) List(dest interface{}) error
- func (d *SQL) Pool() *sqlx.DB
- func (d *SQL) Quoter(quoter Quoter) *SQL
- func (d *SQL) RegisterMacro(prefix byte, pipe string) *SQL
- func (d *SQL) RegisterPipe(name string, fn Pipe) *SQL
- func (d *SQL) Rollback() error
- func (d *SQL) Rows() (*sqlx.Rows, error)
- func (d *SQL) Select(table string, where string, args ...any) *SQL
- func (d *SQL) ToSQL() (string, []any, error)
- func (d *SQL) Transaction(fn func(*SQL) error) error
- func (d *SQL) Unsafe() *SQL
- func (d *SQL) Update(table string, data any, where string, args ...any) *SQL
- func (d *SQL) Use(mw ...Hook) *SQL
- func (d *SQL) Var(key string, query string, args ...any) *SQL
- func (d *SQL) Vars(vars map[string]Node) *SQL
- func (d *SQL) WithCtx(ctx context.Context) *SQL
- type SQLExpr
Constants ¶
const ( F = "F" I = "I" )
F is the default field variable name used by Select and Page.
Variables ¶
This section is empty.
Functions ¶
func ColumnsAndValues ¶ added in v0.5.2
ColumnsAndValues 把 struct 或 map 转换为列名与参数值列表。
struct 策略: 自建递归遍历 (fieldList) 生成字段清单, 遍历时即时判定:
- 原子类型 (driver.Valuer 实现者 / time.Time 及其可转换别名) 作为单列收束;
- struct (匿名嵌入或普通字段, 值或指针) 递归展开子字段;
- 其余基本类型/[]byte 直接作为单列。
time.Time 不实现 Valuer (database/sql 原生参数类型), 由 isAtomicColumn 显式特判。
func GroupBy ¶
func GroupBy[T any, K comparable](slice []T, fn func(T) K) map[K][]T
GroupBy groups slice elements by fn(element): 1 key → N values.
func IndexBy ¶
func IndexBy[T any, K comparable](slice []T, fn func(T) K) (map[K]T, error)
IndexBy converts a slice into a map keyed by fn(element). Returns an error if duplicate keys are found.
Types ¶
type BeforeCreate ¶
type BeforeCreate interface {
BeforeCreate() error
}
BeforeCreate is implemented by structs that need a hook before INSERT.
type BeforeUpdate ¶
type BeforeUpdate interface {
BeforeUpdate() error
}
BeforeUpdate is implemented by structs that need a hook before UPDATE.
type Dao ¶
type Dao[T any] struct { // contains filtered or unexported fields }
Dao is a generic single-table CRUD helper.
func (*Dao[T]) Create ¶
Create inserts a single record and returns the generated primary key. On PostgreSQL, uses RETURNING. On other drivers, uses LastInsertId.
func (*Dao[T]) Get ¶
Get fetches a single record by condition. 未找到时返回 (nil, nil) —— “无结果”是正常业务结果而非错误 (不返回 sql.ErrNoRows); 调用方必须先判 nil 再解引用。
func (*Dao[T]) GetByID ¶
GetByID fetches a single record by primary key. 未找到时返回 (nil, nil) —— “无结果”是正常业务结果而非错误 (不返回 sql.ErrNoRows); 调用方必须先判 nil 再解引用。
func (*Dao[T]) RawBatch ¶
RawBatch bulk-inserts multiple records and returns a SQL builder for chaining.
func (*Dao[T]) RawCreate ¶
RawCreate inserts a single record and returns the SQL builder for chaining (e.g. ON CONFLICT, RETURNING).
type Hook ¶
Hook wraps an ExecFunc in onion-style middleware.
type Pipe ¶ added in v0.8.0
Pipe 管道: 宏内容的解读者。
管道收到宏内容的字面量 (如 #{1|pipe} 的 "1"、@{users} 的 "users"), 由管道自行决定: 字面量直接用 (ident), 或经 ctx.Resolve 取参数 (bind/raw/expand)。 用户管道可自由选择 — 这是管道的灵活性所在。 内置: bind/expand/raw/ident; 用户通过 RegisterPipe 注册。
type RenderCtx ¶ added in v0.8.0
type RenderCtx interface {
// AddParam 写入一个占位符并收集绑定参数 (内部维护序号与方言格式)。
AddParam(v any)
// WriteString 写入原始 SQL 文本。
WriteString(s string)
// QuoteIdent 写入方言 quoting 的标识符。
QuoteIdent(s string)
// Resolve 把宏内容作为参数 key 解析 (位置/命名); 管道按需调用。
Resolve(key string) (any, error)
}
RenderCtx 渲染上下文: 管道与宏的渲染出口。
type SQL ¶
type SQL struct {
// contains filtered or unexported fields
}
SQL is an immutable, chainable query builder backed by sqlx.
func NewFromSqlx ¶
NewFromSqlx creates a SQL builder from a sqlx.DB. Auto-detects driver for placeholder formater and identifier quoting.
func Open ¶
Open connects to a database and returns a SQL builder. It is a convenience over sqlx.Connect + NewFromSqlx.
func (*SQL) BatchInsert ¶
BatchInsert builds a complete INSERT from a slice of entities. All entities must have the same column structure.
func (*SQL) RegisterMacro ¶ added in v0.8.0
RegisterMacro 注册宏别名 (前缀 → 管道名), 返回新 builder:
q := db.RegisterMacro('^', "upper")
q.Add("WHERE name = ^{1}", "bob") → 等价 #{1|upper}
'#'/'$' 为保留前缀不可注册; 管道名允许指向尚未注册的管道 (渲染时报错)。
func (*SQL) RegisterPipe ¶ added in v0.8.0
RegisterPipe 注册自定义管道, 返回新 builder:
q := db.RegisterPipe("upper", func(ctx RenderCtx, v any) error {
ctx.AddParam(strings.ToUpper(fmt.Sprint(v)))
return nil
})
q.Add("WHERE name = #{1|upper}", "bob") → $1 = "BOB"
func (*SQL) Transaction ¶
Transaction executes fn inside a transaction. If fn returns an error or panics, the transaction is rolled back. If already in a transaction, fn is executed directly without nesting.