Skip to content

Commit 08fbcd8

Browse files
committed
Test OUT arguments in PostgreSQL procedures
1 parent 26f6650 commit 08fbcd8

File tree

7 files changed

+202
-0
lines changed

7 files changed

+202
-0
lines changed

internal/endtoend/testdata/ddl_create_procedure_with_out_args/postgresql/pgx/v5/go/db.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/ddl_create_procedure_with_out_args/postgresql/pgx/v5/go/models.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/ddl_create_procedure_with_out_args/postgresql/pgx/v5/go/query.sql.go

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# PostgreSQL Procedure OUT Arguments Support
2+
3+
I have implemented support for capturing `OUT` and `INOUT` arguments from PostgreSQL `CALL` statements in `sqlc`.
4+
5+
## Implementation Details
6+
7+
The `sqlc` analyzer/compiler (`internal/compiler/output_columns.go`) was updated to handle `CALL` statements. Previously, `CALL` statements were treated as having no output columns. The updated logic now:
8+
1. Resolves the procedure definition from the catalog.
9+
2. Identifies arguments with `OUT`, `INOUT`, or `TABLE` modes.
10+
3. Maps these arguments to output columns in the generated code.
11+
12+
## How to get OUT params
13+
14+
To retrieve `OUT` arguments in your Go code:
15+
16+
1. **Annotate your query with `:one`**: Use `:one` (or `:many` if the procedure returns multiple rows) instead of `:exec`. `:exec` tells `sqlc` to disregard any result rows, so it won't capture the `OUT` values.
17+
18+
```sql
19+
-- name: CallInsertData :one
20+
CALL insert_data($1, $2, null);
21+
```
22+
23+
2. **Provide placeholders**: You still need to provide placeholders for the `OUT` arguments in the SQL call (e.g., `null`), as required by PostgreSQL's `CALL` syntax when not using named arguments for everything, or simply to satisfy `sqlc`'s signature matching.
24+
25+
3. **Use the return value**: The generated Go method will now return the `OUT` parameters.
26+
- If there is a single `OUT` parameter, it will be returned directly.
27+
- If there are multiple `OUT` parameters, a struct will be returned containing all of them.
28+
29+
### Example
30+
31+
**Schema:**
32+
```sql
33+
CREATE PROCEDURE insert_data(IN a integer, IN b integer, OUT c integer) ...
34+
```
35+
36+
**Query:**
37+
```sql
38+
-- name: CallInsertData :one
39+
CALL insert_data($1, $2, null);
40+
```
41+
42+
**Generated Go:**
43+
```go
44+
func (q *Queries) CallInsertData(ctx context.Context, arg CallInsertDataParams) (pgtype.Int4, error) {
45+
// ...
46+
var c pgtype.Int4
47+
err := row.Scan(&c)
48+
return c, err
49+
}
50+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- name: CallInsertData :one
2+
CALL insert_data($1, $2, null);
3+
4+
-- name: CallInsertDataNoArgs :exec
5+
CALL insert_data(1, 2, null);
6+
7+
-- name: CallInsertDataNamed :one
8+
CALL insert_data(b => $1, a => $2, c => null);
9+
10+
-- name: CallInsertDataSqlcArgs :exec
11+
CALL insert_data(sqlc.arg('foo'), sqlc.arg('bar'), sqlc.arg('с'));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE TABLE tbl (
2+
value integer
3+
);
4+
5+
-- https://www.postgresql.org/docs/current/sql-createprocedure.html
6+
CREATE PROCEDURE insert_data(IN a integer, IN b integer, OUT c integer)
7+
LANGUAGE plpgsql
8+
AS $$
9+
BEGIN
10+
INSERT INTO tbl VALUES (a);
11+
INSERT INTO tbl VALUES (b);
12+
13+
c := 777;
14+
END;
15+
$$;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "postgresql",
7+
"sql_package": "pgx/v5",
8+
"name": "querytest",
9+
"schema": "schema.sql",
10+
"queries": "query.sql"
11+
}
12+
]
13+
}

0 commit comments

Comments
 (0)