Skip to content

Commit

Permalink
Merge PR: Update E style escape strings for postgresql (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Nov 1, 2023
2 parents 5154745 + a534995 commit e887f76
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/languages/postgresql/postgresql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export const postgresql: DialectOptions = {
stringTypes: [
'$$',
{ quote: "''-qq", prefixes: ['U&'] },
{ quote: "''-bs", prefixes: ['E'], requirePrefix: true },
{ quote: "''-qq-bs", prefixes: ['E'], requirePrefix: true },
{ quote: "''-raw", prefixes: ['B', 'X'], requirePrefix: true },
],
identTypes: [{ quote: '""-qq', prefixes: ['U&'] }],
Expand Down
27 changes: 27 additions & 0 deletions test/features/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type StringType =
| "''-bs" // with backslash escaping
| "U&''" // with repeated-quote escaping
| "N''" // with escaping style depending on whether also ''-qq or ''-bs was specified
| "E''" // with escaping style depending on whether also ''-qq or ''-bs was specified
| "X''" // no escaping
| 'X""' // no escaping
| "B''" // no escaping
Expand Down Expand Up @@ -139,6 +140,32 @@ export default function supportsStrings(format: FormatFn, stringTypes: StringTyp
});
}

if (stringTypes.includes("E''")) {
it('supports unicode strings', () => {
expect(format("SELECT E'where' FROM E'update'")).toBe(dedent`
SELECT
E'where'
FROM
E'update'
`);
});

if (stringTypes.includes("''-qq")) {
it("supports escaping in E'' strings with repeated quote", () => {
expect(format("E'foo '' JOIN bar'")).toBe("E'foo '' JOIN bar'");
});
}
if (stringTypes.includes("''-bs")) {
it("supports escaping in E'' strings with a backslash", () => {
expect(format("E'foo \\' JOIN bar'")).toBe("E'foo \\' JOIN bar'");
});
}

it("detects consecutive E'' strings as separate ones", () => {
expect(format("E'foo'E'bar'")).toBe("E'foo' E'bar'");
});
}

if (stringTypes.includes("X''")) {
it('supports hex byte sequences', () => {
expect(format("x'0E'")).toBe("x'0E'");
Expand Down
2 changes: 1 addition & 1 deletion test/postgresql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('PostgreSqlFormatter', () => {
supportsOnConflict(format);
supportsUpdate(format, { whereCurrentOf: true });
supportsTruncateTable(format, { withoutTable: true });
supportsStrings(format, ["''-qq", "U&''", "X''", "B''"]);
supportsStrings(format, ["''-qq", "U&''", "X''", "B''", "E''"]);
supportsIdentifiers(format, [`""-qq`, 'U&""']);
supportsBetween(format);
supportsSchema(format);
Expand Down

0 comments on commit e887f76

Please sign in to comment.