Skip to content

fix(compiler): Fix column expansion to work with quoted non-keyword identifiers #2576

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

Merged
merged 2 commits into from
Aug 28, 2023
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
38 changes: 31 additions & 7 deletions internal/compiler/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ func (c *Compiler) expand(qc *QueryCatalog, raw *ast.RawStmt) ([]source.Edit, er

func (c *Compiler) quoteIdent(ident string) string {
if c.parser.IsReservedKeyword(ident) {
switch c.conf.Engine {
case config.EngineMySQL:
return "`" + ident + "`"
default:
return "\"" + ident + "\""
}
return c.quote(ident)
}
if c.conf.Engine == config.EnginePostgreSQL {
// camelCase means the column is also camelCase
Expand All @@ -54,6 +49,15 @@ func (c *Compiler) quoteIdent(ident string) string {
return ident
}

func (c *Compiler) quote(x string) string {
switch c.conf.Engine {
case config.EngineMySQL:
return "`" + x + "`"
default:
return "\"" + x + "\""
}
}

func (c *Compiler) expandStmt(qc *QueryCatalog, raw *ast.RawStmt, node ast.Node) ([]source.Edit, error) {
tables, err := c.sourceTables(qc, node)
if err != nil {
Expand Down Expand Up @@ -132,16 +136,36 @@ func (c *Compiler) expandStmt(qc *QueryCatalog, raw *ast.RawStmt, node ast.Node)
for _, p := range parts {
old = append(old, c.quoteIdent(p))
}
oldString := strings.Join(old, ".")

var oldString string
var oldFunc func(string) int

// use the sqlc.embed string instead
if embed, ok := qc.embeds.Find(ref); ok {
oldString = embed.Orig()
} else {
oldFunc = func(s string) int {
length := 0
for i, o := range old {
if hasSeparator := i > 0; hasSeparator {
length++
}
if strings.HasPrefix(s[length:], o) {
length += len(o)
} else if quoted := c.quote(o); strings.HasPrefix(s[length:], quoted) {
length += len(quoted)
} else {
length += len(o)
}
}
return length
}
}

edits = append(edits, source.Edit{
Location: res.Location - raw.StmtLocation,
Old: oldString,
OldFunc: oldFunc,
New: strings.Join(cols, ", "),
})
}
Expand Down
27 changes: 27 additions & 0 deletions internal/endtoend/testdata/star_expansion/mysql/go/query.sql.go

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

3 changes: 3 additions & 0 deletions internal/endtoend/testdata/star_expansion/mysql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ CREATE TABLE foo (a text, b text);

/* name: StarExpansion :many */
SELECT *, *, foo.* FROM foo;

/* name: StarQuotedExpansion :many */
SELECT `t`.* FROM foo `t`;

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 @@ -2,3 +2,6 @@ CREATE TABLE foo (a text, b text);

-- name: StarExpansion :many
SELECT *, *, foo.* FROM foo;

-- name: StarQuotedExpansion :many
SELECT "t".* FROM foo "t";

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 @@ -2,3 +2,6 @@ CREATE TABLE foo (a text, b text);

-- name: StarExpansion :many
SELECT *, *, foo.* FROM foo;

-- name: StarQuotedExpansion :many
SELECT "t".* FROM foo "t";

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 @@ -2,3 +2,6 @@ CREATE TABLE foo (a text, b text);

-- name: StarExpansion :many
SELECT *, *, foo.* FROM foo;

-- name: StarQuotedExpansion :many
SELECT "t".* FROM foo "t";
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/star_expansion/sqlite/go/db.go

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

14 changes: 14 additions & 0 deletions internal/endtoend/testdata/star_expansion/sqlite/go/models.go

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

81 changes: 81 additions & 0 deletions internal/endtoend/testdata/star_expansion/sqlite/go/query.sql.go

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

7 changes: 7 additions & 0 deletions internal/endtoend/testdata/star_expansion/sqlite/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE foo (a text, b text);

-- name: StarExpansion :many
SELECT *, *, foo.* FROM foo;

-- name: StarQuotedExpansion :many
SELECT "t".* FROM foo "t";
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/star_expansion/sqlite/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"engine": "sqlite",
"path": "go",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
Loading