From f945922f139e7cd8eca19f56719841aa21e19d7c Mon Sep 17 00:00:00 2001 From: Nick Glynn Date: Wed, 1 Oct 2025 15:56:01 +1000 Subject: [PATCH 1/2] Fix up space before parens --- .../sqlc_embed/postgresql/pgx/go/query.sql.go | 72 ++++++++++++++++--- .../sqlc_embed/postgresql/pgx/query.sql | 5 +- internal/sql/rewrite/embeds.go | 26 +++++-- 3 files changed, 85 insertions(+), 18 deletions(-) diff --git a/internal/endtoend/testdata/sqlc_embed/postgresql/pgx/go/query.sql.go b/internal/endtoend/testdata/sqlc_embed/postgresql/pgx/go/query.sql.go index 643d5d41d6..c84a283d59 100644 --- a/internal/endtoend/testdata/sqlc_embed/postgresql/pgx/go/query.sql.go +++ b/internal/endtoend/testdata/sqlc_embed/postgresql/pgx/go/query.sql.go @@ -11,7 +11,11 @@ import ( ) const duplicate = `-- name: Duplicate :one -SELECT users.id, users.name, users.age, users.id, users.name, users.age FROM users +SELECT + users.id, users.name, users.age, + users.id, users.name, users.age +FROM + users ` type DuplicateRow struct { @@ -34,8 +38,12 @@ func (q *Queries) Duplicate(ctx context.Context) (DuplicateRow, error) { } const join = `-- name: Join :one -SELECT users.id, users.name, users.age, posts.id, posts.user_id FROM posts -INNER JOIN users ON posts.user_id = users.id +SELECT + users.id, users.name, users.age, + posts.id, posts.user_id +FROM + posts + INNER JOIN users ON posts.user_id = users.id ` type JoinRow struct { @@ -57,7 +65,10 @@ func (q *Queries) Join(ctx context.Context) (JoinRow, error) { } const only = `-- name: Only :one -SELECT users.id, users.name, users.age FROM users +SELECT + users.id, users.name, users.age +FROM + users ` type OnlyRow struct { @@ -72,7 +83,10 @@ func (q *Queries) Only(ctx context.Context) (OnlyRow, error) { } const withAlias = `-- name: WithAlias :one -SELECT u.id, u.name, u.age FROM users u +SELECT + u.id, u.name, u.age +FROM + users u ` type WithAliasRow struct { @@ -87,7 +101,11 @@ func (q *Queries) WithAlias(ctx context.Context) (WithAliasRow, error) { } const withAsterisk = `-- name: WithAsterisk :one -SELECT users.id, users.name, users.age, id, name, age FROM users +SELECT + users.id, users.name, users.age, + id, name, age +FROM + users ` type WithAsteriskRow struct { @@ -112,8 +130,12 @@ func (q *Queries) WithAsterisk(ctx context.Context) (WithAsteriskRow, error) { } const withCrossSchema = `-- name: WithCrossSchema :many -SELECT users.id, users.name, users.age, bu.id, bu.name FROM users -INNER JOIN baz.users bu ON users.id = bu.id +SELECT + users.id, users.name, users.age, + bu.id, bu.name +FROM + users + INNER JOIN baz.users bu ON users.id = bu.id ` type WithCrossSchemaRow struct { @@ -148,7 +170,10 @@ func (q *Queries) WithCrossSchema(ctx context.Context) ([]WithCrossSchemaRow, er } const withSchema = `-- name: WithSchema :one -SELECT bu.id, bu.name FROM baz.users bu +SELECT + bu.id, bu.name +FROM + baz.users bu ` type WithSchemaRow struct { @@ -162,8 +187,35 @@ func (q *Queries) WithSchema(ctx context.Context) (WithSchemaRow, error) { return i, err } +const withSpaceBeforeParen = `-- name: WithSpaceBeforeParen :one +SELECT + users.id, users.name, users.age +FROM + users +` + +type WithSpaceBeforeParenRow struct { + User User `db:"user" json:"user"` +} + +func (q *Queries) WithSpaceBeforeParen(ctx context.Context) (WithSpaceBeforeParenRow, error) { + row := q.db.QueryRow(ctx, withSpaceBeforeParen) + var i WithSpaceBeforeParenRow + err := row.Scan(&i.User.ID, &i.User.Name, &i.User.Age) + return i, err +} + const withSubquery = `-- name: WithSubquery :many -SELECT users.id, users.name, users.age, (SELECT count(*) FROM users) AS total_count FROM users +SELECT + users.id, users.name, users.age, + ( + SELECT + count(*) + FROM + users + ) AS total_count +FROM + users ` type WithSubqueryRow struct { diff --git a/internal/endtoend/testdata/sqlc_embed/postgresql/pgx/query.sql b/internal/endtoend/testdata/sqlc_embed/postgresql/pgx/query.sql index 7822145508..22350e4637 100644 --- a/internal/endtoend/testdata/sqlc_embed/postgresql/pgx/query.sql +++ b/internal/endtoend/testdata/sqlc_embed/postgresql/pgx/query.sql @@ -22,4 +22,7 @@ SELECT sqlc.embed(bu) FROM baz.users bu; -- name: WithCrossSchema :many SELECT sqlc.embed(users), sqlc.embed(bu) FROM users -INNER JOIN baz.users bu ON users.id = bu.id; \ No newline at end of file +INNER JOIN baz.users bu ON users.id = bu.id; + +-- name: WithSpaceBeforeParen :one +SELECT sqlc.embed (users) FROM users; diff --git a/internal/sql/rewrite/embeds.go b/internal/sql/rewrite/embeds.go index 596c03be89..b60e000470 100644 --- a/internal/sql/rewrite/embeds.go +++ b/internal/sql/rewrite/embeds.go @@ -2,6 +2,7 @@ package rewrite import ( "fmt" + "strings" "github.com/sqlc-dev/sqlc/internal/sql/ast" "github.com/sqlc-dev/sqlc/internal/sql/astutils" @@ -9,14 +10,15 @@ import ( // Embed is an instance of `sqlc.embed(param)` type Embed struct { - Table *ast.TableName - param string - Node *ast.ColumnRef + Table *ast.TableName + param string + Node *ast.ColumnRef + spaces string } // Orig string to replace func (e Embed) Orig() string { - return fmt.Sprintf("sqlc.embed(%s)", e.param) + return fmt.Sprintf("sqlc.embed%s(%s)", e.spaces, e.param) } // EmbedSet is a set of Embed instances @@ -51,6 +53,15 @@ func Embeds(raw *ast.RawStmt) (*ast.RawStmt, EmbedSet) { param, _ := flatten(fun.Args) + // Calculate spaces between function name and opening parenthesis + // to handle formatters that insert spaces (e.g., pgFormatter) + funcName := "sqlc.embed" + spaces := "" + if fun.Args != nil && len(fun.Args.Items) > 0 { + leftParen := fun.Args.Items[0].Pos() - 1 + spaces = strings.Repeat(" ", leftParen-fun.Location-len(funcName)) + } + node := &ast.ColumnRef{ Fields: &ast.List{ Items: []ast.Node{ @@ -61,9 +72,10 @@ func Embeds(raw *ast.RawStmt) (*ast.RawStmt, EmbedSet) { } embeds = append(embeds, &Embed{ - Table: &ast.TableName{Name: param}, - param: param, - Node: node, + Table: &ast.TableName{Name: param}, + param: param, + Node: node, + spaces: spaces, }) cr.Replace(node) From 0be2ea3c5ffb0e89902fb62feb23f8b10fc874d0 Mon Sep 17 00:00:00 2001 From: Tim Millard Date: Wed, 1 Oct 2025 17:03:56 +1000 Subject: [PATCH 2/2] fix for nick --- .../sqlc_embed/postgresql/pgx/go/query.sql.go | 59 ++++--------------- 1 file changed, 11 insertions(+), 48 deletions(-) diff --git a/internal/endtoend/testdata/sqlc_embed/postgresql/pgx/go/query.sql.go b/internal/endtoend/testdata/sqlc_embed/postgresql/pgx/go/query.sql.go index c84a283d59..2fab1e54f9 100644 --- a/internal/endtoend/testdata/sqlc_embed/postgresql/pgx/go/query.sql.go +++ b/internal/endtoend/testdata/sqlc_embed/postgresql/pgx/go/query.sql.go @@ -11,11 +11,7 @@ import ( ) const duplicate = `-- name: Duplicate :one -SELECT - users.id, users.name, users.age, - users.id, users.name, users.age -FROM - users +SELECT users.id, users.name, users.age, users.id, users.name, users.age FROM users ` type DuplicateRow struct { @@ -38,12 +34,8 @@ func (q *Queries) Duplicate(ctx context.Context) (DuplicateRow, error) { } const join = `-- name: Join :one -SELECT - users.id, users.name, users.age, - posts.id, posts.user_id -FROM - posts - INNER JOIN users ON posts.user_id = users.id +SELECT users.id, users.name, users.age, posts.id, posts.user_id FROM posts +INNER JOIN users ON posts.user_id = users.id ` type JoinRow struct { @@ -65,10 +57,7 @@ func (q *Queries) Join(ctx context.Context) (JoinRow, error) { } const only = `-- name: Only :one -SELECT - users.id, users.name, users.age -FROM - users +SELECT users.id, users.name, users.age FROM users ` type OnlyRow struct { @@ -83,10 +72,7 @@ func (q *Queries) Only(ctx context.Context) (OnlyRow, error) { } const withAlias = `-- name: WithAlias :one -SELECT - u.id, u.name, u.age -FROM - users u +SELECT u.id, u.name, u.age FROM users u ` type WithAliasRow struct { @@ -101,11 +87,7 @@ func (q *Queries) WithAlias(ctx context.Context) (WithAliasRow, error) { } const withAsterisk = `-- name: WithAsterisk :one -SELECT - users.id, users.name, users.age, - id, name, age -FROM - users +SELECT users.id, users.name, users.age, id, name, age FROM users ` type WithAsteriskRow struct { @@ -130,12 +112,8 @@ func (q *Queries) WithAsterisk(ctx context.Context) (WithAsteriskRow, error) { } const withCrossSchema = `-- name: WithCrossSchema :many -SELECT - users.id, users.name, users.age, - bu.id, bu.name -FROM - users - INNER JOIN baz.users bu ON users.id = bu.id +SELECT users.id, users.name, users.age, bu.id, bu.name FROM users +INNER JOIN baz.users bu ON users.id = bu.id ` type WithCrossSchemaRow struct { @@ -170,10 +148,7 @@ func (q *Queries) WithCrossSchema(ctx context.Context) ([]WithCrossSchemaRow, er } const withSchema = `-- name: WithSchema :one -SELECT - bu.id, bu.name -FROM - baz.users bu +SELECT bu.id, bu.name FROM baz.users bu ` type WithSchemaRow struct { @@ -188,10 +163,7 @@ func (q *Queries) WithSchema(ctx context.Context) (WithSchemaRow, error) { } const withSpaceBeforeParen = `-- name: WithSpaceBeforeParen :one -SELECT - users.id, users.name, users.age -FROM - users +SELECT users.id, users.name, users.age FROM users ` type WithSpaceBeforeParenRow struct { @@ -206,16 +178,7 @@ func (q *Queries) WithSpaceBeforeParen(ctx context.Context) (WithSpaceBeforePare } const withSubquery = `-- name: WithSubquery :many -SELECT - users.id, users.name, users.age, - ( - SELECT - count(*) - FROM - users - ) AS total_count -FROM - users +SELECT users.id, users.name, users.age, (SELECT count(*) FROM users) AS total_count FROM users ` type WithSubqueryRow struct {