Skip to content

Commit 8e0928f

Browse files
EliSauderCopilot
andcommitted
fix(mssql): measure a star qualifier with escaped quotes as written
A star qualified by a name that needs quoting was measured from the name as sqlc quotes it for output, so "a""b".* read as [a"b] measured short and left the star in the query, and "x]y".* measured long. Each quoted form is now built from the name itself, doubling its quotes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 916869a commit 8e0928f

8 files changed

Lines changed: 158 additions & 2 deletions

File tree

‎internal/compiler/expand.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ func (c *Compiler) starOldFunc(parts []string) func(string) int {
108108
}
109109
if strings.HasPrefix(s[length:], o) {
110110
length += len(o)
111-
} else if quoted := c.quote(o); strings.HasPrefix(s[length:], quoted) {
111+
} else if quoted := c.quote(parts[i]); strings.HasPrefix(s[length:], quoted) {
112112
length += len(quoted)
113-
} else if quoted := `"` + o + `"`; strings.HasPrefix(s[length:], quoted) {
113+
} else if quoted := `"` + strings.ReplaceAll(parts[i], `"`, `""`) + `"`; strings.HasPrefix(s[length:], quoted) {
114114
// SQL Server quotes with brackets, but reads double quotes
115115
// as an identifier too.
116116
length += len(quoted)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"contexts": ["base"]}

‎internal/endtoend/testdata/mssql_star_escaped_qualifier/mssql/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/mssql_star_escaped_qualifier/mssql/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/mssql_star_escaped_qualifier/mssql/go/query.sql.go‎

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- name: A :many
2+
SELECT "a""b".* FROM authors AS "a""b";
3+
4+
-- name: B :many
5+
SELECT "x]y".* FROM authors AS "x]y";
6+
7+
-- name: C :many
8+
SELECT [x]]y].* FROM authors AS [x]]y];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE authors (id INT NOT NULL, name NVARCHAR(10) NULL);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- engine: "mssql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "database/sql"

0 commit comments

Comments
 (0)