Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: to allow spaces between function name and arguments of functions to be rewritten #2250

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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