Documentation
¶
Index ¶
- Variables
- type Clause
- type Condition
- type DB
- func (d *DB) Close() error
- func (d *DB) Create(m model.Model) error
- func (d *DB) Delete(m model.Model, cond storage.Condition, rest ...storage.Condition) error
- func (d *DB) Query(m model.Model) *QB
- func (d *DB) RawConn() storage.Conn
- func (d *DB) SetLog(fn func(messages ...any))
- func (d *DB) Tx(fn func(tx *DB) error) error
- func (d *DB) Update(m model.Model, cond storage.Condition, rest ...storage.Condition) error
- func (d *DB) UpdateFields(m model.Model, fields []string, cond storage.Condition, ...) error
- type Order
- type OrderClause
- type QB
- func (qb *QB) GroupBy(columns ...string) *QB
- func (qb *QB) Limit(limit int) *QB
- func (qb *QB) Offset(offset int) *QB
- func (qb *QB) Or() *QB
- func (qb *QB) OrderBy(column string) *OrderClause
- func (qb *QB) ReadAll(new func() model.Model, onRow func(model.Model)) error
- func (qb *QB) ReadOne() error
- func (qb *QB) Where(column string) *Clause
Constants ¶
This section is empty.
Variables ¶
var ( Eq = storage.Eq Neq = storage.Neq Gt = storage.Gt Gte = storage.Gte Lt = storage.Lt Lte = storage.Lte Like = storage.Like In = storage.In Or = storage.Or IsNotNull = storage.IsNotNull Asc = storage.Asc Desc = storage.Desc )
var ErrEmptyTable = fmt.Err("name", "table", "empty")
ErrEmptyTable is returned when ModelName() returns an empty string.
var ErrNoTxSupport = fmt.Err("transaction", "not", "supported")
ErrNoTxSupport is returned by DB.Tx() when the underlying storage.Conn does not implement storage.TxExecutor.
var ErrNotFound = fmt.Err("record", "not", "found")
ErrNotFound is returned when ReadOne() finds no matching row. Translates storage.ErrNoRows — storage itself has no concept of "not found", only "no rows" (see qb.go's ReadOne).
var ErrValidation = fmt.Err("error", "validation")
ErrValidation is returned when validate() finds a mismatch.
Functions ¶
This section is empty.
Types ¶
type Clause ¶ added in v0.0.8
type Clause struct {
// contains filtered or unexported fields
}
Clause represents an intermediate state for building a query condition.
type Condition ¶
Re-exports of storage's DML value types and condition/order constructors, so that a consumer calling Update/Delete (which take a storage.Condition explicitly) doesn't need a second import just for Eq/Gt/etc. These are aliases, not new types/wrappers — orm.Condition IS storage.Condition, orm.Eq IS storage.Eq. Zero duplication, zero conversion. See docs/PLAN.md §3.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB represents an ergonomic handle over a storage backend (a storage.Conn). Consumers instantiate it via New(). This type owns no contract — storage.Conn is the contract; DB is the fluent layer on top of it (see docs/ARQUITECTURE.md).
func New ¶
New wraps a storage.Conn (a backend's Executor+Compiler pair, e.g. sqlt.Open(dsn) or mem.New()) in the ergonomic DB handle. One argument, not two: storage.Conn already unifies Executor+Compiler so an Executor from one backend can never be paired with a Compiler from another.
func (*DB) Delete ¶
Delete deletes a model from the database. At least one Condition is required. Providing zero conditions is a compile-time error, preventing accidental full-table DELETE statements.
func (*DB) RawConn ¶ added in v0.11.0
RawConn returns the underlying storage.Conn. Renamed from RawExecutor: what's underneath is a full Conn (Executor+Compiler), not just an Executor.
func (*DB) SetLog ¶ added in v0.9.1
SetLog sets the log function for warnings and informational messages. If not set, messages are silently discarded.
func (*DB) Tx ¶
Tx executes a function within a transaction. The underlying storage.Conn must implement storage.TxExecutor (type-asserted here) — most backends do; mem.New() also implements it as a no-op so tests can exercise this path without a real transactional backend.
func (*DB) Update ¶
Update modifies an existing row. At least one Condition is required. Providing zero conditions is a compile-time error — there is no variadic fallback — preventing accidental full-table UPDATE statements.
func (*DB) UpdateFields ¶ added in v0.12.0
func (d *DB) UpdateFields(m model.Model, fields []string, cond storage.Condition, rest ...storage.Condition) error
UpdateFields updates ONLY the named columns, leaving every other column of the matched rows untouched. It is the PATCH counterpart of Update, which writes the whole schema and therefore overwrites columns the caller never meant to touch — a lost update whenever anyone else changed one of them in the meantime.
fields holds Schema() field names. Order is irrelevant; duplicates are rejected. An empty fields slice is an error, not a silent no-op: a caller that computed an empty change set has a bug, and a no-op UPDATE would hide it.
At least one Condition is required, same as Update — there is no variadic fallback, which is what makes an accidental whole-table UPDATE a compile-time error rather than a production incident.
type OrderClause ¶ added in v0.0.8
type OrderClause struct {
// contains filtered or unexported fields
}
OrderClause represents an intermediate state for building an order by clause.
func (*OrderClause) Asc ¶ added in v0.0.8
func (o *OrderClause) Asc() *QB
Asc sets the order direction to ascending.
func (*OrderClause) Desc ¶ added in v0.0.8
func (o *OrderClause) Desc() *QB
Desc sets the order direction to descending.
type QB ¶
type QB struct {
// contains filtered or unexported fields
}
QB represents a query builder. Consumers hold a *QB reference in variables for incremental building.
func (*QB) OrderBy ¶
func (qb *QB) OrderBy(column string) *OrderClause
OrderBy starts a new order clause for the given column.