1
1
import { ident , literal } from 'pg-format'
2
2
import { DEFAULT_SYSTEM_SCHEMAS } from './constants.js'
3
- import { filterByList } from './helpers.js'
4
- import { functionsSql } from './sql/index.js'
3
+ import { filterByList , filterByValue } from './helpers.js'
5
4
import { PostgresMetaResult , PostgresFunction , PostgresFunctionCreate } from './types.js'
5
+ import { FUNCTIONS_SQL } from './sql/index.js'
6
6
7
7
export default class PostgresMetaFunctions {
8
8
query : ( sql : string ) => Promise < PostgresMetaResult < any > >
@@ -24,21 +24,12 @@ export default class PostgresMetaFunctions {
24
24
limit ?: number
25
25
offset ?: number
26
26
} = { } ) : Promise < PostgresMetaResult < PostgresFunction [ ] > > {
27
- let sql = enrichedFunctionsSql
28
- const filter = filterByList (
27
+ const schemaFilter = filterByList (
29
28
includedSchemas ,
30
29
excludedSchemas ,
31
30
! includeSystemSchemas ? DEFAULT_SYSTEM_SCHEMAS : undefined
32
31
)
33
- if ( filter ) {
34
- sql += ` WHERE schema ${ filter } `
35
- }
36
- if ( limit ) {
37
- sql = `${ sql } LIMIT ${ limit } `
38
- }
39
- if ( offset ) {
40
- sql = `${ sql } OFFSET ${ offset } `
41
- }
32
+ let sql = FUNCTIONS_SQL ( { schemaFilter, limit, offset } )
42
33
return await this . query ( sql )
43
34
}
44
35
@@ -63,8 +54,10 @@ export default class PostgresMetaFunctions {
63
54
schema ?: string
64
55
args ?: string [ ]
65
56
} ) : Promise < PostgresMetaResult < PostgresFunction > > {
57
+ const schemaFilter = schema ? filterByList ( [ schema ] , [ ] ) : undefined
66
58
if ( id ) {
67
- const sql = `${ enrichedFunctionsSql } WHERE id = ${ literal ( id ) } ;`
59
+ const idsFilter = filterByValue ( [ `${ id } ` ] )
60
+ const sql = FUNCTIONS_SQL ( { idsFilter } )
68
61
const { data, error } = await this . query ( sql )
69
62
if ( error ) {
70
63
return { data, error }
@@ -74,7 +67,8 @@ export default class PostgresMetaFunctions {
74
67
return { data : data [ 0 ] , error }
75
68
}
76
69
} else if ( name && schema && args ) {
77
- const sql = this . generateRetrieveFunctionSql ( { name, schema, args } )
70
+ const nameFilter = filterByValue ( [ name ] )
71
+ const sql = this . generateRetrieveFunctionSql ( { schemaFilter, nameFilter, schema, name, args } )
78
72
const { data, error } = await this . query ( sql )
79
73
if ( error ) {
80
74
return { data, error }
@@ -177,7 +171,7 @@ export default class PostgresMetaFunctions {
177
171
178
172
IF (
179
173
SELECT id
180
- FROM (${ functionsSql } ) AS f
174
+ FROM (${ FUNCTIONS_SQL ( { } ) } ) AS f
181
175
WHERE f.schema = ${ literal ( currentFunc ! . schema ) }
182
176
AND f.name = ${ literal ( currentFunc ! . name ) }
183
177
AND f.identity_argument_types = ${ literal ( identityArgs ) }
@@ -264,17 +258,17 @@ export default class PostgresMetaFunctions {
264
258
}
265
259
266
260
private generateRetrieveFunctionSql ( {
267
- schema ,
268
- name ,
261
+ schemaFilter ,
262
+ nameFilter ,
269
263
args,
270
264
} : {
265
+ schemaFilter ?: string
266
+ nameFilter ?: string
271
267
schema : string
272
268
name : string
273
269
args : string [ ]
274
270
} ) : string {
275
- return `${ enrichedFunctionsSql } JOIN pg_proc AS p ON id = p.oid WHERE schema = ${ literal (
276
- schema
277
- ) } AND name = ${ literal ( name ) } AND p.proargtypes::text = ${
271
+ return `${ FUNCTIONS_SQL ( { schemaFilter, nameFilter } ) } JOIN pg_proc AS p ON f.oid = p.oid WHERE p.proargtypes::text = ${
278
272
args . length
279
273
? `(
280
274
SELECT STRING_AGG(type_oid::text, ' ') FROM (
@@ -299,12 +293,3 @@ export default class PostgresMetaFunctions {
299
293
} `
300
294
}
301
295
}
302
-
303
- const enrichedFunctionsSql = `
304
- WITH f AS (
305
- ${ functionsSql }
306
- )
307
- SELECT
308
- f.*
309
- FROM f
310
- `
0 commit comments