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>
This commit is contained in:
@@ -70,3 +70,31 @@ func MustFetchOneContext[T any](ctx context.Context, sqlo Queryable, stmt Statem
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user