Skip to content
Merged
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
2 changes: 2 additions & 0 deletions __fixtures__/plpgsql-generated/generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@
"plpgsql_deparser_fixes-68.sql": "CREATE FUNCTION test_multiline_literals(x boolean) RETURNS text\nLANGUAGE plpgsql AS $$\nDECLARE\n v text := 'declared\ndefault';\nBEGIN\n IF x THEN\n FOR i IN 1..2 LOOP\n RAISE NOTICE 'line one\nline two';\n PERFORM length('a\nb');\n v := 'c\nd' || E'tab\\there\nquote\\'s';\n EXECUTE $q$SELECT 'e\nf'$q$;\n BEGIN\n INSERT INTO t (memo) VALUES ('g\nh') ON CONFLICT (memo) DO UPDATE SET memo = 'i\nj';\n EXCEPTION WHEN others THEN\n RAISE EXCEPTION 'failed:\n%', sqlerrm;\n END;\n END LOOP;\n ELSIF v = 'k\nl' THEN\n CASE v WHEN 'm\nn' THEN\n RETURN 'o\np';\n ELSE\n NULL;\n END CASE;\n END IF;\n RETURN v;\nEND$$",
"plpgsql_deparser_fixes-69.sql": "DO $do$\nDECLARE\n r record;\nBEGIN\n FOR r IN SELECT \"odd\ncolumn\" AS c FROM t WHERE memo = $s$dollar\nquoted$s$ LOOP\n WHILE r.c IS NOT NULL LOOP\n RAISE NOTICE '%', r.c || '\nsuffix';\n EXIT;\n END LOOP;\n END LOOP;\nEND$do$",
"plpgsql_deparser_fixes-70.sql": "CREATE FUNCTION test_searched_case_in(x int) RETURNS text\nLANGUAGE plpgsql AS $$\nBEGIN\n CASE\n WHEN x IN (1, 2) THEN\n RETURN 'low';\n WHEN x IN (\n 3, 4) THEN\n RETURN 'mid';\n ELSE\n RETURN 'high';\n END CASE;\nEND$$",
"plpgsql_deparser_fixes-71.sql": "CREATE FUNCTION test_internal_char() RETURNS \"char\"\nLANGUAGE plpgsql AS $$\nDECLARE\n c \"char\" := 'a';\n chars \"char\"[] := ARRAY['a'::\"char\"];\n ordinary char := 'b';\nBEGIN\n RETURN c;\nEND$$",
"plpgsql_deparser_fixes-72.sql": "CREATE FUNCTION test_quoted_types() RETURNS void\nLANGUAGE plpgsql AS $$\nDECLARE\n keyword_type \"end\";\n spaced_type \"has space\";\n mixed_type \"MixedCase\";\n escaped_type \"has\"\"quote\";\n dotted_type \"has.dot\";\n array_type \"MixedCase\"[][];\n ordinary_type simple;\nBEGIN\n NULL;\nEND$$",
"plpgsql_control-1.sql": "do $$\nbegin\n -- basic case\n for i in 1..3 loop\n raise notice '1..3: i = %', i;\n end loop;\n -- with BY, end matches exactly\n for i in 1..10 by 3 loop\n raise notice '1..10 by 3: i = %', i;\n end loop;\n -- with BY, end does not match\n for i in 1..11 by 3 loop\n raise notice '1..11 by 3: i = %', i;\n end loop;\n -- zero iterations\n for i in 1..0 by 3 loop\n raise notice '1..0 by 3: i = %', i;\n end loop;\n -- REVERSE\n for i in reverse 10..0 by 3 loop\n raise notice 'reverse 10..0 by 3: i = %', i;\n end loop;\n -- potential overflow\n for i in 2147483620..2147483647 by 10 loop\n raise notice '2147483620..2147483647 by 10: i = %', i;\n end loop;\n -- potential overflow, reverse direction\n for i in reverse -2147483620..-2147483647 by 10 loop\n raise notice 'reverse -2147483620..-2147483647 by 10: i = %', i;\n end loop;\nend$$",
"plpgsql_control-2.sql": "do $$\nbegin\n for i in 1..3 by 0 loop\n raise notice '1..3 by 0: i = %', i;\n end loop;\nend$$",
"plpgsql_control-3.sql": "do $$\nbegin\n for i in 1..3 by -1 loop\n raise notice '1..3 by -1: i = %', i;\n end loop;\nend$$",
Expand Down
26 changes: 26 additions & 0 deletions __fixtures__/plpgsql/plpgsql_deparser_fixes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -908,3 +908,29 @@ BEGIN
RETURN 'high';
END CASE;
END$$;

-- Test 71: internal "char" declarations retain their distinct type
CREATE FUNCTION test_internal_char() RETURNS "char"
LANGUAGE plpgsql AS $$
DECLARE
c "char" := 'a';
chars "char"[] := ARRAY['a'::"char"];
ordinary char := 'b';
BEGIN
RETURN c;
END$$;

-- Test 72: simple type identifiers retain required quoting
CREATE FUNCTION test_quoted_types() RETURNS void
LANGUAGE plpgsql AS $$
DECLARE
keyword_type "end";
spaced_type "has space";
mixed_type "MixedCase";
escaped_type "has""quote";
dotted_type "has.dot";
array_type "MixedCase"[][];
ordinary_type simple;
BEGIN
NULL;
END$$;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`preserves internal char and char array declarations 1`] = `
"DECLARE
c "char" := 'a';
chars "char"[] := ARRAY['a'::"char"];
ordinary char := 'b';
BEGIN
RETURN c;
END"
`;

exports[`preserves required quoting in simple type declarations 1`] = `
"DECLARE
keyword_type "end";
spaced_type "has space";
mixed_type "MixedCase";
escaped_type "has""quote";
dotted_type "has.dot";
array_type "MixedCase"[][];
ordinary_type simple;
BEGIN
RETURN;
END"
`;
36 changes: 36 additions & 0 deletions packages/plpgsql-deparser/__tests__/quoted-char.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { loadModule, parsePlPgSQLSync } from 'libpg-query';

import { deparseSync, PLpgSQLParseResult } from '../src';
import { PLpgSQLTestUtils } from '../test-utils';

const fixtures = require('../../../__fixtures__/plpgsql-generated/generated.json');

beforeAll(async () => {
await loadModule();
});

it('preserves internal char and char array declarations', async () => {
const sql = fixtures['plpgsql_deparser_fixes-71.sql'];
await new PLpgSQLTestUtils().expectAstMatch('quoted char', sql);
const parsed = parsePlPgSQLSync(sql) as unknown as PLpgSQLParseResult;
const output = deparseSync(parsed);
expect(output).toContain('c "char"');
expect(output).toContain('chars "char"[]');
expect(output).not.toContain('ordinary "char"');
expect(output).toMatchSnapshot();
});

it('preserves required quoting in simple type declarations', async () => {
const sql = fixtures['plpgsql_deparser_fixes-72.sql'];
await new PLpgSQLTestUtils().expectAstMatch('quoted simple types', sql);
const parsed = parsePlPgSQLSync(sql) as unknown as PLpgSQLParseResult;
const output = deparseSync(parsed);
expect(output).toContain('keyword_type "end"');
expect(output).toContain('spaced_type "has space"');
expect(output).toContain('mixed_type "MixedCase"');
expect(output).toContain('escaped_type "has""quote"');
expect(output).toContain('dotted_type "has.dot"');
expect(output).toContain('array_type "MixedCase"[][]');
expect(output).toContain('ordinary_type simple');
expect(output).toMatchSnapshot();
});
9 changes: 7 additions & 2 deletions packages/plpgsql-deparser/src/plpgsql-deparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -883,8 +883,13 @@ export class PLpgSQLDeparser {
const isSchemaQualified = this.isSchemaQualifiedType(typname);

if (!isSchemaQualified) {
// Simple type - just strip quotes and return as-is
return typname.replace(/"/g, '').trim();
const trimmed = typname.trim();
const quoted = trimmed.match(/^"((?:[^"]|"")*)"((?:\[\d*\])*)$/);
if (quoted) {
const name = quoted[1].replace(/""/g, '"');
return (name === 'char' ? '"char"' : QuoteUtils.quoteIdentifier(name)) + quoted[2];
}
return trimmed;
}

// Schema-qualified type - apply proper quoting
Expand Down
Loading