diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2026-11-16 19:24:27 +0100 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2026-11-16 19:24:27 +0100 |
| commit | 5856700e36d9102a0762602024f7671c47c9a5f7 (patch) | |
| tree | a9b162a8c2278ff466cd97c6bfd1508a45a493a5 | |
| parent | 0aaaa36644ec4671e20a93fb12939612410c802e (diff) | |
| download | go-gg-main.tar.gz | |
It's good that I slept on this. These names are inconsistent with the
existing pairing of oblast.Select and oblast.SelectOne.
If anything, they should be called oblast.SelectOne{Pair,Triple},
which reveals the concept to be as ugly as it really is. At this point,
I'd rather use oblast.TupleSelectOne consistently, despite is slight
ugliness from forcing either a separate type declaration or newlines in
the type argument.
| -rw-r--r-- | CHANGELOG.md | 1 | ||||
| -rw-r--r-- | oblast/select.go | 32 |
2 files changed, 6 insertions, 27 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d509df7..e75585e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,6 @@ SPDX-License-Identifier: Apache-2.0 Changes: - gsql: Add func WithPreparedStatement. -- oblast: Add func SelectTwo, SelectThree. # v1.21.0 (2026-11-10) diff --git a/oblast/select.go b/oblast/select.go index cf21c9f..a0d10d5 100644 --- a/oblast/select.go +++ b/oblast/select.go @@ -287,41 +287,21 @@ 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. -// -// 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. - - 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) { +func SelectOne[T any](ctx context.Context, db gsql.Handle, query string, args ...any) (T, 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 + var result T + err := selectOneValue(ctx, db, &result, query, args) + return result, err } -func selectOneRowOfValues(ctx context.Context, db gsql.Handle, targets []any, query string, args []any) error { +func selectOneValue(ctx context.Context, db gsql.Handle, target any, query string, args []any) error { stmt, err := db.GSQLPrepare(ctx, query, false) if err != nil { return err } - err = stmt.QueryRow(ctx, args, targets) + err = stmt.QueryRow(ctx, args, []any{target}) return errext.WithCleanup(err, "Stmt.Close", stmt.Close()) } |
