@@ -14,8 +14,8 @@ export default class PostgresMetaFunctions {
14
14
PostgresMetaResult < PostgresFunction [ ] >
15
15
> {
16
16
const sql = includeSystemSchemas
17
- ? functionsSql
18
- : `${ functionsSql } WHERE NOT (n.nspname IN (${ DEFAULT_SYSTEM_SCHEMAS . map ( literal ) . join (
17
+ ? enrichedFunctionsSql
18
+ : `${ enrichedFunctionsSql } WHERE NOT (schema IN (${ DEFAULT_SYSTEM_SCHEMAS . map ( literal ) . join (
19
19
','
20
20
) } ));`
21
21
return await this . query ( sql )
@@ -25,21 +25,25 @@ export default class PostgresMetaFunctions {
25
25
async retrieve ( {
26
26
name,
27
27
schema,
28
+ args,
28
29
} : {
29
30
name : string
30
31
schema : string
32
+ args : string [ ]
31
33
} ) : Promise < PostgresMetaResult < PostgresFunction > >
32
34
async retrieve ( {
33
35
id,
34
36
name,
35
37
schema = 'public' ,
38
+ args = [ ] ,
36
39
} : {
37
40
id ?: number
38
41
name ?: string
39
42
schema ?: string
43
+ args ?: string [ ]
40
44
} ) : Promise < PostgresMetaResult < PostgresFunction > > {
41
45
if ( id ) {
42
- const sql = `${ functionsSql } WHERE p.oid = ${ literal ( id ) } ;`
46
+ const sql = `${ enrichedFunctionsSql } WHERE id = ${ literal ( id ) } ;`
43
47
const { data, error } = await this . query ( sql )
44
48
if ( error ) {
45
49
return { data, error }
@@ -48,17 +52,20 @@ export default class PostgresMetaFunctions {
48
52
} else {
49
53
return { data : data [ 0 ] , error }
50
54
}
51
- } else if ( name ) {
52
- const sql = `${ functionsSql } WHERE p.proname = ${ literal ( name ) } AND n.nspname = ${ literal (
53
- schema
54
- ) } ;`
55
+ } else if ( name && schema && args ) {
56
+ const argTypes = args . join ( ', ' )
57
+ const sql = `${ enrichedFunctionsSql } WHERE schema = ${ literal ( schema ) } AND name = ${ literal (
58
+ name
59
+ ) } AND argument_types = ${ literal ( argTypes ) } ;`
55
60
const { data, error } = await this . query ( sql )
56
61
if ( error ) {
57
62
return { data, error }
58
63
} else if ( data . length === 0 ) {
59
64
return {
60
65
data : null ,
61
- error : { message : `Cannot find a function named ${ name } in schema ${ schema } ` } ,
66
+ error : {
67
+ message : `Cannot find function "${ schema } "."${ name } "(${ argTypes } )` ,
68
+ } ,
62
69
}
63
70
} else {
64
71
return { data : data [ 0 ] , error }
@@ -71,21 +78,20 @@ export default class PostgresMetaFunctions {
71
78
async create ( {
72
79
name,
73
80
schema = 'public' ,
74
- params ,
81
+ args = [ ] ,
75
82
definition,
76
83
rettype = 'void' ,
77
84
language = 'sql' ,
78
85
} : {
79
86
name : string
80
87
schema ?: string
81
- params ?: string [ ]
88
+ args ?: string [ ]
82
89
definition : string
83
90
rettype ?: string
84
91
language ?: string
85
92
} ) : Promise < PostgresMetaResult < PostgresFunction > > {
86
93
const sql = `
87
- CREATE FUNCTION ${ ident ( schema ) } .${ ident ( name ) }
88
- ${ params && params . length ? `(${ params . join ( ',' ) } )` : '()' }
94
+ CREATE FUNCTION ${ ident ( schema ) } .${ ident ( name ) } (${ args . join ( ', ' ) } )
89
95
RETURNS ${ rettype }
90
96
AS ${ literal ( definition ) }
91
97
LANGUAGE ${ language }
@@ -95,7 +101,7 @@ export default class PostgresMetaFunctions {
95
101
if ( error ) {
96
102
return { data : null , error }
97
103
}
98
- return await this . retrieve ( { name, schema } )
104
+ return await this . retrieve ( { name, schema, args } )
99
105
}
100
106
101
107
async update (
@@ -127,7 +133,7 @@ export default class PostgresMetaFunctions {
127
133
} ) SET SCHEMA ${ ident ( schema ) } ;`
128
134
: ''
129
135
130
- const sql = `BEGIN;${ updateNameSql } ${ updateSchemaSql } COMMIT;`
136
+ const sql = `BEGIN; ${ updateNameSql } ${ updateSchemaSql } COMMIT;`
131
137
132
138
const { error } = await this . query ( sql )
133
139
if ( error ) {
@@ -145,7 +151,7 @@ export default class PostgresMetaFunctions {
145
151
return { data : null , error }
146
152
}
147
153
const sql = `DROP FUNCTION ${ ident ( func ! . schema ) } .${ ident ( func ! . name ) }
148
- ${ func ! . argument_types ? ` (${ func ! . argument_types } )` : '()' }
154
+ (${ func ! . argument_types } )
149
155
${ cascade ? 'CASCADE' : 'RESTRICT' } ;`
150
156
{
151
157
const { error } = await this . query ( sql )
@@ -156,3 +162,12 @@ export default class PostgresMetaFunctions {
156
162
return { data : func ! , error : null }
157
163
}
158
164
}
165
+
166
+ const enrichedFunctionsSql = `
167
+ WITH functions AS (
168
+ ${ functionsSql }
169
+ )
170
+ SELECT
171
+ *
172
+ FROM functions
173
+ `
0 commit comments