Skip to content

Commit

Permalink
fix(compiler): Fix column expansion to work with quoted non-keyword i…
Browse files Browse the repository at this point in the history
…dentifiers (#2576)

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

close #2575

* test: add endtoend
  • Loading branch information
orisano authored Aug 28, 2023
1 parent fefa6f4 commit 4d65e68
Show file tree
Hide file tree
Showing 16 changed files with 303 additions and 13 deletions.
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

0 comments on commit 4d65e68

Please sign in to comment.