summaryrefslogtreecommitdiff
path: root/gsql
diff options
context:
space:
mode:
Diffstat (limited to 'gsql')
-rw-r--r--gsql/gsql.go18
-rw-r--r--gsql/std.go2
2 files changed, 18 insertions, 2 deletions
diff --git a/gsql/gsql.go b/gsql/gsql.go
index e7e2aa3..6e1cc97 100644
--- a/gsql/gsql.go
+++ b/gsql/gsql.go
@@ -8,7 +8,7 @@
// A [Handle] implementation for use with [pgx] is provided in [gg-pgx].
//
// [pgx]: https://github.com/jackc/pgx
-// [gg-pgx]: https://git.xyrillian.de/go-gg-pgx/
+// [gg-pgx]: https://pkg.go.dev/go.xyrillian.de/gg-pgx
package gsql
import (
@@ -16,6 +16,7 @@ import (
"database/sql"
"errors"
+ "go.xyrillian.de/gg/errext"
. "go.xyrillian.de/gg/option"
)
@@ -94,3 +95,18 @@ func NoneIfNoRows[T any](value T, err error) (Option[T], error) {
return None[T](), err
}
}
+
+// Exec is a shorthand for preparing and executing a one-off query that returns an [sql.Result].
+//
+// For shorthands involving [Statement.QueryRow] instead of [Statement.Exec],
+// look at Select() and SelectOne() in [gg/oblast].
+//
+// [gg/oblast]: https://pkg.go.dev/go.xyrillian.de/gg/oblast
+func Exec(ctx context.Context, db Handle, query string, args ...any) (sql.Result, error) {
+ stmt, err := db.GSQLPrepare(ctx, query, false)
+ if err != nil {
+ return nil, err
+ }
+ result, err := stmt.Exec(ctx, args)
+ return result, errext.WithCleanup(err, "Statement.Close", stmt.Close())
+}
diff --git a/gsql/std.go b/gsql/std.go
index bf4c718..94dd38f 100644
--- a/gsql/std.go
+++ b/gsql/std.go
@@ -18,7 +18,7 @@ import (
// of type [Handle] would allocate a new fat pointer when converting from e.g. [*DB]
// at the callsite to [Handle] in the argument value.
//
-// To circumvent this, our types only _have_ [Handle] instances within them within them
+// To circumvent this, our types only _have_ [Handle] instances within them
// as an embedded field, thus implementing [Handle] indirectly instead of directly.
// DB wraps [*sql.DB] into a [Handle].