Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/lib/PostgresMetaFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import { filterByList, filterByValue } from './helpers.js'
import { PostgresMetaResult, PostgresFunction, PostgresFunctionCreate } from './types.js'
import { FUNCTIONS_SQL } from './sql/functions.sql.js'

// GUC_LIST_INPUT parameters (e.g. search_path) expect a comma-separated list of
// individually-quoted values. Scalar GUCs (e.g. statement_timeout = 5s) must be
// a single quoted literal — unquoted `TO 5s` is a syntax error.
const literalConfigValue = (value: string): string =>
value
.split(',')
.map((part) => literal(part.trim()))
.join(', ')

export default class PostgresMetaFunctions {
query: (sql: string) => Promise<PostgresMetaResult<any>>

Expand Down Expand Up @@ -252,9 +261,10 @@ export default class PostgresMetaFunctions {
${
config_params
? Object.entries(config_params)
.map(
([param, value]: string[]) =>
`SET ${param} ${value[0] === 'FROM CURRENT' ? 'FROM CURRENT' : 'TO ' + value}`
.map(([param, value]: string[]) =>
value === 'FROM CURRENT'
? `SET ${ident(param)} FROM CURRENT`
: `SET ${ident(param)} TO ${literalConfigValue(value)}`
)
.join('\n')
: ''
Expand Down
20 changes: 20 additions & 0 deletions test/lib/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,23 @@ test('retrieve function by args filter - function with no arguments', async () =
})
expect(res.error).toBeNull()
})

test('create function with statement_timeout config_params', async () => {
// Unquoted `SET statement_timeout TO 5s` is invalid SQL; values must be literals.
const res = await pgMeta.functions.create({
name: 'test_timeout_func',
schema: 'public',
args: [],
definition: 'select 1',
return_type: 'integer',
language: 'sql',
behavior: 'VOLATILE',
security_definer: false,
config_params: { statement_timeout: '5s' },
})
expect(res.error).toBeNull()
expect(res.data?.config_params).toMatchObject({ statement_timeout: '5s' })
expect(res.data?.complete_statement).toContain(`SET statement_timeout TO '5s'`)

await pgMeta.functions.remove(res.data!.id)
})