Skip to content

Commit 582ba6e

Browse files
authored
Added postgres CALL support (#1495)
* Added postgres CALL support * Regenerated endtoend * Converted FuncCall to a pointer
1 parent 0897308 commit 582ba6e

File tree

11 files changed

+168
-16
lines changed

11 files changed

+168
-16
lines changed

internal/compiler/find_params.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ func (p paramSearch) Visit(node ast.Node) astutils.Visitor {
6666
case *ast.BetweenExpr:
6767
p.parent = node
6868

69+
case *ast.CallStmt:
70+
p.parent = n.FuncCall
71+
6972
case *ast.FuncCall:
7073
p.parent = node
7174

internal/compiler/output_columns.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
8383
if len(targets.Items) == 0 && n.Larg != nil {
8484
return outputColumns(qc, n.Larg)
8585
}
86+
case *ast.CallStmt:
87+
targets = &ast.List{}
8688
case *ast.TruncateStmt:
8789
targets = &ast.List{}
8890
case *ast.UpdateStmt:
@@ -386,6 +388,8 @@ func sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, error) {
386388
list = &ast.List{
387389
Items: append(n.FromClause.Items, n.Relations.Items...),
388390
}
391+
case *ast.CallStmt:
392+
list = &ast.List{}
389393
default:
390394
return nil, fmt.Errorf("sourceTables: unsupported node type: %T", n)
391395
}

internal/compiler/parse.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
4848
}
4949
var table *ast.TableName
5050
switch n := raw.Stmt.(type) {
51+
case *ast.CallStmt:
5152
case *ast.SelectStmt:
5253
case *ast.DeleteStmt:
5354
case *ast.InsertStmt:

internal/endtoend/testdata/ddl_create_procedure/postgresql/pgx/go/query.sql.go

Lines changed: 46 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
-- name: Placeholder :exec
2-
SELECT 1;
1+
-- name: CallInsertData :exec
2+
CALL insert_data($1, $2);
33

4-
-- FIXME: Implement CALL
5-
-- name: CallInsertData :select
4+
-- name: CallInsertDataNoArgs :exec
65
CALL insert_data(1, 2);
6+
7+
-- name: CallInsertDataNamed :exec
8+
CALL insert_data(b => $1, a => $2);
9+
10+
-- name: CallInsertDataSqlcArgs :exec
11+
CALL insert_data(sqlc.arg('foo'), sqlc.arg('bar'));

internal/endtoend/testdata/ddl_create_procedure/postgresql/stdlib/go/query.sql.go

Lines changed: 46 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
-- name: Placeholder :exec
2-
SELECT 1;
1+
-- name: CallInsertData :exec
2+
CALL insert_data($1, $2);
33

4-
-- FIXME: Implement CALL
5-
-- name: CallInsertData :select
4+
-- name: CallInsertDataNoArgs :exec
65
CALL insert_data(1, 2);
6+
7+
-- name: CallInsertDataNamed :exec
8+
CALL insert_data(b => $1, a => $2);
9+
10+
-- name: CallInsertDataSqlcArgs :exec
11+
CALL insert_data(sqlc.arg('foo'), sqlc.arg('bar'));

internal/engine/postgresql/convert.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,33 @@ func convertBooleanTest(n *pg.BooleanTest) *ast.BooleanTest {
639639
}
640640
}
641641

642+
func convertCallStmt(n *pg.CallStmt) *ast.CallStmt {
643+
if n == nil {
644+
return nil
645+
}
646+
rel, err := parseRelationFromNodes(n.Funccall.Funcname)
647+
if err != nil {
648+
// TODO: How should we handle errors?
649+
panic(err)
650+
}
651+
652+
return &ast.CallStmt{
653+
FuncCall: &ast.FuncCall{
654+
Func: rel.FuncName(),
655+
Funcname: convertSlice(n.Funccall.Funcname),
656+
Args: convertSlice(n.Funccall.Args),
657+
AggOrder: convertSlice(n.Funccall.AggOrder),
658+
AggFilter: convertNode(n.Funccall.AggFilter),
659+
AggWithinGroup: n.Funccall.AggWithinGroup,
660+
AggStar: n.Funccall.AggStar,
661+
AggDistinct: n.Funccall.AggDistinct,
662+
FuncVariadic: n.Funccall.FuncVariadic,
663+
Over: convertWindowDef(n.Funccall.Over),
664+
Location: int(n.Funccall.Location),
665+
},
666+
}
667+
}
668+
642669
func convertCaseExpr(n *pg.CaseExpr) *ast.CaseExpr {
643670
if n == nil {
644671
return nil
@@ -3092,6 +3119,9 @@ func convertNode(node *pg.Node) ast.Node {
30923119

30933120
case *pg.Node_BooleanTest:
30943121
return convertBooleanTest(n.BooleanTest)
3122+
3123+
case *pg.Node_CallStmt:
3124+
return convertCallStmt(n.CallStmt)
30953125

30963126
case *pg.Node_CaseExpr:
30973127
return convertCaseExpr(n.CaseExpr)

internal/sql/ast/call_stmt.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ast
2+
3+
type CallStmt struct {
4+
FuncCall *FuncCall
5+
}
6+
7+
func (n *CallStmt) Pos() int {
8+
if n.FuncCall == nil {
9+
return 0
10+
}
11+
return n.FuncCall.Pos()
12+
}

internal/sql/astutils/rewrite.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.
414414
a.apply(n, "Xpr", nil, n.Xpr)
415415
a.apply(n, "Arg", nil, n.Arg)
416416

417+
case *ast.CallStmt:
418+
a.apply(n, "FuncCall", nil, n.FuncCall)
419+
417420
case *ast.CaseExpr:
418421
a.apply(n, "Xpr", nil, n.Xpr)
419422
a.apply(n, "Arg", nil, n.Arg)

internal/sql/astutils/walk.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@ func Walk(f Visitor, node ast.Node) {
511511
Walk(f, n.Arg)
512512
}
513513

514+
case *ast.CallStmt:
515+
if n.FuncCall != nil {
516+
Walk(f, n.FuncCall)
517+
}
518+
514519
case *ast.CaseExpr:
515520
if n.Xpr != nil {
516521
Walk(f, n.Xpr)

0 commit comments

Comments
 (0)