Skip to content

Commit f9a6345

Browse files
authored
feat: to allow spaces between function name and arguments of functions to be rewritten (#2250)
close #2130
1 parent fcfa8a4 commit f9a6345

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

examples/batch/postgresql/query.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ WHERE book_id = $1;
1212

1313
-- name: DeleteBookNamedFunc :batchexec
1414
DELETE FROM books
15-
WHERE book_id = sqlc.arg(book_id);
15+
WHERE book_id = sqlc.arg (book_id);
1616

1717
-- name: DeleteBookNamedSign :batchexec
1818
DELETE FROM books
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# package querytest
2-
query.sql:9:1: column reference "invalid_reference" not found
2+
query.sql:11:10: column reference "invalid_reference" not found
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# package querytest
2-
query.sql:9:1: table alias "p" does not exist
2+
query.sql:11:9: table alias "p" does not exist

internal/engine/dolphin/convert.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ func (c *cc) convertColumnNameExpr(n *pcast.ColumnNameExpr) *ast.ColumnRef {
300300
Fields: &ast.List{
301301
Items: items,
302302
},
303+
Location: n.OriginTextPosition(),
303304
}
304305
}
305306

@@ -603,6 +604,7 @@ func (c *cc) convertValueExpr(n *driver.ValueExpr) *ast.A_Const {
603604
Val: &ast.Integer{
604605
Ival: n.Datum.GetInt64(),
605606
},
607+
Location: n.OriginTextPosition(),
606608
}
607609

608610
case mysql.TypeDouble,
@@ -612,6 +614,7 @@ func (c *cc) convertValueExpr(n *driver.ValueExpr) *ast.A_Const {
612614
Val: &ast.Float{
613615
// TODO: Extract the value from n.TexprNode
614616
},
617+
Location: n.OriginTextPosition(),
615618
}
616619

617620
case mysql.TypeBlob, mysql.TypeString, mysql.TypeVarchar, mysql.TypeVarString, mysql.TypeLongBlob, mysql.TypeMediumBlob, mysql.TypeTinyBlob, mysql.TypeEnum:
@@ -620,6 +623,7 @@ func (c *cc) convertValueExpr(n *driver.ValueExpr) *ast.A_Const {
620623
Val: &ast.String{
621624
Str: n.Datum.GetString(),
622625
},
626+
Location: n.OriginTextPosition(),
623627
}
624628
}
625629

internal/sql/rewrite/parameters.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package rewrite
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/kyleconroy/sqlc/internal/config"
78
"github.com/kyleconroy/sqlc/internal/source"
@@ -66,7 +67,14 @@ func paramFromFuncCall(call *ast.FuncCall) (named.Param, string) {
6667

6768
// TODO: This code assumes that sqlc.arg(name) / sqlc.narg(name) is on a single line
6869
// with no extraneous spaces (or any non-significant tokens for that matter)
69-
origText := fmt.Sprintf("%s.%s(%s)", call.Func.Schema, call.Func.Name, origName)
70+
// except between the function name and argument
71+
funcName := call.Func.Schema + "." + call.Func.Name
72+
spaces := ""
73+
if call.Args != nil && len(call.Args.Items) > 0 {
74+
leftParen := call.Args.Items[0].Pos() - 1
75+
spaces = strings.Repeat(" ", leftParen-call.Location-len(funcName))
76+
}
77+
origText := fmt.Sprintf("%s%s(%s)", funcName, spaces, origName)
7078
return param, origText
7179
}
7280

0 commit comments

Comments
 (0)