1
1
import { ident , literal } from 'pg-format'
2
2
import { DEFAULT_SYSTEM_SCHEMAS } from './constants'
3
3
import { functionsSql } from './sql'
4
- import { PostgresMetaResult , PostgresFunction } from './types'
5
-
6
- type FunctionInputs = {
7
- name : string
8
- definition : string
9
- args ?: string [ ]
10
- behavior ?: 'IMMUTABLE' | 'STABLE' | 'VOLATILE'
11
- config_params ?: { [ key : string ] : string }
12
- schema ?: string
13
- language ?: string
14
- return_type ?: string
15
- security_definer ?: boolean
16
- }
4
+ import { PostgresMetaResult , PostgresFunction , PostgresFunctionCreate } from './types'
17
5
18
6
export default class PostgresMetaFunctions {
19
7
query : ( sql : string ) => Promise < PostgresMetaResult < any > >
@@ -94,7 +82,7 @@ export default class PostgresMetaFunctions {
94
82
behavior = 'VOLATILE' ,
95
83
security_definer = false ,
96
84
config_params = { } ,
97
- } : FunctionInputs ) : Promise < PostgresMetaResult < PostgresFunction > > {
85
+ } : PostgresFunctionCreate ) : Promise < PostgresMetaResult < PostgresFunction > > {
98
86
const sql = this . generateCreateFunctionSql ( {
99
87
name,
100
88
schema,
@@ -130,19 +118,26 @@ export default class PostgresMetaFunctions {
130
118
return { data : null , error }
131
119
}
132
120
133
- const updateDefinitionSql = typeof definition === 'string' ? this . generateCreateFunctionSql (
134
- { ...currentFunc ! , definition } ,
135
- { replace : true }
136
- ) : ''
121
+ const args = currentFunc ! . argument_types . split ( ', ' )
137
122
138
- const retrieveFunctionSql = this . generateRetrieveFunctionSql (
139
- {
140
- schema : currentFunc ! . schema ,
141
- name : currentFunc ! . name ,
142
- args : currentFunc ! . argument_types . split ( ', ' ) ,
143
- } ,
144
- { terminateCommand : false }
145
- )
123
+ const updateDefinitionSql =
124
+ typeof definition === 'string'
125
+ ? this . generateCreateFunctionSql (
126
+ {
127
+ ...currentFunc ! ,
128
+ definition,
129
+ args,
130
+ config_params : currentFunc ! . config_params ?? { } ,
131
+ } ,
132
+ { replace : true }
133
+ )
134
+ : ''
135
+
136
+ const retrieveFunctionSql = this . generateRetrieveFunctionSql ( {
137
+ schema : currentFunc ! . schema ,
138
+ name : currentFunc ! . name ,
139
+ args,
140
+ } )
146
141
147
142
const updateNameSql =
148
143
name && name !== currentFunc ! . name
@@ -218,19 +213,18 @@ export default class PostgresMetaFunctions {
218
213
name,
219
214
schema,
220
215
args,
221
- argument_types,
222
216
definition,
223
217
return_type,
224
218
language,
225
219
behavior,
226
220
security_definer,
227
221
config_params,
228
- } : Partial < Omit < FunctionInputs , 'config_params' > & PostgresFunction > ,
229
- { replace = false , terminateCommand = true } = { }
222
+ } : PostgresFunctionCreate ,
223
+ { replace = false } = { }
230
224
) : string {
231
225
return `
232
226
CREATE ${ replace ? 'OR REPLACE' : '' } FUNCTION ${ ident ( schema ! ) } .${ ident ( name ! ) } (${
233
- argument_types || args ?. join ( ', ' ) || ''
227
+ args ?. join ( ', ' ) || ''
234
228
} )
235
229
RETURNS ${ return_type }
236
230
AS ${ literal ( definition ) }
@@ -247,23 +241,19 @@ export default class PostgresMetaFunctions {
247
241
)
248
242
. join ( '\n' )
249
243
: ''
250
- }
251
- ${ terminateCommand ? ';' : '' }
244
+ } ;
252
245
`
253
246
}
254
247
255
- private generateRetrieveFunctionSql (
256
- {
257
- schema,
258
- name,
259
- args,
260
- } : {
261
- schema : string
262
- name : string
263
- args : string [ ]
264
- } ,
265
- { terminateCommand = true } = { }
266
- ) : string {
248
+ private generateRetrieveFunctionSql ( {
249
+ schema,
250
+ name,
251
+ args,
252
+ } : {
253
+ schema : string
254
+ name : string
255
+ args : string [ ]
256
+ } ) : string {
267
257
return `${ enrichedFunctionsSql } JOIN pg_proc AS p ON id = p.oid WHERE schema = ${ literal (
268
258
schema
269
259
) } AND name = ${ literal ( name ) } AND p.proargtypes::text = ${
@@ -286,7 +276,7 @@ export default class PostgresMetaFunctions {
286
276
) AS arr
287
277
) AS split_args
288
278
) args
289
- ) ${ terminateCommand ? ';' : '' } `
279
+ )`
290
280
: literal ( '' )
291
281
} `
292
282
}
0 commit comments