Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sqlc.embed compatible with pgFormatter and sql-formatter #3431

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions examples/batch/postgresql/batch.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/batch/postgresql/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions examples/batch/postgresql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ WHERE book_id = $3;
-- name: GetBiography :batchone
SELECT biography FROM authors
WHERE author_id = $1;

-- name: GetAuthorWithFirstBook :batchone
SELECT sqlc.embed (books), sqlc.embed (authors)
FROM authors
INNER JOIN books ON authors.author_id = books.author_id
WHERE authors.author_id = $1;
22 changes: 15 additions & 7 deletions internal/sql/rewrite/embeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ package rewrite

import (
"fmt"
"strings"

"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/astutils"
)

// Embed is an instance of `sqlc.embed(param)`
type Embed struct {
Table *ast.TableName
param string
Node *ast.ColumnRef
Table *ast.TableName
param string
origin string
Node *ast.ColumnRef
}

// Orig string to replace
func (e Embed) Orig() string {
return fmt.Sprintf("sqlc.embed(%s)", e.param)
return e.origin
}

// EmbedSet is a set of Embed instances
Expand Down Expand Up @@ -60,10 +62,16 @@ func Embeds(raw *ast.RawStmt) (*ast.RawStmt, EmbedSet) {
},
}

// Get the origin string
const noSpaceLen = 11
spaceCount := fun.Args.Items[0].Pos() - fun.Pos() - noSpaceLen
origin := fmt.Sprintf("sqlc.embed%s(%s)", strings.Repeat(" ", spaceCount), param)

embeds = append(embeds, &Embed{
Table: &ast.TableName{Name: param},
param: param,
Node: node,
Table: &ast.TableName{Name: param},
param: param,
origin: origin,
Node: node,
})

cr.Replace(node)
Expand Down