Skip to content

Commit ae4798e

Browse files
committed
feat: add functions args retrieval
1 parent 7d532a3 commit ae4798e

File tree

6 files changed

+39
-43
lines changed

6 files changed

+39
-43
lines changed

schema-filter-demo.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/lib/PostgresMetaColumnPrivileges.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ end $$;
7474
// Return the updated column privileges for modified columns.
7575
const columnIds = [...new Set(grants.map(({ column_id }) => column_id))]
7676
const columnIdsFilter = filterByValue(columnIds)
77-
sql = COLUMN_PRIVILEGES_SQL({ schemaFilter: undefined, columnIdsFilter })
77+
sql = COLUMN_PRIVILEGES_SQL({ columnIdsFilter })
7878
return await this.query(sql)
7979
}
8080

@@ -114,7 +114,7 @@ end $$;
114114
// Return the updated column privileges for modified columns.
115115
const columnIds = [...new Set(revokes.map(({ column_id }) => column_id))]
116116
const columnIdsFilter = filterByValue(columnIds)
117-
sql = COLUMN_PRIVILEGES_SQL({ schemaFilter: undefined, columnIdsFilter })
117+
sql = COLUMN_PRIVILEGES_SQL({ columnIdsFilter })
118118
return await this.query(sql)
119119
}
120120
}

src/lib/PostgresMetaExtensions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default class PostgresMetaExtensions {
2222
}
2323

2424
async retrieve({ name }: { name: string }): Promise<PostgresMetaResult<PostgresExtension>> {
25-
const nameFilter = filterByValue([`${name}`])
25+
const nameFilter = filterByValue([name])
2626
const sql = EXTENSIONS_SQL({ nameFilter })
2727
const { data, error } = await this.query(sql)
2828
if (error) {

src/lib/PostgresMetaFunctions.ts

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export default class PostgresMetaFunctions {
6868
}
6969
} else if (name && schema && args) {
7070
const nameFilter = filterByValue([name])
71-
const sql = this.generateRetrieveFunctionSql({ schemaFilter, nameFilter, schema, name, args })
71+
const sql = FUNCTIONS_SQL({ schemaFilter, nameFilter, args: args.map(literal) })
7272
const { data, error } = await this.query(sql)
7373
if (error) {
7474
return { data, error }
@@ -163,6 +163,11 @@ export default class PostgresMetaFunctions {
163163
)}(${identityArgs}) SET SCHEMA ${ident(schema)};`
164164
: ''
165165

166+
const currentSchemaFilter = currentFunc!.schema
167+
? filterByList([currentFunc!.schema], [])
168+
: undefined
169+
const currentNameFilter = currentFunc!.name ? filterByValue([currentFunc!.name]) : undefined
170+
166171
const sql = `
167172
DO LANGUAGE plpgsql $$
168173
BEGIN
@@ -171,7 +176,7 @@ export default class PostgresMetaFunctions {
171176
172177
IF (
173178
SELECT id
174-
FROM (${FUNCTIONS_SQL({})}) AS f
179+
FROM (${FUNCTIONS_SQL({ schemaFilter: currentSchemaFilter, nameFilter: currentNameFilter })}) AS f
175180
WHERE f.schema = ${literal(currentFunc!.schema)}
176181
AND f.name = ${literal(currentFunc!.name)}
177182
AND f.identity_argument_types = ${literal(identityArgs)}
@@ -256,40 +261,4 @@ export default class PostgresMetaFunctions {
256261
};
257262
`
258263
}
259-
260-
private generateRetrieveFunctionSql({
261-
schemaFilter,
262-
nameFilter,
263-
args,
264-
}: {
265-
schemaFilter?: string
266-
nameFilter?: string
267-
schema: string
268-
name: string
269-
args: string[]
270-
}): string {
271-
return `${FUNCTIONS_SQL({ schemaFilter, nameFilter })} JOIN pg_proc AS p ON f.oid = p.oid WHERE p.proargtypes::text = ${
272-
args.length
273-
? `(
274-
SELECT STRING_AGG(type_oid::text, ' ') FROM (
275-
SELECT (
276-
split_args.arr[
277-
array_length(
278-
split_args.arr,
279-
1
280-
)
281-
]::regtype::oid
282-
) AS type_oid FROM (
283-
SELECT STRING_TO_ARRAY(
284-
UNNEST(
285-
ARRAY[${args.map(literal)}]
286-
),
287-
' '
288-
) AS arr
289-
) AS split_args
290-
) args
291-
)`
292-
: literal('')
293-
}`
294-
}
295264
}

src/lib/PostgresMetaSchemas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default class PostgresMetaSchemas {
5959
return { data: data[0], error }
6060
}
6161
} else if (name) {
62-
const nameFilter = filterByValue([`${name}`])
62+
const nameFilter = filterByValue([name])
6363
const sql = SCHEMAS_SQL({ nameFilter })
6464
const { data, error } = await this.query(sql)
6565
if (error) {

src/lib/sql/functions.sql.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { SQLQueryPropsWithSchemaFilterAndIdsFilter } from './common.js'
33
export const FUNCTIONS_SQL = (
44
props: SQLQueryPropsWithSchemaFilterAndIdsFilter & {
55
nameFilter?: string
6+
args?: string[]
67
}
78
) => /* SQL */ `
89
-- CTE with sane arg_modes, arg_names, and arg_types.
@@ -33,6 +34,33 @@ with functions as (
3334
${props.schemaFilter ? `n.nspname ${props.schemaFilter} AND` : ''}
3435
${props.idsFilter ? `p.oid ${props.idsFilter} AND` : ''}
3536
${props.nameFilter ? `p.proname ${props.nameFilter} AND` : ''}
37+
${
38+
props.args && props.args.length > 0
39+
? `p.proargtypes::text = ${
40+
props.args.length
41+
? `(
42+
SELECT STRING_AGG(type_oid::text, ' ') FROM (
43+
SELECT (
44+
split_args.arr[
45+
array_length(
46+
split_args.arr,
47+
1
48+
)
49+
]::regtype::oid
50+
) AS type_oid FROM (
51+
SELECT STRING_TO_ARRAY(
52+
UNNEST(
53+
ARRAY[${props.args}]
54+
),
55+
' '
56+
) AS arr
57+
) AS split_args
58+
) args
59+
)`
60+
: "''"
61+
} AND`
62+
: ''
63+
}
3664
p.prokind = 'f'
3765
)
3866
select

0 commit comments

Comments
 (0)