@@ -14,8 +14,8 @@ export default class PostgresMetaFunctions {
1414 PostgresMetaResult < PostgresFunction [ ] >
1515 > {
1616 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 (
1919 ','
2020 ) } ));`
2121 return await this . query ( sql )
@@ -25,21 +25,25 @@ export default class PostgresMetaFunctions {
2525 async retrieve ( {
2626 name,
2727 schema,
28+ args,
2829 } : {
2930 name : string
3031 schema : string
32+ args : string [ ]
3133 } ) : Promise < PostgresMetaResult < PostgresFunction > >
3234 async retrieve ( {
3335 id,
3436 name,
3537 schema = 'public' ,
38+ args = [ ] ,
3639 } : {
3740 id ?: number
3841 name ?: string
3942 schema ?: string
43+ args ?: string [ ]
4044 } ) : Promise < PostgresMetaResult < PostgresFunction > > {
4145 if ( id ) {
42- const sql = `${ functionsSql } WHERE p.oid = ${ literal ( id ) } ;`
46+ const sql = `${ enrichedFunctionsSql } WHERE id = ${ literal ( id ) } ;`
4347 const { data, error } = await this . query ( sql )
4448 if ( error ) {
4549 return { data, error }
@@ -48,17 +52,20 @@ export default class PostgresMetaFunctions {
4852 } else {
4953 return { data : data [ 0 ] , error }
5054 }
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 ) } ;`
5560 const { data, error } = await this . query ( sql )
5661 if ( error ) {
5762 return { data, error }
5863 } else if ( data . length === 0 ) {
5964 return {
6065 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+ } ,
6269 }
6370 } else {
6471 return { data : data [ 0 ] , error }
@@ -71,21 +78,20 @@ export default class PostgresMetaFunctions {
7178 async create ( {
7279 name,
7380 schema = 'public' ,
74- params ,
81+ args = [ ] ,
7582 definition,
7683 rettype = 'void' ,
7784 language = 'sql' ,
7885 } : {
7986 name : string
8087 schema ?: string
81- params ?: string [ ]
88+ args ?: string [ ]
8289 definition : string
8390 rettype ?: string
8491 language ?: string
8592 } ) : Promise < PostgresMetaResult < PostgresFunction > > {
8693 const sql = `
87- CREATE FUNCTION ${ ident ( schema ) } .${ ident ( name ) }
88- ${ params && params . length ? `(${ params . join ( ',' ) } )` : '()' }
94+ CREATE FUNCTION ${ ident ( schema ) } .${ ident ( name ) } (${ args . join ( ', ' ) } )
8995 RETURNS ${ rettype }
9096 AS ${ literal ( definition ) }
9197 LANGUAGE ${ language }
@@ -95,7 +101,7 @@ export default class PostgresMetaFunctions {
95101 if ( error ) {
96102 return { data : null , error }
97103 }
98- return await this . retrieve ( { name, schema } )
104+ return await this . retrieve ( { name, schema, args } )
99105 }
100106
101107 async update (
@@ -127,7 +133,7 @@ export default class PostgresMetaFunctions {
127133 } ) SET SCHEMA ${ ident ( schema ) } ;`
128134 : ''
129135
130- const sql = `BEGIN;${ updateNameSql } ${ updateSchemaSql } COMMIT;`
136+ const sql = `BEGIN; ${ updateNameSql } ${ updateSchemaSql } COMMIT;`
131137
132138 const { error } = await this . query ( sql )
133139 if ( error ) {
@@ -145,7 +151,7 @@ export default class PostgresMetaFunctions {
145151 return { data : null , error }
146152 }
147153 const sql = `DROP FUNCTION ${ ident ( func ! . schema ) } .${ ident ( func ! . name ) }
148- ${ func ! . argument_types ? ` (${ func ! . argument_types } )` : '()' }
154+ (${ func ! . argument_types } )
149155 ${ cascade ? 'CASCADE' : 'RESTRICT' } ;`
150156 {
151157 const { error } = await this . query ( sql )
@@ -156,3 +162,12 @@ export default class PostgresMetaFunctions {
156162 return { data : func ! , error : null }
157163 }
158164}
165+
166+ const enrichedFunctionsSql = `
167+ WITH functions AS (
168+ ${ functionsSql }
169+ )
170+ SELECT
171+ *
172+ FROM functions
173+ `
0 commit comments