Skip to content

Commit 4d65e68

Browse files
authored
fix(compiler): Fix column expansion to work with quoted non-keyword identifiers (#2576)
* fix(compiler): Fix column expansion to work with quoted non-keyword identifiers close #2575 * test: add endtoend
1 parent fefa6f4 commit 4d65e68

File tree

16 files changed

+303
-13
lines changed

16 files changed

+303
-13
lines changed

internal/compiler/expand.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,7 @@ func (c *Compiler) expand(qc *QueryCatalog, raw *ast.RawStmt) ([]source.Edit, er
3838

3939
func (c *Compiler) quoteIdent(ident string) string {
4040
if c.parser.IsReservedKeyword(ident) {
41-
switch c.conf.Engine {
42-
case config.EngineMySQL:
43-
return "`" + ident + "`"
44-
default:
45-
return "\"" + ident + "\""
46-
}
41+
return c.quote(ident)
4742
}
4843
if c.conf.Engine == config.EnginePostgreSQL {
4944
// camelCase means the column is also camelCase
@@ -54,6 +49,15 @@ func (c *Compiler) quoteIdent(ident string) string {
5449
return ident
5550
}
5651

52+
func (c *Compiler) quote(x string) string {
53+
switch c.conf.Engine {
54+
case config.EngineMySQL:
55+
return "`" + x + "`"
56+
default:
57+
return "\"" + x + "\""
58+
}
59+
}
60+
5761
func (c *Compiler) expandStmt(qc *QueryCatalog, raw *ast.RawStmt, node ast.Node) ([]source.Edit, error) {
5862
tables, err := c.sourceTables(qc, node)
5963
if err != nil {
@@ -132,16 +136,36 @@ func (c *Compiler) expandStmt(qc *QueryCatalog, raw *ast.RawStmt, node ast.Node)
132136
for _, p := range parts {
133137
old = append(old, c.quoteIdent(p))
134138
}
135-
oldString := strings.Join(old, ".")
139+
140+
var oldString string
141+
var oldFunc func(string) int
136142

137143
// use the sqlc.embed string instead
138144
if embed, ok := qc.embeds.Find(ref); ok {
139145
oldString = embed.Orig()
146+
} else {
147+
oldFunc = func(s string) int {
148+
length := 0
149+
for i, o := range old {
150+
if hasSeparator := i > 0; hasSeparator {
151+
length++
152+
}
153+
if strings.HasPrefix(s[length:], o) {
154+
length += len(o)
155+
} else if quoted := c.quote(o); strings.HasPrefix(s[length:], quoted) {
156+
length += len(quoted)
157+
} else {
158+
length += len(o)
159+
}
160+
}
161+
return length
162+
}
140163
}
141164

142165
edits = append(edits, source.Edit{
143166
Location: res.Location - raw.StmtLocation,
144167
Old: oldString,
168+
OldFunc: oldFunc,
145169
New: strings.Join(cols, ", "),
146170
})
147171
}

internal/endtoend/testdata/star_expansion/mysql/go/query.sql.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/star_expansion/mysql/query.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ CREATE TABLE foo (a text, b text);
22

33
/* name: StarExpansion :many */
44
SELECT *, *, foo.* FROM foo;
5+
6+
/* name: StarQuotedExpansion :many */
7+
SELECT `t`.* FROM foo `t`;

internal/endtoend/testdata/star_expansion/postgresql/pgx/v4/go/query.sql.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/star_expansion/postgresql/pgx/v4/query.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ CREATE TABLE foo (a text, b text);
22

33
-- name: StarExpansion :many
44
SELECT *, *, foo.* FROM foo;
5+
6+
-- name: StarQuotedExpansion :many
7+
SELECT "t".* FROM foo "t";

internal/endtoend/testdata/star_expansion/postgresql/pgx/v5/go/query.sql.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/star_expansion/postgresql/pgx/v5/query.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ CREATE TABLE foo (a text, b text);
22

33
-- name: StarExpansion :many
44
SELECT *, *, foo.* FROM foo;
5+
6+
-- name: StarQuotedExpansion :many
7+
SELECT "t".* FROM foo "t";

internal/endtoend/testdata/star_expansion/postgresql/stdlib/go/query.sql.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/star_expansion/postgresql/stdlib/query.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ CREATE TABLE foo (a text, b text);
22

33
-- name: StarExpansion :many
44
SELECT *, *, foo.* FROM foo;
5+
6+
-- name: StarQuotedExpansion :many
7+
SELECT "t".* FROM foo "t";

internal/endtoend/testdata/star_expansion/sqlite/go/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/star_expansion/sqlite/go/models.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/star_expansion/sqlite/go/query.sql.go

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE foo (a text, b text);
2+
3+
-- name: StarExpansion :many
4+
SELECT *, *, foo.* FROM foo;
5+
6+
-- name: StarQuotedExpansion :many
7+
SELECT "t".* FROM foo "t";
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"engine": "sqlite",
6+
"path": "go",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)