@@ -32,9 +32,11 @@ export default class PostgresMetaFunctions {
32
32
async retrieve ( {
33
33
id,
34
34
name,
35
+ schema = 'public' ,
35
36
} : {
36
37
id ?: number
37
38
name ?: string
39
+ schema ?: string
38
40
} ) : Promise < PostgresMetaResult < PostgresFunction > > {
39
41
if ( id ) {
40
42
const sql = `${ functionsSql } WHERE p.oid = ${ literal ( id ) } ;`
@@ -47,14 +49,16 @@ export default class PostgresMetaFunctions {
47
49
return { data : data [ 0 ] , error }
48
50
}
49
51
} else if ( name ) {
50
- const sql = `${ functionsSql } WHERE p.proname = ${ literal ( name ) } ;`
52
+ const sql = `${ functionsSql } WHERE p.proname = ${ literal ( name ) } AND n.nspname = ${ literal (
53
+ schema
54
+ ) } ;`
51
55
const { data, error } = await this . query ( sql )
52
56
if ( error ) {
53
57
return { data, error }
54
58
} else if ( data . length === 0 ) {
55
59
return {
56
60
data : null ,
57
- error : { message : `Cannot find a function named ${ name } ` } ,
61
+ error : { message : `Cannot find a function named ${ name } in schema ${ schema } ` } ,
58
62
}
59
63
} else {
60
64
return { data : data [ 0 ] , error }
@@ -80,10 +84,10 @@ export default class PostgresMetaFunctions {
80
84
language ?: string
81
85
} ) : Promise < PostgresMetaResult < PostgresFunction > > {
82
86
const sql = `
83
- CREATE FUNCTION ${ ident ( schema ) } .${ ident ( name ) }
87
+ CREATE OR REPLACE FUNCTION ${ ident ( schema ) } .${ ident ( name ) }
84
88
${ params && params . length ? `(${ params . join ( ',' ) } )` : '()' }
85
- RETURNS ${ rettype || 'void' }
86
- AS ' ${ definition } '
89
+ RETURNS ${ rettype }
90
+ AS ${ literal ( definition ) }
87
91
LANGUAGE ${ language }
88
92
RETURNS NULL ON NULL INPUT;
89
93
`
@@ -99,40 +103,25 @@ export default class PostgresMetaFunctions {
99
103
{
100
104
name,
101
105
schema = 'public' ,
102
- params,
103
- extension,
104
106
} : {
105
107
name : string
106
108
schema ?: string
107
- params ?: string [ ] //optional params for overloaded functions
108
- extension ?: string //e.g. sqrt DEPENDS ON EXTENSION mathlib
109
109
}
110
110
) : Promise < PostgresMetaResult < PostgresFunction > > {
111
111
const { data : old , error : retrieveError } = await this . retrieve ( { id } )
112
112
if ( retrieveError ) {
113
113
return { data : null , error : retrieveError }
114
114
}
115
115
116
- let alter = `ALTER FUNCTION ${ ident ( old ! . schema ) } .${ ident ( old ! . name ) } ${
117
- params && params . length ? `(${ params . join ( ',' ) } )` : ''
118
- } `
119
-
120
- let schemaSql = ''
121
- if ( schema !== undefined && schema !== old ! . schema ) {
122
- schemaSql = `${ alter } SET SCHEMA ${ ident ( schema ) } ;`
123
- alter = `ALTER FUNCTION ${ ident ( schema ) } .${ ident ( old ! . name ) } ${
124
- params && params . length ? `(${ params . join (
8000
9;,' ) } )` : ''
125
- } `
126
- }
127
-
116
+ let alter = `ALTER FUNCTION ${ ident ( old ! . name ) } `
128
117
const nameSql =
129
118
name === undefined || name == old ! . name ? '' : `${ alter } RENAME TO ${ ident ( name ) } ;`
130
119
131
- //Note: leaving out search_path and owner - should these be alterable from this api?
132
- //Note: also leaving out extensions - to enable extensions, they would have to already be
133
- //installed. Current Postgres docker image has no extensions installed
120
+ alter = `ALTER FUNCTION ${ ident ( name ) } `
121
+ const schemaSql =
122
+ schema === undefined || schema == old ! . schema ? '' : ` ${ alter } SET SCHEMA ${ ident ( schema ) } ;`
134
123
135
- const sql = `BEGIN;${ schemaSql } ${ nameSql } COMMIT;`
124
+ const sql = `BEGIN;${ nameSql } ${ schemaSql } COMMIT;`
136
125
137
126
const { error } = await this . query ( sql )
138
127
if ( error ) {
@@ -149,9 +138,9 @@ export default class PostgresMetaFunctions {
149
138
if ( error ) {
150
139
return { data : null , error }
151
140
}
152
- const sql = `DROP FUNCTION ${ ident ( func ! . schema ) } .${ ident ( func ! . name ) } ${
153
- cascade ? 'CASCADE' : 'RESTRICT'
154
- } ;`
141
+ const sql = `DROP FUNCTION ${ ident ( func ! . schema ) } .${ ident ( func ! . name ) }
142
+ ${ func ! . argument_types ? `( ${ func ! . argument_types } )` : '()' }
143
+ ${ cascade ? 'CASCADE' : 'RESTRICT' } ;`
155
144
{
156
145
const { error } = await this . query ( sql )
157
146
if ( error ) {
0 commit comments