aboutsummaryrefslogtreecommitdiff
path: root/select.go
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2026-05-12 13:11:41 +0200
committerStefan Majewsky <majewsky@gmx.net>2026-05-12 13:11:41 +0200
commit7bdc14a145c4b5c380921abf1db3f2c00a9c66cc (patch)
treed08f4436739456ab015598eb9f02047208f981de /select.go
parentd5e652b41e68538fc7a7d8f67c3b8dfe3129a464 (diff)
downloadgo-oblast-7bdc14a145c4b5c380921abf1db3f2c00a9c66cc.tar.gz
change Handle to a generic interface without explicit dep on database/sql
Diffstat (limited to 'select.go')
-rw-r--r--select.go18
1 files changed, 12 insertions, 6 deletions
diff --git a/select.go b/select.go
index fc98fe7..87dd36b 100644
--- a/select.go
+++ b/select.go
@@ -11,6 +11,7 @@ import (
"reflect"
. "go.xyrillian.de/gg/option"
+ "go.xyrillian.de/oblast/handle"
)
// Select executes the provided SQL query and fills an instance of the record type R for each row in the result set,
@@ -103,8 +104,8 @@ func (s Store[R]) SelectWhere(ctx context.Context, db Handle, partialQuery strin
return result, newIOError(err, "Rows.Err", rows.Err())
}
-func startSelectQuery(ctx context.Context, db Handle, plan plan, query string, args ...any) (*sql.Rows, [][]int, error) {
- rows, err := db.QueryContext(ctx, query, args...)
+func startSelectQuery(ctx context.Context, db Handle, plan plan, query string, args ...any) (handle.Rows, [][]int, error) {
+ rows, err := db.Query(ctx, query, args)
if err != nil {
return nil, nil, fmt.Errorf("during Query(): %w", err)
}
@@ -130,19 +131,19 @@ func startSelectQuery(ctx context.Context, db Handle, plan plan, query string, a
return rows, indexes, nil
}
-func startSelectWhereQuery(ctx context.Context, db Handle, plan plan, partialQuery string, args ...any) (rows *sql.Rows, indexes [][]int, err error) {
+func startSelectWhereQuery(ctx context.Context, db Handle, plan plan, partialQuery string, args ...any) (rows handle.Rows, indexes [][]int, err error) {
if plan.Select.Query == "" {
return nil, nil, errors.New("cannot execute SelectWhere() because query could not be autogenerated")
}
query := plan.Select.Query + partialQuery
- rows, err = db.QueryContext(ctx, query, args...)
+ rows, err = db.Query(ctx, query, args)
if err != nil {
err = fmt.Errorf("during Query(): %w", err)
}
return rows, plan.Select.ScanIndexes, err
}
-func collectRow(rows *sql.Rows, plan plan, v reflect.Value, slots []any, indexes [][]int) error {
+func collectRow(rows handle.Rows, plan plan, v reflect.Value, slots []any, indexes [][]int) error {
for _, index := range plan.IndexesOfTransparentPointerStructs {
f := v.FieldByIndex(index)
f.Set(reflect.New(f.Type().Elem()))
@@ -239,7 +240,12 @@ func selectOne(ctx context.Context, db Handle, plan plan, v reflect.Value, query
for idx, index := range plan.Select.ScanIndexes {
slots[idx] = v.FieldByIndex(index).Addr().Interface()
}
- return db.QueryRowContext(ctx, query, args...).Scan(slots...)
+ stmt, err := db.Prepare(ctx, query, false)
+ if err != nil {
+ return err
+ }
+ err = stmt.QueryRow(ctx, args, slots)
+ return newIOError(err, "Stmt.Close", stmt.Close())
}
func noRowsToNone[R any](record R, err error) (Option[R], error) {