Skip to content

Commit

Permalink
Format full PostgreSQL func/proc parameter syntax
Browse files Browse the repository at this point in the history
Also BEGIN ATOMIC .. END
  • Loading branch information
nene committed Jan 27, 2024
1 parent 27b83ae commit 46be069
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/syntax/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export const functionMap: Partial<CstToDocMap<AllFunctionNodes>> = {
),
],
],
function_param: (print) => print.spaced(["mode", "name", "dataType"]),
function_param: (print) =>
print.spaced(["mode", "name", "dataType", "default"]),
function_param_default: (print) => print.spaced(["operator", "expr"]),
drop_function_stmt: (print) =>
group([
print.spaced(["dropKw", "tableKw", "functionKw", "ifExistsKw", "name"]),
Expand Down
2 changes: 1 addition & 1 deletion src/syntax/procedural_language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { CstToDocMap } from "../CstToDocMap";
export const proceduralLanguageMap: CstToDocMap<AllProceduralNodes> = {
// BEGIN .. END
block_stmt: (print, node) => [
print("beginKw"),
print.spaced(["beginKw", "atomicKw"]),
indent([hardline, stripTrailingHardline(print("program"))]),
node.exception ? [hardline, print("exception")] : [],
hardline,
Expand Down
13 changes: 7 additions & 6 deletions test/ddl/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ describe("function", () => {
});

it(`formats long parameter list to multiple lines`, async () => {
await testBigquery(dedent`
await testPostgresql(dedent`
CREATE FUNCTION my_func(
first_name STRING,
last_name STRING,
year_of_birth INT64
) AS
(SELECT * FROM client)
IN first_name TEXT,
OUT last_name TEXT,
year_of_birth INT DEFAULT 2000,
INOUT age INT = 0,
VARIADIC other_names TEXT[]
) AS 'SELECT 1'
`);
});

Expand Down
11 changes: 11 additions & 0 deletions test/ddl/procedure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ describe("procedure", () => {
);
});

it(`formats default parameter values`, async () => {
await testPostgresql(
dedent`
CREATE PROCEDURE eliminate_tbl(id INT = 1, TEXT DEFAULT 'foo')
BEGIN ATOMIC
DROP TABLE my_table;
END
`,
);
});

it(`formats OPTIONS (..)`, async () => {
await testBigquery(
dedent`
Expand Down

0 comments on commit 46be069

Please sign in to comment.