Skip to content

fix(compiler): Support functions with OUT params #2865

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

Merged
merged 3 commits into from
Oct 17, 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
29 changes: 21 additions & 8 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,12 +550,15 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
if err != nil {
continue
}
table, err := qc.GetTable(&ast.TableName{
Catalog: fn.ReturnType.Catalog,
Schema: fn.ReturnType.Schema,
Name: fn.ReturnType.Name,
})
if err != nil {
var table *Table
if fn.ReturnType != nil {
table, err = qc.GetTable(&ast.TableName{
Catalog: fn.ReturnType.Catalog,
Schema: fn.ReturnType.Schema,
Name: fn.ReturnType.Name,
})
}
if table == nil || err != nil {
if n.Alias != nil && len(n.Alias.Colnames.Items) > 0 {
table = &Table{}
for _, colName := range n.Alias.Colnames.Items {
Expand All @@ -575,12 +578,22 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
Schema: fn.Rel.Schema,
Name: fn.Rel.Name,
},
Columns: []*Column{
}
if len(fn.Outs) > 0 {
for _, arg := range fn.Outs {
table.Columns = append(table.Columns, &Column{
Name: arg.Name,
DataType: arg.Type.Name,
})
}
}
if fn.ReturnType != nil {
table.Columns = []*Column{
{
Name: colName,
DataType: fn.ReturnType.Name,
},
},
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions internal/compiler/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package compiler

import (
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
)

type Function struct {
Rel *ast.FuncName
ReturnType *ast.TypeName
Outs []*catalog.Argument
}

type Table struct {
Expand Down
1 change: 1 addition & 0 deletions internal/compiler/query_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (qc QueryCatalog) GetFunc(rel *ast.FuncName) (*Function, error) {
}
return &Function{
Rel: rel,
Outs: funcs[0].OutArgs(),
ReturnType: funcs[0].ReturnType,
}, nil
}
1 change: 1 addition & 0 deletions internal/endtoend/testdata/func_out_param/issue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/sqlc-dev/sqlc/issues/1654
32 changes: 32 additions & 0 deletions internal/endtoend/testdata/func_out_param/pgx/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions internal/endtoend/testdata/func_out_param/pgx/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions internal/endtoend/testdata/func_out_param/pgx/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions internal/endtoend/testdata/func_out_param/pgx/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- name: CreateAuthor :one
SELECT * FROM add_author (
sqlc.arg(name), sqlc.arg(bio)
);
14 changes: 14 additions & 0 deletions internal/endtoend/testdata/func_out_param/pgx/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Example queries for sqlc
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text
);

CREATE OR REPLACE FUNCTION add_author (name text, bio text, out id int)
AS $$
DECLARE
BEGIN
id = 123;
END;
$$ LANGUAGE plpgsql;
10 changes: 10 additions & 0 deletions internal/endtoend/testdata/func_out_param/pgx/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "2"
sql:
- engine: "postgresql"
schema: "schema.sql"
queries: "query.sql"
gen:
go:
package: "querytest"
out: "go"
sql_package: "pgx/v5"
11 changes: 11 additions & 0 deletions internal/sql/catalog/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ func (f *Function) InArgs() []*Argument {
return args
}

func (f *Function) OutArgs() []*Argument {
var args []*Argument
for _, a := range f.Args {
switch a.Mode {
case ast.FuncParamOut:
args = append(args, a)
}
}
return args
}

func (c *Catalog) createFunction(stmt *ast.CreateFunctionStmt) error {
ns := stmt.Func.Schema
if ns == "" {
Expand Down