feat: add delete, context, and tx helpers

Add Delete/DeleteAffected, context-aware CRUD helpers, WithTx, ContainsCol,
and queryReturning deduplication for returning statements.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-06-29 17:49:57 -07:00
parent 8053aafa6f
commit 8d697eda5b
11 changed files with 517 additions and 23 deletions
+28 -5
View File
@@ -1,12 +1,20 @@
package dbx
import "errors"
import (
"context"
"errors"
)
// Fetch queries the database and returns the result as a slice. If the query
// returns no rows, it returns an empty slice and no error.
func Fetch[T any](sqlo Queryable, stmt Statement) ([]*T, error) {
return FetchContext[T](context.Background(), sqlo, stmt)
}
// FetchContext is the context-aware variant of [Fetch].
func FetchContext[T any](ctx context.Context, sqlo Queryable, stmt Statement) ([]*T, error) {
var result []*T
if err := stmt.Query(sqlo, &result); err != nil && !errors.Is(err, ErrNoRows) {
if err := stmt.QueryContext(ctx, sqlo, &result); err != nil && !errors.Is(err, ErrNoRows) {
return nil, err
}
return result, nil
@@ -15,7 +23,12 @@ func Fetch[T any](sqlo Queryable, stmt Statement) ([]*T, error) {
// MustFetch queries the database and returns the result as a slice. If the query
// returns no rows, it returns an empty slice and the desired error.
func MustFetch[T any](sqlo Queryable, stmt Statement, notFoundErr error) ([]*T, error) {
result, err := Fetch[T](sqlo, stmt)
return MustFetchContext[T](context.Background(), sqlo, stmt, notFoundErr)
}
// MustFetchContext is the context-aware variant of [MustFetch].
func MustFetchContext[T any](ctx context.Context, sqlo Queryable, stmt Statement, notFoundErr error) ([]*T, error) {
result, err := FetchContext[T](ctx, sqlo, stmt)
if err != nil {
return nil, err
}
@@ -28,7 +41,12 @@ func MustFetch[T any](sqlo Queryable, stmt Statement, notFoundErr error) ([]*T,
// FetchOne queries the database and returns a single result. If the query
// returns no rows, it returns nil and no error.
func FetchOne[T any](sqlo Queryable, stmt Statement) (*T, error) {
result, err := Fetch[T](sqlo, stmt)
return FetchOneContext[T](context.Background(), sqlo, stmt)
}
// FetchOneContext is the context-aware variant of [FetchOne].
func FetchOneContext[T any](ctx context.Context, sqlo Queryable, stmt Statement) (*T, error) {
result, err := FetchContext[T](ctx, sqlo, stmt)
if err != nil {
return nil, err
}
@@ -41,7 +59,12 @@ func FetchOne[T any](sqlo Queryable, stmt Statement) (*T, error) {
// MustFetchOne queries the database and returns a single result. If the query
// returns no rows, it returns nil and the desired error.
func MustFetchOne[T any](sqlo Queryable, stmt Statement, notFoundErr error) (*T, error) {
result, err := MustFetch[T](sqlo, stmt, notFoundErr)
return MustFetchOneContext[T](context.Background(), sqlo, stmt, notFoundErr)
}
// MustFetchOneContext is the context-aware variant of [MustFetchOne].
func MustFetchOneContext[T any](ctx context.Context, sqlo Queryable, stmt Statement, notFoundErr error) (*T, error) {
result, err := MustFetchContext[T](ctx, sqlo, stmt, notFoundErr)
if err != nil {
return nil, err
}