aboutsummaryrefslogtreecommitdiff
path: root/query.go
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2026-04-11 20:19:12 +0200
committerStefan Majewsky <majewsky@gmx.net>2026-04-11 20:20:03 +0200
commite9d31443f01eda2ecee66dbc25f154a6949a9c97 (patch)
tree1824c7dc3290e4d38ab111522938e8a33e2f9618 /query.go
parent3d28ce0650fc85ca054a608bce32f88f2d90295f (diff)
downloadgo-oblast-e9d31443f01eda2ecee66dbc25f154a6949a9c97.tar.gz
reorganize the API from type DB to type Store
Diffstat (limited to 'query.go')
-rw-r--r--query.go19
1 files changed, 8 insertions, 11 deletions
diff --git a/query.go b/query.go
index a464c9e..e47feb6 100644
--- a/query.go
+++ b/query.go
@@ -11,15 +11,12 @@ import (
"go.xyrillian.de/oblast/internal"
)
-func Select[T any](db *DB, query string, args ...any) (result []T, returnedError error) {
+// TODO: allow taking *sql.Tx in addition to *sql.DB
+func (s Store[R]) Select(db *sql.DB, query string, args ...any) (result []R, returnedError error) {
// NOTE: This function body should be as short as possible to reduce the binary size after monomorphization.
- // Any expression that does not depend on type T should be factored out into a reusable function.
+ // Any expression that does not depend on type R should be factored out into a reusable function.
- plan, err := db.getPlan(reflect.TypeFor[T]())
- if err != nil {
- return nil, err
- }
- rows, indexes, err := db.startQuery(plan, query, args...)
+ rows, indexes, err := startQuery(db, s.plan, query, args...)
if err != nil {
return nil, err
}
@@ -29,8 +26,8 @@ func Select[T any](db *DB, query string, args ...any) (result []T, returnedError
slots := make([]any, len(indexes))
for rows.Next() {
- var target T
- err = db.collectRow(rows, reflect.ValueOf(&target).Elem(), slots, indexes)
+ var target R
+ err = collectRow(rows, reflect.ValueOf(&target).Elem(), slots, indexes)
if err != nil {
return nil, err
}
@@ -40,7 +37,7 @@ func Select[T any](db *DB, query string, args ...any) (result []T, returnedError
return result, nil
}
-func (db *DB) startQuery(plan internal.Plan, query string, args ...any) (rows *sql.Rows, indexes [][]int, err error) {
+func startQuery(db *sql.DB, plan internal.Plan, query string, args ...any) (rows *sql.Rows, indexes [][]int, err error) {
rows, err = db.Query(query, args...)
if err != nil {
return nil, nil, fmt.Errorf("during Query(): %w", err)
@@ -73,7 +70,7 @@ func (db *DB) startQuery(plan internal.Plan, query string, args ...any) (rows *s
return rows, indexes, nil
}
-func (db *DB) collectRow(rows *sql.Rows, v reflect.Value, slots []any, indexes [][]int) error {
+func collectRow(rows *sql.Rows, v reflect.Value, slots []any, indexes [][]int) error {
for idx, index := range indexes {
slots[idx] = v.FieldByIndex(index).Addr().Interface()
}