aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md7
-rw-r--r--gsql/gsql.go32
-rw-r--r--oblast/query.go4
-rw-r--r--oblast/select.go32
4 files changed, 65 insertions, 10 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 10b1f3c..d509df7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,13 @@ SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net>
SPDX-License-Identifier: Apache-2.0
-->
+# v1.22.0 (TBD)
+
+Changes:
+
+- gsql: Add func WithPreparedStatement.
+- oblast: Add func SelectTwo, SelectThree.
+
# v1.21.0 (2026-11-10)
Changes:
diff --git a/gsql/gsql.go b/gsql/gsql.go
index 6e1cc97..23068fd 100644
--- a/gsql/gsql.go
+++ b/gsql/gsql.go
@@ -55,7 +55,7 @@ type Handle interface {
}
// Statement represents a prepared statement returned from the GSQLPrepare() method of [Handle].
-// The Exec and QueryRow methods shall work similarly to the respective functions on [*sql.Tx], as indicated in the comments.
+// The Exec and QueryRow methods shall work similarly to the respective functions on [*sql.Stmt], as indicated in the comments.
//
// You will not need to interact with this type except when implementing your own [Handle].
type Statement interface {
@@ -65,7 +65,7 @@ type Statement interface {
// The returned Result object must remain usable after Close() is called on this Statement instance.
Exec(ctx context.Context, args []any) (sql.Result, error)
- // QueryRow works like stmt.QueryRow(ctx, args...).Scan(slots...).
+ // QueryRow works like stmt.QueryRowContext(ctx, args...).Scan(slots...).
QueryRow(ctx context.Context, args []any, slots []any) error
}
@@ -98,6 +98,8 @@ func NoneIfNoRows[T any](value T, err error) (Option[T], error) {
// Exec is a shorthand for preparing and executing a one-off query that returns an [sql.Result].
//
+// For executing the same query multiple times, see [WithPreparedStatement].
+//
// For shorthands involving [Statement.QueryRow] instead of [Statement.Exec],
// look at Select() and SelectOne() in [gg/oblast].
//
@@ -110,3 +112,29 @@ func Exec(ctx context.Context, db Handle, query string, args ...any) (sql.Result
result, err := stmt.Exec(ctx, args)
return result, errext.WithCleanup(err, "Statement.Close", stmt.Close())
}
+
+// WithPreparedStatement is a shorthand for preparing a query to be executed multiple times.
+// The provided action callback will be executed with access to the [PreparedStatement],
+// which will automatically be closed once the callback returns.
+//
+// For executing a query just once, see [Exec].
+func WithPreparedStatement(ctx context.Context, db Handle, query string, action func(stmt PreparedStatement) error) error {
+ stmt, err := db.GSQLPrepare(ctx, query, true)
+ if err != nil {
+ return err
+ }
+ err = action(PreparedStatement{stmt})
+ return errext.WithCleanup(err, "Statement.Close", stmt.Close())
+}
+
+// PreparedStatement is made available to actions executed within [WithPreparedStatement];
+// see documentation over there for details.
+type PreparedStatement struct {
+ stmt Statement
+}
+
+// Exec is like [Statement.Exec], but takes args as a variadic parameter for ergonomic reasons.
+func (p PreparedStatement) Exec(ctx context.Context, args ...any) (sql.Result, error) {
+ // TODO(v2): change Statement.Exec to variadic and remove this type, using Statement directly instead
+ return p.stmt.Exec(ctx, args)
+}
diff --git a/oblast/query.go b/oblast/query.go
index 6e375e4..d112735 100644
--- a/oblast/query.go
+++ b/oblast/query.go
@@ -24,10 +24,10 @@ import (
// Our benchmarking with the mattn/go-sqlite3 driver (and last checked with Go 1.26.2 on x86_64) indicates that this becomes a worthwhile investment at 8 or more records, so this is our default.
// If your benchmarking indicates a different tradeoff depending on your choice of Go version or SQL driver, you may adjust this variable accordingly.
//
-// The actual effect of this setting is to control the value of the "repeated" argument in [Handle.Prepare].
+// The actual effect of this setting is to control the value of the "repeated" argument in [gsql.Handle.Prepare].
var PrepareThreshold int = 8
-// prepare behaves like [Handle.Prepare].
+// prepare behaves like [gsql.Handle.Prepare].
func prepare(ctx context.Context, db gsql.Handle, readOnly bool, query, operation string, inputSize int) (gsql.Statement, error) {
if query == "" {
if readOnly {
diff --git a/oblast/select.go b/oblast/select.go
index a0d10d5..cf21c9f 100644
--- a/oblast/select.go
+++ b/oblast/select.go
@@ -287,21 +287,41 @@ func startSelectValueQuery(ctx context.Context, db gsql.Handle, query string, ar
// or whatever the equivalent for the DB handle in question is.
//
// If there are no rows in the result set, [sql.ErrNoRows] is returned.
-func SelectOne[T any](ctx context.Context, db gsql.Handle, query string, args ...any) (T, error) {
+//
+// There are also [SelectTwo] or [SelectThree] for ad-hoc queries returning more than one value.
+// Beyond two or three return values, it is usually more readable to declare an (ad-hoc) struct type and use either [Store.SelectOne] or [TupleSelectOne].
+func SelectOne[T any](ctx context.Context, db gsql.Handle, query string, args ...any) (result T, err 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 R should be factored out into a reusable function.
- var result T
- err := selectOneValue(ctx, db, &result, query, args)
- return result, err
+ err = selectOneRowOfValues(ctx, db, []any{&result}, query, args)
+ return
+}
+
+// SelectTwo is like [SelectOne], but for queries returning exactly one row containing exactly *two* values.
+func SelectTwo[T, U any](ctx context.Context, db gsql.Handle, query string, args ...any) (result1 T, result2 U, err 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 R should be factored out into a reusable function.
+
+ err = selectOneRowOfValues(ctx, db, []any{&result1, &result2}, query, args)
+ return
+}
+
+// SelectThree is like [SelectOne], but for queries returning exactly one row containing exactly *three* values.
+func SelectThree[T, U, V any](ctx context.Context, db gsql.Handle, query string, args ...any) (result1 T, result2 U, result3 V, err 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 R should be factored out into a reusable function.
+
+ err = selectOneRowOfValues(ctx, db, []any{&result1, &result2, &result3}, query, args)
+ return
}
-func selectOneValue(ctx context.Context, db gsql.Handle, target any, query string, args []any) error {
+func selectOneRowOfValues(ctx context.Context, db gsql.Handle, targets []any, query string, args []any) error {
stmt, err := db.GSQLPrepare(ctx, query, false)
if err != nil {
return err
}
- err = stmt.QueryRow(ctx, args, []any{target})
+ err = stmt.QueryRow(ctx, args, targets)
return errext.WithCleanup(err, "Stmt.Close", stmt.Close())
}