Skip to content

Commit

Permalink
feat: to allow spaces between function name and arguments of function…
Browse files Browse the repository at this point in the history
…s to be rewritten (#2250)

close #2130
  • Loading branch information
orisano committed Jun 6, 2023
1 parent fcfa8a4 commit f9a6345
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/batch/postgresql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ WHERE book_id = $1;

-- name: DeleteBookNamedFunc :batchexec
DELETE FROM books
WHERE book_id = sqlc.arg(book_id);
WHERE book_id = sqlc.arg (book_id);

-- name: DeleteBookNamedSign :batchexec
DELETE FROM books
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# package querytest
query.sql:9:1: column reference "invalid_reference" not found
query.sql:11:10: column reference "invalid_reference" not found
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# package querytest
query.sql:9:1: table alias "p" does not exist
query.sql:11:9: table alias "p" does not exist
4 changes: 4 additions & 0 deletions internal/engine/dolphin/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ func (c *cc) convertColumnNameExpr(n *pcast.ColumnNameExpr) *ast.ColumnRef {
Fields: &ast.List{
Items: items,
},
Location: n.OriginTextPosition(),
}
}

Expand Down Expand Up @@ -603,6 +604,7 @@ func (c *cc) convertValueExpr(n *driver.ValueExpr) *ast.A_Const {
Val: &ast.Integer{
Ival: n.Datum.GetInt64(),
},
Location: n.OriginTextPosition(),
}

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

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

Expand Down
10 changes: 9 additions & 1 deletion internal/sql/rewrite/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rewrite

import (
"fmt"
"strings"

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

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

Expand Down

0 comments on commit f9a6345

Please sign in to comment.