Skip to content

Commit

Permalink
Support FUNC/PROC default parameter values in PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Jan 26, 2024
1 parent 20c2470 commit 17a2f95
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/cst/Function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { BlockStmt } from "./ProceduralLanguage";
export type AllFunctionNodes =
| AllFunctionStatements
| FunctionParam
| FunctionParamDefault
| ReturnClause;

export type AllFunctionStatements = CreateFunctionStmt | DropFunctionStmt;
Expand All @@ -39,6 +40,13 @@ export interface FunctionParam extends BaseNode {
mode?: Keyword<"IN" | "OUT" | "INOUT" | "VARIADIC">;
name?: Identifier;
dataType: DataType;
default?: FunctionParamDefault;
}

export interface FunctionParamDefault extends BaseNode {
type: "function_param_default";
operator: Keyword<"DEFAULT"> | "=";
expr: Expr;
}

type CreateFunctionClause =
Expand Down
15 changes: 13 additions & 2 deletions src/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2930,19 +2930,30 @@ procedure_param
dataType: type,
});
}
/ &postgres mode:((INOUT / IN / OUT / VARIADIC) __)? name:(ident __) type:data_type {
/ &postgres mode:((INOUT / IN / OUT / VARIADIC) __)? name:(ident __) type:data_type def:(__ function_param_default)? {
return loc({
type: "function_param",
mode: read(mode),
name: read(name),
dataType: type,
default: read(def),
});
}
/ &postgres mode:((INOUT / IN / OUT / VARIADIC) __)? type:data_type {
/ &postgres mode:((INOUT / IN / OUT / VARIADIC) __)? type:data_type def:(__ function_param_default)? {
return loc({
type: "function_param",
mode: read(mode),
dataType: type,
default: read(def),
});
}

function_param_default
= operator:(DEFAULT / "=") expr:(__ expr) {
return loc({
type: "function_param_default",
operator,
expr: read(expr),
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/showNode/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const functionMap: FullTransformMap<string, AllFunctionNodes> = {
node.params,
node.clauses,
]),
function_param: (node) => show([node.mode, node.name, node.dataType]),
function_param: (node) =>
show([node.mode, node.name, node.dataType, node.default]),
function_param_default: (node) => show([node.operator, node.expr]),
return_clause: (node) => show([node.returnKw, node.expr]),
drop_function_stmt: (node) =>
show([
Expand Down
4 changes: 4 additions & 0 deletions test/ddl/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ describe("function", () => {
AS $$ SELECT ARRAY[$1, $2]; $$
`);
});

it("supports default values", () => {
testWc(`CREATE FUNCTION foo(IN x INT = 1, y INT DEFAULT 2, INT = 3) ${body}`);
});
});

it("supports OR REPLACE", () => {
Expand Down
4 changes: 4 additions & 0 deletions test/ddl/procedure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ describe("procedure", () => {
AS $$ SELECT $1, $2; $$
`);
});

it("supports default values", () => {
testWc(`CREATE PROCEDURE foo(x INT = 1, OUT y INT DEFAULT 2) ${BEGIN} SELECT 1; END`);
});
});

it("supports OR REPLACE", () => {
Expand Down

0 comments on commit 17a2f95

Please sign in to comment.