diff options
| -rw-r--r-- | CHANGELOG.md | 1 | ||||
| -rw-r--r-- | gsql/gsql.go | 18 | ||||
| -rw-r--r-- | gsql/std.go | 2 | ||||
| -rw-r--r-- | pgruntime/behavior.go | 8 | ||||
| -rw-r--r-- | pgruntime/connector.go | 4 | ||||
| -rw-r--r-- | pgruntime/helpers.go | 16 | ||||
| -rw-r--r-- | testing/pgruntime/connect_test.go | 3 | ||||
| -rw-r--r-- | testing/pgruntime/helpers_test.go | 23 | ||||
| -rw-r--r-- | testing/pgruntime/migration_test.go | 5 |
9 files changed, 30 insertions, 50 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 9494b6e..3027ab3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Changes: have been replaced with implementations of encoding/json/v2.MarshalerTo and encoding/json/v2.UnmarshalerFrom. This is technically a backwards-incompatibility for applications that call these methods directly, but all actual users call these functions indirectly through encoding/json or encoding/json/v2. +- gsql: Add Exec(). # v1.19.0 (2026-10-06) 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]. diff --git a/pgruntime/behavior.go b/pgruntime/behavior.go index d327d09..76d8bd0 100644 --- a/pgruntime/behavior.go +++ b/pgruntime/behavior.go @@ -63,7 +63,7 @@ func (b ConnectionBehavior) applyTo(ctx context.Context, db gsql.ConnectionHandl func applyMigrations(ctx context.Context, db gsql.ConnectionHandle, migrations map[int64]string) error { // apply schema_migrations table schema - _, err := execQuery(ctx, db, MigrationsSchema, nil) + _, err := gsql.Exec(ctx, db, MigrationsSchema) if err != nil { return fmt.Errorf("could not apply schema_migrations table schema: %w", err) } @@ -81,7 +81,7 @@ func applyMigrations(ctx context.Context, db gsql.ConnectionHandle, migrations m switch rowCount { case 0: record.CurrentVersion = 0 - _, err = execQuery(ctx, db, `INSERT INTO schema_migrations (version, dirty) VALUES (0, FALSE)`, nil) + _, err = gsql.Exec(ctx, db, `INSERT INTO schema_migrations (version, dirty) VALUES (0, FALSE)`) if err != nil { return fmt.Errorf("could not initialize schema_migrations record: %w", err) } @@ -126,11 +126,11 @@ func applyMigrations(ctx context.Context, db gsql.ConnectionHandle, migrations m } // perform the next migration - _, err = execQuery(ctx, db, migrations[version], nil) + _, err = gsql.Exec(ctx, db, migrations[version]) if err != nil { return fmt.Errorf("could not execute schema migration: %w", err) } - _, err = execQuery(ctx, db, `UPDATE schema_migrations SET version = $1, dirty = FALSE`, []any{version}) + _, err = gsql.Exec(ctx, db, `UPDATE schema_migrations SET version = $1, dirty = FALSE`, version) if err != nil { return fmt.Errorf("could not update schema_migrations record: %w", err) } diff --git a/pgruntime/connector.go b/pgruntime/connector.go index 3a41548..0d79bf1 100644 --- a/pgruntime/connector.go +++ b/pgruntime/connector.go @@ -143,7 +143,7 @@ func createDatabaseIfMissing(ctx context.Context, db gsql.Handle, dbName string) // create database if necessary if !exists { - _, err = execQuery(ctx, db, "CREATE DATABASE "+quoteIdentifier(dbName), nil) + _, err = gsql.Exec(ctx, db, "CREATE DATABASE "+quoteIdentifier(dbName)) if err != nil { return fmt.Errorf("during CREATE DATABASE: %w", err) } @@ -167,7 +167,7 @@ func resetTestDatabase(ctx context.Context, db gsql.Handle, params testSetupPara // truncate all tables at once if len(quotedTableNames) > 0 { query = fmt.Sprintf(`TRUNCATE %s RESTART IDENTITY CASCADE`, strings.Join(quotedTableNames, ", ")) - _, err = execQuery(ctx, db, query, nil) + _, err = gsql.Exec(ctx, db, query) if err != nil { return fmt.Errorf("during %s: %w", query, err) } diff --git a/pgruntime/helpers.go b/pgruntime/helpers.go index 1b9e11f..d68b920 100644 --- a/pgruntime/helpers.go +++ b/pgruntime/helpers.go @@ -4,25 +4,9 @@ package pgruntime import ( - "context" - "database/sql" "strings" - - "go.xyrillian.de/gg/errext" - "go.xyrillian.de/gg/gsql" ) -// Convenience function for executing a one-off SQL query returning no rows. -// TODO: move to gsql -func execQuery(ctx context.Context, db gsql.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, "stmt.Close", stmt.Close()) -} - // Convenience function for preparing an identifier that needs to be inserted into a query verbatim // (e.g. a database name for CREATE DATABASE). func quoteIdentifier(name string) string { diff --git a/testing/pgruntime/connect_test.go b/testing/pgruntime/connect_test.go index f43c6f8..d821fb0 100644 --- a/testing/pgruntime/connect_test.go +++ b/testing/pgruntime/connect_test.go @@ -7,6 +7,7 @@ import ( "testing" "go.xyrillian.de/gg/assert" + "go.xyrillian.de/gg/gsql" "go.xyrillian.de/gg/oblast" ) @@ -21,7 +22,7 @@ func TestMultipleConnectionsToSameDB(t *testing.T) { `INSERT INTO objects (name) VALUES ('foo')`, // -> id = 1 `INSERT INTO objects (name) VALUES ('bar')`, // -> id = 2 } { - _, err := execQuery(ctx, db1, query, nil) + _, err := gsql.Exec(ctx, db1, query) if err != nil { t.Fatalf("in query %q: %s", query, err.Error()) } diff --git a/testing/pgruntime/helpers_test.go b/testing/pgruntime/helpers_test.go deleted file mode 100644 index d8c3cc9..0000000 --- a/testing/pgruntime/helpers_test.go +++ /dev/null @@ -1,23 +0,0 @@ -// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net> -// SPDX-License-Identifier: Apache-2.0 - -package pgruntime_test - -import ( - "context" - "database/sql" - - "go.xyrillian.de/gg/errext" - "go.xyrillian.de/gg/gsql" -) - -// Convenience function for executing a one-off SQL query returning no rows. -// TODO: move to gsql -func execQuery(ctx context.Context, db gsql.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, "stmt.Close", stmt.Close()) -} diff --git a/testing/pgruntime/migration_test.go b/testing/pgruntime/migration_test.go index 6113c36..d65741a 100644 --- a/testing/pgruntime/migration_test.go +++ b/testing/pgruntime/migration_test.go @@ -7,6 +7,7 @@ import ( "testing" "go.xyrillian.de/gg/assert" + "go.xyrillian.de/gg/gsql" "go.xyrillian.de/gg/oblast" "go.xyrillian.de/gg/pgruntime" ) @@ -17,7 +18,7 @@ func TestMigrations(t *testing.T) { // reset the test DB to empty if it exists db, _ := connector.ConnectForTest(t, defaultBehavior) for _, tableName := range []string{"comments", "posts", "schema_migrations"} { - _, err := execQuery(ctx, db, `DROP TABLE IF EXISTS `+tableName, nil) + _, err := gsql.Exec(ctx, db, `DROP TABLE IF EXISTS `+tableName) if err != nil { t.Fatal(err.Error()) } @@ -50,7 +51,7 @@ func TestMigrations(t *testing.T) { if err != nil { t.Fatal(err.Error()) } - _, err = execQuery(ctx, db, `INSERT INTO comments (post_id, message) VALUES ($1, $2)`, []any{postID, "Hi there."}) + _, err = gsql.Exec(ctx, db, `INSERT INTO comments (post_id, message) VALUES ($1, $2)`, postID, "Hi there.") if err != nil { t.Fatal(err.Error()) } |
