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 Arg
- type ArgWriter
- 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 Quoter
- 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) 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
- type TableDef
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 Arg ¶ added in v0.7.0
Arg 参数修饰接口: 实现者自定义该参数在 SQL 中的渲染方式。
build 对 #{} 参数检测 Arg, 命中则委托 WriteTo, 否则按单值绑定。 用户可通过实现本接口扩展参数语义, 无需修改 dba (GORM clause.Expression 同款模式)。
func Expand ¶ added in v0.7.0
Expand 展开切片/数组为独立占位符:
q.Add("WHERE id IN (#{1})", dba.Expand([]int{1, 2})) → IN ($1, $2)
空切片展开为 0 个参数 (如 `IN ()`), 由数据库报语法错误, 框架不拦截。 []byte 同样逐字节展开 (显式意图自担)。
func ExpandOrNull ¶ added in v0.7.0
ExpandOrNull 与 Expand 相同, 但空切片渲染为 NULL 字面量:
q.Add("WHERE id IN (#{1})", dba.ExpandOrNull([]int{})) → IN (NULL)
type ArgWriter ¶ added in v0.7.0
type ArgWriter interface {
// AddParam 写入一个占位符并收集绑定参数 (内部维护序号与方言格式)。
AddParam(v any)
// WriteString 写入原始 SQL 文本。
WriteString(s string)
}
ArgWriter 参数渲染出口: Arg 实现者通过它写入占位符与原始文本。 实现者不应持有 ArgWriter 超过 WriteTo 调用周期。
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 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) 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.
type TableDef ¶
type TableDef struct {
// contains filtered or unexported fields
}
TableDef builds a set of Node variables for a table, enabling Add("select ${u.*} from ${u}"). Usage:
Vars(Table("user", "u").Fields("id", "name").Build())
func (*TableDef) Build ¶
Build returns a map[string]Node suitable for SQL.Vars(). 每条规则注册一个 ${key}, key 可以是表名、别名、带限定符的字段:
Table("users", "u").Fields("id", "name").Build()
"u" → Node{RawSQL: "@{1} AS @{2}", Args: []any{"users", "u"}} → `users` AS `u`
"u.id" → Node{RawSQL: "@{1}.@{2}", Args: []any{"u", "id"}} → `u`.`id`
"u.name" → Node{RawSQL: "@{1}.@{2}", Args: []any{"u", "name"}} → `u`.`name`
"u.*" → Node{RawSQL: "@{1}.*", Args: []any{"u"}} → `u`.*
"users" → Node{RawSQL: "@{1}", Args: []any{"users"}} → `users`
"users.id" → Node{RawSQL: "@{1}.@{2}", Args: []any{"users", "id"}} → `users`.`id`
"users.*" → Node{RawSQL: "@{1}.*", Args: []any{"users"}} → `users`.*
用法:
Vars(Table("users", "u").Build())
Add("SELECT ${u.*} FROM ${u} LEFT JOIN @{profiles} p ON ${u.id} = p.user_id")