Skip to content

Commit 44801f7

Browse files
committed
refactor: support return stmt sql body in create function in pg
1 parent 7e9fc48 commit 44801f7

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

pegjs/postgresql.pegjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -698,19 +698,20 @@ create_func_opt
698698
value,
699699
}
700700
}
701-
701+
/ return_stmt
702+
702703
create_function_stmt
703704
= a:KW_CREATE __
704705
or:(KW_OR __ KW_REPLACE)? __
705706
t:'FUNCTION'i __
706-
c:table_name __ LPAREN __ args:alter_func_args? __ RPAREN __
707+
c:proc_func_name __ LPAREN __ args:alter_func_args? __ RPAREN __
707708
r:func_returns? __
708709
fo:create_func_opt* __ SEMICOLON? __ {
709710
/*
710711
export type create_function_stmt_t = {
711712
type: 'create';
712713
replace?: string;
713-
name: { schema?: string; name: string };
714+
name: proc_func_name;
714715
args?: alter_func_args;
715716
returns?: func_returns;
716717
keyword: 'function';
@@ -725,7 +726,7 @@ create_function_stmt
725726
args: args || [],
726727
type: 'create',
727728
replace: or && 'or replace',
728-
name: { schema: c.db, name: c.table },
729+
name: c,
729730
returns: r,
730731
keyword: t && t.toLowerCase(),
731732
options: fo || [],
@@ -5281,6 +5282,7 @@ KW_DELETE = "DELETE"i !ident_start
52815282
KW_INSERT = "INSERT"i !ident_start
52825283
KW_RECURSIVE= "RECURSIVE" !ident_start { return 'RECURSIVE'; }
52835284
KW_REPLACE = "REPLACE"i !ident_start
5285+
KW_RETURN = 'RETURN'i !ident_start { return 'RETURN' }
52845286
KW_RETURNING = "RETURNING"i !ident_start { return 'RETURNING' }
52855287
KW_RENAME = "RENAME"i !ident_start
52865288
KW_IGNORE = "IGNORE"i !ident_start
@@ -5445,7 +5447,6 @@ KW_VAR_PRE_DOLLAR = '$'
54455447
KW_VAR_PRE_DOLLAR_DOUBLE = '$$'
54465448
KW_VAR_PRE
54475449
= KW_VAR__PRE_AT_AT / KW_VAR__PRE_AT / KW_VAR_PRE_DOLLAR / KW_VAR_PRE_DOLLAR
5448-
KW_RETURN = 'return'i
54495450
KW_ASSIGN = ':='
54505451
KW_DOUBLE_COLON = '::'
54515452
KW_ASSIGIN_EQUAL = '='
@@ -5575,7 +5576,6 @@ assign_stmt
55755576
};
55765577
}
55775578

5578-
55795579
return_stmt
55805580
= KW_RETURN __ e:proc_expr {
55815581
// => { type: 'return'; expr: proc_expr; }

src/create.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,16 @@ function createFunctionOptionToSQL(stmt) {
336336
return [toUpper(type), stmt.symbol, unionToSQL(stmt.declare), toUpper(stmt.begin), multipleToSQL(stmt.expr), toUpper(stmt.end), stmt.symbol].filter(hasVal).join(' ')
337337
case 'set':
338338
return [toUpper(type), stmt.parameter, toUpper(stmt.value && stmt.value.prefix), stmt.value && stmt.value.expr.map(exprToSQL).join(', ')].filter(hasVal).join(' ')
339+
case 'return':
340+
return [toUpper(type), exprToSQL(stmt.expr)].filter(hasVal).join(' ')
339341
default:
340342
return exprToSQL(stmt)
341343
}
342344
}
343345
function createFunctionToSQL(stmt) {
344346
const { type, replace, keyword, name, args, returns, options, last } = stmt
345347
const sql = [toUpper(type), toUpper(replace), toUpper(keyword)]
346-
const functionName = [identifierToSql(name.schema), name.name].filter(hasVal).join('.')
348+
const functionName = [literalToSQL(name.schema), name.name.map(literalToSQL).join('.')].filter(hasVal).join('.')
347349
const argsSQL = args.map(alterArgsToSQL).filter(hasVal).join(', ')
348350
sql.push(`${functionName}(${argsSQL})`, createFunctionReturnsToSQL(returns), options.map(createFunctionOptionToSQL).join(' '), last)
349351
return sql.filter(hasVal).join(' ')

test/postgres.spec.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@ describe('Postgres', () => {
11311131
ELSE $1 || ', ' || $2
11321132
END
11331133
$_$;`,
1134-
`CREATE FUNCTION "public"._group_concat(TEXT, TEXT) RETURNS TEXT LANGUAGE sql IMMUTABLE AS $_$ SELECT CASE WHEN $2 IS NULL THEN $1 WHEN $1 IS NULL THEN $2 ELSE $1 || ', ' || $2 END $_$`
1134+
`CREATE FUNCTION public._group_concat(TEXT, TEXT) RETURNS TEXT LANGUAGE sql IMMUTABLE AS $_$ SELECT CASE WHEN $2 IS NULL THEN $1 WHEN $1 IS NULL THEN $2 ELSE $1 || ', ' || $2 END $_$`
11351135
]
11361136
},
11371137
{
@@ -1146,7 +1146,7 @@ describe('Postgres', () => {
11461146
AND store_id = $2
11471147
AND NOT inventory_in_stock(inventory_id);
11481148
$_$;`,
1149-
`CREATE FUNCTION "public".film_not_in_stock(p_film_id INTEGER DEFAULT 1, p_store_id INTEGER = 1, OUT p_film_count INTEGER) RETURNS SETOF INTEGER LANGUAGE sql AS $_$ SELECT inventory_id FROM "inventory" WHERE film_id = $1 AND store_id = $2 AND NOT inventory_in_stock(inventory_id) $_$`
1149+
`CREATE FUNCTION public.film_not_in_stock(p_film_id INTEGER DEFAULT 1, p_store_id INTEGER = 1, OUT p_film_count INTEGER) RETURNS SETOF INTEGER LANGUAGE sql AS $_$ SELECT inventory_id FROM "inventory" WHERE film_id = $1 AND store_id = $2 AND NOT inventory_in_stock(inventory_id) $_$`
11501150
]
11511151
},
11521152
{
@@ -1182,7 +1182,7 @@ describe('Postgres', () => {
11821182
{
11831183
title: 'create function with if else stmt',
11841184
sql: [
1185-
`CREATE FUNCTION public.inventory_in_stock(p_inventory_id integer) RETURNS boolean
1185+
`CREATE FUNCTION "public".inventory_in_stock(p_inventory_id integer) RETURNS boolean
11861186
LANGUAGE plpgsql
11871187
AS $$
11881188
DECLARE
@@ -1220,7 +1220,7 @@ describe('Postgres', () => {
12201220
{
12211221
title: 'create function without args',
12221222
sql: [
1223-
`CREATE FUNCTION public.last_updated() RETURNS trigger
1223+
`CREATE FUNCTION "public".last_updated() RETURNS trigger
12241224
LANGUAGE plpgsql
12251225
AS $$
12261226
BEGIN
@@ -1230,6 +1230,17 @@ describe('Postgres', () => {
12301230
'CREATE FUNCTION "public".last_updated() RETURNS "trigger" LANGUAGE plpgsql AS $$ BEGIN NEW.last_update = CURRENT_TIMESTAMP ; RETURN NEW END $$'
12311231
]
12321232
},
1233+
{
1234+
title: 'create function with sql body',
1235+
sql: [
1236+
`CREATE FUNCTION add(a integer, b integer) RETURNS integer
1237+
LANGUAGE SQL
1238+
IMMUTABLE
1239+
RETURNS NULL ON NULL INPUT
1240+
RETURN a + b;`,
1241+
'CREATE FUNCTION add(a INTEGER, b INTEGER) RETURNS INTEGER LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT RETURN a + b'
1242+
]
1243+
},
12331244
{
12341245
title: 'create aggregate',
12351246
sql: [

0 commit comments

Comments
 (0)