Skip to content

Commit b40e006

Browse files
committed
fix: set-returning function return_type
1 parent 26e54f3 commit b40e006

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
lines changed

src/lib/sql/functions.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ select
4040
coalesce(f_args.args, '[]') as args,
4141
pg_get_function_arguments(f.oid) as argument_types,
4242
pg_get_function_identity_arguments(f.oid) as identity_argument_types,
43-
rt.typname as return_type,
43+
f.prorettype::int8 as return_type_id,
44+
pg_get_function_result(f.oid) as return_type,
4445
nullif(rt.typrelid::int8, 0) as return_type_relation_id,
4546
f.proretset as is_set_returning_function,
4647
case

src/lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ const postgresFunctionSchema = Type.Object({
107107
),
108108
argument_types: Type.String(),
109109
identity_argument_types: Type.String(),
110+
return_type_id: Type.Integer(),
110111
return_type: Type.String(),
111112
return_type_relation_id: Type.Union([Type.Integer(), Type.Null()]),
112113
is_set_returning_function: Type.Boolean(),

src/server/templates/typescript.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export interface Database {
224224
.map(
225225
({
226226
args,
227-
return_type,
227+
return_type_id,
228228
return_type_relation_id,
229229
is_set_returning_function,
230230
}) => `{
@@ -312,7 +312,12 @@ export interface Database {
312312
}
313313
314314
// Case 3: returns base/composite/enum type.
315-
return pgTypeToTsType(return_type, types, schemas)
315+
const type = types.find(({ id }) => id === return_type_id)
316+
if (type) {
317+
return pgTypeToTsType(type.name, types, schemas)
318+
}
319+
320+
return 'unknown'
316321
})()})${is_set_returning_function ? '[]' : ''}
317322
}`
318323
)

test/lib/functions.ts

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ test('list', async () => {
3535
"is_set_returning_function": false,
3636
"language": "sql",
3737
"name": "add",
38-
"return_type": "int4",
38+
"return_type": "integer",
39+
"return_type_id": 23,
3940
"return_type_relation_id": null,
4041
"schema": "public",
4142
"security_definer": false,
@@ -134,7 +135,8 @@ test('retrieve, create, update, delete', async () => {
134135
"is_set_returning_function": false,
135136
"language": "sql",
136137
"name": "test_func",
137-
"return_type": "int4",
138+
"return_type": "integer",
139+
"return_type_id": 23,
138140
"return_type_relation_id": null,
139141
"schema": "public",
140142
"security_definer": true,
@@ -183,7 +185,8 @@ test('retrieve, create, update, delete', async () => {
183185
"is_set_returning_function": false,
184186
"language": "sql",
185187
"name": "test_func",
186-
"return_type": "int4",
188+
"return_type": "integer",
189+
"return_type_id": 23,
187190
"return_type_relation_id": null,
188191
"schema": "public",
189192
"security_definer": true,
@@ -236,7 +239,8 @@ test('retrieve, create, update, delete', async () => {
236239
"is_set_returning_function": false,
237240
"language": "sql",
238241
"name": "test_func_renamed",
239-
"return_type": "int4",
242+
"return_type": "integer",
243+
"return_type_id": 23,
240244
"return_type_relation_id": null,
241245
"schema": "test_schema",
242246
"security_definer": true,
@@ -285,7 +289,8 @@ test('retrieve, create, update, delete', async () => {
285289
"is_set_returning_function": false,
286290
"language": "sql",
287291
"name": "test_func_renamed",
288-
"return_type": "int4",
292+
"return_type": "integer",
293+
"return_type_id": 23,
289294
"return_type_relation_id": null,
290295
"schema": "test_schema",
291296
"security_definer": true,
@@ -304,3 +309,43 @@ test('retrieve, create, update, delete', async () => {
304309

305310
await pgMeta.schemas.remove(testSchemaId)
306311
})
312+
313+
test('retrieve set-returning function', async () => {
314+
const res = await pgMeta.functions.retrieve({
315+
schema: 'public',
316+
name: 'function_returning_set_of_rows',
317+
args: [],
318+
})
319+
expect(res.data).toMatchInlineSnapshot(
320+
{ id: expect.any(Number) },
321+
`
322+
{
323+
"args": [],
324+
"argument_types": "",
325+
"behavior": "STABLE",
326+
"complete_statement": "CREATE OR REPLACE FUNCTION public.function_returning_set_of_rows()
327+
RETURNS SETOF users
328+
LANGUAGE sql
329+
STABLE
330+
AS $function$
331+
select * from public.users;
332+
$function$
333+
",
334+
"config_params": null,
335+
"definition": "
336+
select * from public.users;
337+
",
338+
"id": Any<Number>,
339+
"identity_argument_types": "",
340+
"is_set_returning_function": true,
341+
"language": "sql",
342+
"name": "function_returning_set_of_rows",
343+
"return_type": "SETOF users",
344+
"return_type_id": 16392,
345+
"return_type_relation_id": 16390,
346+
"schema": "public",
347+
"security_definer": false,
348+
}
349+
`
350+
)
351+
})

0 commit comments

Comments
 (0)