11import { ident , literal } from 'pg-format'
22import { DEFAULT_SYSTEM_SCHEMAS } from './constants'
33import { 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'
175
186export default class PostgresMetaFunctions {
197 query : ( sql : string ) => Promise < PostgresMetaResult < any > >
@@ -94,7 +82,7 @@ export default class PostgresMetaFunctions {
9482 behavior = 'VOLATILE' ,
9583 security_definer = false ,
9684 config_params = { } ,
97- } : FunctionInputs ) : Promise < PostgresMetaResult < PostgresFunction > > {
85+ } : PostgresFunctionCreate ) : Promise < PostgresMetaResult < PostgresFunction > > {
9886 const sql = this . generateCreateFunctionSql ( {
9987 name,
10088 schema,
@@ -130,19 +118,26 @@ export default class PostgresMetaFunctions {
130118 return { data : null , error }
131119 }
132120
133- const updateDefinitionSql = typeof definition === 'string' ? this . generateCreateFunctionSql (
134- { ...currentFunc ! , definition } ,
135- { replace : true }
136- ) : ''
121+ const args = currentFunc ! . argument_types . split ( ', ' )
137122
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+ } )
146141
147142 const updateNameSql =
148143 name && name !== currentFunc ! . name
@@ -218,19 +213,18 @@ export default class PostgresMetaFunctions {
218213 name,
219214 schema,
220215 args,
221- argument_types,
222216 definition,
223217 return_type,
224218 language,
225219 behavior,
226220 security_definer,
227221 config_params,
228- } : Partial < Omit < FunctionInputs , 'config_params' > & PostgresFunction > ,
229- { replace = false , terminateCommand = true } = { }
222+ } : PostgresFunctionCreate ,
223+ { replace = false } = { }
230224 ) : string {
231225 return `
232226 CREATE ${ replace ? 'OR REPLACE' : '' } FUNCTION ${ ident ( schema ! ) } .${ ident ( name ! ) } (${
233- argument_types || args ?. join ( ', ' ) || ''
227+ args ?. join ( ', ' ) || ''
234228 } )
235229 RETURNS ${ return_type }
236230 AS ${ literal ( definition ) }
@@ -247,23 +241,19 @@ export default class PostgresMetaFunctions {
247241 )
248242 . join ( '\n' )
249243 : ''
250- }
251- ${ terminateCommand ? ';' : '' }
244+ } ;
252245 `
253246 }
254247
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 {
267257 return `${ enrichedFunctionsSql } JOIN pg_proc AS p ON id = p.oid WHERE schema = ${ literal (
268258 schema
269259 ) } AND name = ${ literal ( name ) } AND p.proargtypes::text = ${
@@ -286,7 +276,7 @@ export default class PostgresMetaFunctions {
286276 ) AS arr
287277 ) AS split_args
288278 ) args
289- ) ${ terminateCommand ? ';' : '' } `
279+ )`
290280 : literal ( '' )
291281 } `
292282 }
0 commit comments