Skip to content

Commit a56a9c1

Browse files
authored
handling of dollar signs in function names by removing the char from codegen output (#1359)
adding a e2e case
1 parent 379cc12 commit a56a9c1

File tree

8 files changed

+85
-3
lines changed

8 files changed

+85
-3
lines changed

internal/codegen/golang/result.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,12 @@ func buildQueries(r *compiler.Result, settings config.CombinedSettings, structs
193193

194194
if len(query.Columns) == 1 {
195195
c := query.Columns[0]
196+
name := columnName(c, 0)
197+
if c.IsFuncCall {
198+
name = strings.Replace(name, "$", "_", -1)
199+
}
196200
gq.Ret = QueryValue{
197-
Name: columnName(c, 0),
201+
Name: name,
198202
Typ: goType(r, c, settings),
199203
SQLPackage: sqlpkg,
200204
}

internal/compiler/output_columns.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,18 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
210210
}
211211
fun, err := qc.catalog.ResolveFuncCall(n)
212212
if err == nil {
213-
cols = append(cols, &Column{Name: name, DataType: dataType(fun.ReturnType), NotNull: !fun.ReturnTypeNullable})
213+
cols = append(cols, &Column{
214+
Name: name,
215+
DataType: dataType(fun.ReturnType),
216+
NotNull: !fun.ReturnTypeNullable,
217+
IsFuncCall: true,
218+
})
214219
} else {
215-
cols = append(cols, &Column{Name: name, DataType: "any"})
220+
cols = append(cols, &Column{
221+
Name: name,
222+
DataType: "any",
223+
IsFuncCall: true,
224+
})
216225
}
217226

218227
case *ast.SubLink:

internal/compiler/query.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Column struct {
2222
Comment string
2323
Length *int
2424
IsNamedParam bool
25+
IsFuncCall bool
2526

2627
// XXX: Figure out what PostgreSQL calls `foo.id`
2728
Scope string

internal/endtoend/testdata/identifier_dollar_sign/db/db.go

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

internal/endtoend/testdata/identifier_dollar_sign/db/models.go

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

internal/endtoend/testdata/identifier_dollar_sign/db/query.sql.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE FUNCTION f$n() RETURNS integer AS 'SELECT 1';
2+
3+
-- name: Fn :one
4+
SELECT f$n();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "db",
6+
"engine": "postgresql",
7+
"schema": "query.sql",
8+
"queries": "query.sql"
9+
}
10+
]
11+
}

0 commit comments

Comments
 (0)