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:
@@ -1,11 +1,19 @@
|
||||
package dbx
|
||||
|
||||
import "errors"
|
||||
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) {
|
||||
res, err := stmt.Exec(sqlo)
|
||||
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
|
||||
}
|
||||
@@ -26,24 +34,34 @@ func Insert(sqlo Executable, stmt Statement) (uint64, error) {
|
||||
// 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) {
|
||||
var result T
|
||||
err := stmt.Query(sqlo, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
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 {
|
||||
_, err := stmt.Exec(sqlo)
|
||||
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) {
|
||||
res, err := stmt.Exec(sqlo)
|
||||
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
|
||||
}
|
||||
@@ -60,9 +78,49 @@ func UpdateAffected(sqlo Executable, stmt Statement) (int64, error) {
|
||||
// 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) {
|
||||
var result T
|
||||
err := stmt.Query(sqlo, &result)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user