Files
dbx/exec.go
T
end fbb9c74aab feat(query): add Query, MustQuery, and UpdateOne helpers
Adds scan-into-dest query helpers and update-one-row semantics with
Context variants, tests, and documentation updates.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-29 18:24:29 -07:00

146 lines
4.6 KiB
Go

package dbx
import (
"context"
"errors"
)
// Insert executes an insert statement, returning the last inserted ID or an
// error if the insert fails.
func Insert(sqlo Executable, stmt Statement) (uint64, error) {
return InsertContext(context.Background(), sqlo, stmt)
}
// InsertContext is the context-aware variant of [Insert].
func InsertContext(ctx context.Context, sqlo Executable, stmt Statement) (uint64, error) {
res, err := stmt.ExecContext(ctx, sqlo)
if err != nil {
return 0, err
}
id, err := res.LastInsertId()
if err != nil {
return 0, err
}
if id < 1 {
return 0, errors.New("inserted ID is less than 1")
}
return uint64(id), nil
}
// InsertReturning executes an insert statement that returns the inserted row.
// The statement MUST be a Jet InsertStatement with a RETURNING clause. Returns
// the inserted row object T or an error if the insert fails or no rows are returned.
func InsertReturning[T any](sqlo Queryable, stmt Statement) (*T, error) {
return InsertReturningContext[T](context.Background(), sqlo, stmt)
}
// InsertReturningContext is the context-aware variant of [InsertReturning].
func InsertReturningContext[T any](ctx context.Context, sqlo Queryable, stmt Statement) (*T, error) {
return queryReturningContext[T](ctx, sqlo, stmt)
}
// Update executes an update statement, returning an error if the update fails.
func Update(sqlo Executable, stmt Statement) error {
return UpdateContext(context.Background(), sqlo, stmt)
}
// UpdateContext is the context-aware variant of [Update].
func UpdateContext(ctx context.Context, sqlo Executable, stmt Statement) error {
_, err := stmt.ExecContext(ctx, sqlo)
return err
}
// UpdateAffected executes an update statement and returns the number of rows
// affected and an error if any.
func UpdateAffected(sqlo Executable, stmt Statement) (int64, error) {
return UpdateAffectedContext(context.Background(), sqlo, stmt)
}
// UpdateAffectedContext is the context-aware variant of [UpdateAffected].
func UpdateAffectedContext(ctx context.Context, sqlo Executable, stmt Statement) (int64, error) {
res, err := stmt.ExecContext(ctx, sqlo)
if err != nil {
return 0, err
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return 0, err
}
return rowsAffected, nil
}
// UpdateOne executes an update statement and returns notFoundErr if zero rows
// were affected.
func UpdateOne(sqlo Executable, stmt Statement, notFoundErr error) error {
return UpdateOneContext(context.Background(), sqlo, stmt, notFoundErr)
}
// UpdateOneContext is the context-aware variant of [UpdateOne].
func UpdateOneContext(ctx context.Context, sqlo Executable, stmt Statement, notFoundErr error) error {
n, err := UpdateAffectedContext(ctx, sqlo, stmt)
if err != nil {
return err
}
if n == 0 {
return notFoundErr
}
return nil
}
// UpdateReturning executes an update statement that returns the updated row.
// The statement MUST be a Jet UpdateStatement with a RETURNING clause. Returns
// the updated row object T or an error if the update fails or no rows are returned.
func UpdateReturning[T any](sqlo Queryable, stmt Statement) (*T, error) {
return UpdateReturningContext[T](context.Background(), sqlo, stmt)
}
// UpdateReturningContext is the context-aware variant of [UpdateReturning].
func UpdateReturningContext[T any](ctx context.Context, sqlo Queryable, stmt Statement) (*T, error) {
return queryReturningContext[T](ctx, sqlo, stmt)
}
// Delete executes a delete statement, returning an error if the delete fails.
func Delete(sqlo Executable, stmt Statement) error {
return DeleteContext(context.Background(), sqlo, stmt)
}
// DeleteContext is the context-aware variant of [Delete].
func DeleteContext(ctx context.Context, sqlo Executable, stmt Statement) error {
_, err := stmt.ExecContext(ctx, sqlo)
return err
}
// DeleteAffected executes a delete statement and returns the number of rows
// affected and an error if any.
func DeleteAffected(sqlo Executable, stmt Statement) (int64, error) {
return DeleteAffectedContext(context.Background(), sqlo, stmt)
}
// DeleteAffectedContext is the context-aware variant of [DeleteAffected].
func DeleteAffectedContext(ctx context.Context, sqlo Executable, stmt Statement) (int64, error) {
res, err := stmt.ExecContext(ctx, sqlo)
if err != nil {
return 0, err
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return 0, err
}
return rowsAffected, nil
}
func queryReturningContext[T any](ctx context.Context, sqlo Queryable, stmt Statement) (*T, error) {
var result T
if err := stmt.QueryContext(ctx, sqlo, &result); err != nil {
return nil, err
}
return &result, nil
}