8d697eda5b
Add Delete/DeleteAffected, context-aware CRUD helpers, WithTx, ContainsCol, and queryReturning deduplication for returning statements. Co-authored-by: Cursor <cursoragent@cursor.com>
128 lines
4.0 KiB
Go
128 lines
4.0 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
|
|
}
|
|
|
|
// 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
|
|
}
|