Skip to content
Merged
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

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

Original file line number Diff line number Diff line change
Expand Up @@ -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;
INNER JOIN baz.users bu ON users.id = bu.id;

-- name: WithSpaceBeforeParen :one
SELECT sqlc.embed (users) FROM users;
26 changes: 19 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
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
Expand Down Expand Up @@ -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{
Expand All @@ -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)
Expand Down