package dbx 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.QueryContext(ctx, sqlo, &result); err != nil && !errors.Is(err, ErrNoRows) { return nil, err } return result, nil } // 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) { 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 } if len(result) == 0 { return nil, notFoundErr } return result, nil } // 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) { 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 } if len(result) == 0 { return nil, nil } return result[0], nil } // 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) { 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 } return result[0], nil } // Query executes a Jet statement and scans the result into dest. func Query(sqlo Queryable, stmt Statement, dest any) error { return QueryContext(context.Background(), sqlo, stmt, dest) } // QueryContext is the context-aware variant of [Query]. func QueryContext(ctx context.Context, sqlo Queryable, stmt Statement, dest any) error { return stmt.QueryContext(ctx, sqlo, dest) } // MustQuery executes a Jet statement and scans the result into dest. If the // query returns no rows, notFoundErr is returned instead of [ErrNoRows]. func MustQuery(sqlo Queryable, stmt Statement, dest any, notFoundErr error) error { return MustQueryContext(context.Background(), sqlo, stmt, dest, notFoundErr) } // MustQueryContext is the context-aware variant of [MustQuery]. func MustQueryContext(ctx context.Context, sqlo Queryable, stmt Statement, dest any, notFoundErr error) error { err := stmt.QueryContext(ctx, sqlo, dest) if err != nil { if errors.Is(err, ErrNoRows) { return notFoundErr } return err } return nil }