From c1067fed447b6c14b36e93c6cb4dac18c5fee777 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Tue, 26 Sep 2023 01:22:19 -0700 Subject: [PATCH 1/2] feat(compiler): Support DO statements --- internal/compiler/output_columns.go | 4 +++ internal/compiler/parse.go | 1 + .../testdata/do/postgresql/pgx/db/db.go | 32 +++++++++++++++++++ .../testdata/do/postgresql/pgx/db/models.go | 15 +++++++++ .../do/postgresql/pgx/db/query.sql.go | 24 ++++++++++++++ .../testdata/do/postgresql/pgx/query.sql | 7 ++++ .../testdata/do/postgresql/pgx/schema.sql | 5 +++ .../testdata/do/postgresql/pgx/sqlc.json | 12 +++++++ .../testdata/do/postgresql/pq/db/db.go | 31 ++++++++++++++++++ .../testdata/do/postgresql/pq/db/models.go | 15 +++++++++ .../testdata/do/postgresql/pq/db/query.sql.go | 29 +++++++++++++++++ .../testdata/do/postgresql/pq/query.sql | 7 ++++ .../testdata/do/postgresql/pq/schema.sql | 5 +++ .../testdata/do/postgresql/pq/sqlc.json | 11 +++++++ 14 files changed, 198 insertions(+) create mode 100644 internal/endtoend/testdata/do/postgresql/pgx/db/db.go create mode 100644 internal/endtoend/testdata/do/postgresql/pgx/db/models.go create mode 100644 internal/endtoend/testdata/do/postgresql/pgx/db/query.sql.go create mode 100644 internal/endtoend/testdata/do/postgresql/pgx/query.sql create mode 100644 internal/endtoend/testdata/do/postgresql/pgx/schema.sql create mode 100644 internal/endtoend/testdata/do/postgresql/pgx/sqlc.json create mode 100644 internal/endtoend/testdata/do/postgresql/pq/db/db.go create mode 100644 internal/endtoend/testdata/do/postgresql/pq/db/models.go create mode 100644 internal/endtoend/testdata/do/postgresql/pq/db/query.sql.go create mode 100644 internal/endtoend/testdata/do/postgresql/pq/query.sql create mode 100644 internal/endtoend/testdata/do/postgresql/pq/schema.sql create mode 100644 internal/endtoend/testdata/do/postgresql/pq/sqlc.json diff --git a/internal/compiler/output_columns.go b/internal/compiler/output_columns.go index 545869cf6a..75ff457d63 100644 --- a/internal/compiler/output_columns.go +++ b/internal/compiler/output_columns.go @@ -115,6 +115,8 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er if isUnion { return c.outputColumns(qc, n.Larg) } + case *ast.DoStmt: + targets = &ast.List{} case *ast.CallStmt: targets = &ast.List{} case *ast.TruncateStmt, *ast.RefreshMatViewStmt, *ast.NotifyStmt, *ast.ListenStmt: @@ -509,6 +511,8 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro list = &ast.List{ Items: append(n.FromClause.Items, n.Relations.Items...), } + case *ast.DoStmt: + list = &ast.List{} case *ast.CallStmt: list = &ast.List{} case *ast.NotifyStmt, *ast.ListenStmt: diff --git a/internal/compiler/parse.go b/internal/compiler/parse.go index 8354bd340a..c660ef4fb4 100644 --- a/internal/compiler/parse.go +++ b/internal/compiler/parse.go @@ -39,6 +39,7 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query, case *ast.CallStmt: case *ast.SelectStmt: case *ast.DeleteStmt: + case *ast.DoStmt: case *ast.InsertStmt: if err := validate.InsertStmt(n); err != nil { return nil, err diff --git a/internal/endtoend/testdata/do/postgresql/pgx/db/db.go b/internal/endtoend/testdata/do/postgresql/pgx/db/db.go new file mode 100644 index 0000000000..0975c670b0 --- /dev/null +++ b/internal/endtoend/testdata/do/postgresql/pgx/db/db.go @@ -0,0 +1,32 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.21.0 + +package db + +import ( + "context" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" +) + +type DBTX interface { + Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error) + Query(context.Context, string, ...interface{}) (pgx.Rows, error) + QueryRow(context.Context, string, ...interface{}) pgx.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx pgx.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/do/postgresql/pgx/db/models.go b/internal/endtoend/testdata/do/postgresql/pgx/db/models.go new file mode 100644 index 0000000000..9d556e5184 --- /dev/null +++ b/internal/endtoend/testdata/do/postgresql/pgx/db/models.go @@ -0,0 +1,15 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.21.0 + +package db + +import ( + "github.com/jackc/pgx/v5/pgtype" +) + +type Author struct { + ID int64 + Name string + Bio pgtype.Text +} diff --git a/internal/endtoend/testdata/do/postgresql/pgx/db/query.sql.go b/internal/endtoend/testdata/do/postgresql/pgx/db/query.sql.go new file mode 100644 index 0000000000..b56dac6fc7 --- /dev/null +++ b/internal/endtoend/testdata/do/postgresql/pgx/db/query.sql.go @@ -0,0 +1,24 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.21.0 +// source: query.sql + +package db + +import ( + "context" +) + +const doStuff = `-- name: DoStuff :exec +DO $$ + BEGIN + ALTER TABLE authors + ADD COLUMN marked_for_processing bool; + END +$$ +` + +func (q *Queries) DoStuff(ctx context.Context) error { + _, err := q.db.Exec(ctx, doStuff) + return err +} diff --git a/internal/endtoend/testdata/do/postgresql/pgx/query.sql b/internal/endtoend/testdata/do/postgresql/pgx/query.sql new file mode 100644 index 0000000000..26a790d341 --- /dev/null +++ b/internal/endtoend/testdata/do/postgresql/pgx/query.sql @@ -0,0 +1,7 @@ +-- name: DoStuff :exec +DO $$ + BEGIN + ALTER TABLE authors + ADD COLUMN marked_for_processing bool; + END +$$; diff --git a/internal/endtoend/testdata/do/postgresql/pgx/schema.sql b/internal/endtoend/testdata/do/postgresql/pgx/schema.sql new file mode 100644 index 0000000000..69b607d902 --- /dev/null +++ b/internal/endtoend/testdata/do/postgresql/pgx/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE authors ( + id BIGSERIAL PRIMARY KEY, + name text NOT NULL, + bio text +); diff --git a/internal/endtoend/testdata/do/postgresql/pgx/sqlc.json b/internal/endtoend/testdata/do/postgresql/pgx/sqlc.json new file mode 100644 index 0000000000..b74a2efca0 --- /dev/null +++ b/internal/endtoend/testdata/do/postgresql/pgx/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "path": "db", + "engine": "postgresql", + "schema": "schema.sql", + "queries": "query.sql", + "sql_package": "pgx/v5" + } + ] +} diff --git a/internal/endtoend/testdata/do/postgresql/pq/db/db.go b/internal/endtoend/testdata/do/postgresql/pq/db/db.go new file mode 100644 index 0000000000..46fda54404 --- /dev/null +++ b/internal/endtoend/testdata/do/postgresql/pq/db/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.21.0 + +package db + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/do/postgresql/pq/db/models.go b/internal/endtoend/testdata/do/postgresql/pq/db/models.go new file mode 100644 index 0000000000..b73fc43b09 --- /dev/null +++ b/internal/endtoend/testdata/do/postgresql/pq/db/models.go @@ -0,0 +1,15 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.21.0 + +package db + +import ( + "database/sql" +) + +type Author struct { + ID int64 + Name string + Bio sql.NullString +} diff --git a/internal/endtoend/testdata/do/postgresql/pq/db/query.sql.go b/internal/endtoend/testdata/do/postgresql/pq/db/query.sql.go new file mode 100644 index 0000000000..9c06d240ba --- /dev/null +++ b/internal/endtoend/testdata/do/postgresql/pq/db/query.sql.go @@ -0,0 +1,29 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.21.0 +// source: query.sql + +package db + +import ( + "context" +) + +const doStuff = `-- name: DoStuff :one +DO $$ + BEGIN + ALTER TABLE authors + ADD COLUMN marked_for_processing bool; + END +$$ +` + +type DoStuffRow struct { +} + +func (q *Queries) DoStuff(ctx context.Context) (DoStuffRow, error) { + row := q.db.QueryRowContext(ctx, doStuff) + var i DoStuffRow + err := row.Scan() + return i, err +} diff --git a/internal/endtoend/testdata/do/postgresql/pq/query.sql b/internal/endtoend/testdata/do/postgresql/pq/query.sql new file mode 100644 index 0000000000..26a790d341 --- /dev/null +++ b/internal/endtoend/testdata/do/postgresql/pq/query.sql @@ -0,0 +1,7 @@ +-- name: DoStuff :exec +DO $$ + BEGIN + ALTER TABLE authors + ADD COLUMN marked_for_processing bool; + END +$$; diff --git a/internal/endtoend/testdata/do/postgresql/pq/schema.sql b/internal/endtoend/testdata/do/postgresql/pq/schema.sql new file mode 100644 index 0000000000..69b607d902 --- /dev/null +++ b/internal/endtoend/testdata/do/postgresql/pq/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE authors ( + id BIGSERIAL PRIMARY KEY, + name text NOT NULL, + bio text +); diff --git a/internal/endtoend/testdata/do/postgresql/pq/sqlc.json b/internal/endtoend/testdata/do/postgresql/pq/sqlc.json new file mode 100644 index 0000000000..2a46100899 --- /dev/null +++ b/internal/endtoend/testdata/do/postgresql/pq/sqlc.json @@ -0,0 +1,11 @@ +{ + "version": "1", + "packages": [ + { + "path": "db", + "engine": "postgresql", + "schema": "schema.sql", + "queries": "query.sql" + } + ] +} From da19ae034196466cbc9824d3a21ba891c62015f6 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Tue, 26 Sep 2023 02:12:44 -0700 Subject: [PATCH 2/2] fix exec --- .../testdata/do/postgresql/pq/db/query.sql.go | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/internal/endtoend/testdata/do/postgresql/pq/db/query.sql.go b/internal/endtoend/testdata/do/postgresql/pq/db/query.sql.go index 9c06d240ba..3da76a3e74 100644 --- a/internal/endtoend/testdata/do/postgresql/pq/db/query.sql.go +++ b/internal/endtoend/testdata/do/postgresql/pq/db/query.sql.go @@ -9,7 +9,7 @@ import ( "context" ) -const doStuff = `-- name: DoStuff :one +const doStuff = `-- name: DoStuff :exec DO $$ BEGIN ALTER TABLE authors @@ -18,12 +18,7 @@ DO $$ $$ ` -type DoStuffRow struct { -} - -func (q *Queries) DoStuff(ctx context.Context) (DoStuffRow, error) { - row := q.db.QueryRowContext(ctx, doStuff) - var i DoStuffRow - err := row.Scan() - return i, err +func (q *Queries) DoStuff(ctx context.Context) error { + _, err := q.db.ExecContext(ctx, doStuff) + return err }