Skip to content

Commit e887f76

Browse files
authored
Merge PR: Update E style escape strings for postgresql (#655)
2 parents 5154745 + a534995 commit e887f76

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/languages/postgresql/postgresql.formatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export const postgresql: DialectOptions = {
266266
stringTypes: [
267267
'$$',
268268
{ quote: "''-qq", prefixes: ['U&'] },
269-
{ quote: "''-bs", prefixes: ['E'], requirePrefix: true },
269+
{ quote: "''-qq-bs", prefixes: ['E'], requirePrefix: true },
270270
{ quote: "''-raw", prefixes: ['B', 'X'], requirePrefix: true },
271271
],
272272
identTypes: [{ quote: '""-qq', prefixes: ['U&'] }],

test/features/strings.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type StringType =
1212
| "''-bs" // with backslash escaping
1313
| "U&''" // with repeated-quote escaping
1414
| "N''" // with escaping style depending on whether also ''-qq or ''-bs was specified
15+
| "E''" // with escaping style depending on whether also ''-qq or ''-bs was specified
1516
| "X''" // no escaping
1617
| 'X""' // no escaping
1718
| "B''" // no escaping
@@ -139,6 +140,32 @@ export default function supportsStrings(format: FormatFn, stringTypes: StringTyp
139140
});
140141
}
141142

143+
if (stringTypes.includes("E''")) {
144+
it('supports unicode strings', () => {
145+
expect(format("SELECT E'where' FROM E'update'")).toBe(dedent`
146+
SELECT
147+
E'where'
148+
FROM
149+
E'update'
150+
`);
151+
});
152+
153+
if (stringTypes.includes("''-qq")) {
154+
it("supports escaping in E'' strings with repeated quote", () => {
155+
expect(format("E'foo '' JOIN bar'")).toBe("E'foo '' JOIN bar'");
156+
});
157+
}
158+
if (stringTypes.includes("''-bs")) {
159+
it("supports escaping in E'' strings with a backslash", () => {
160+
expect(format("E'foo \\' JOIN bar'")).toBe("E'foo \\' JOIN bar'");
161+
});
162+
}
163+
164+
it("detects consecutive E'' strings as separate ones", () => {
165+
expect(format("E'foo'E'bar'")).toBe("E'foo' E'bar'");
166+
});
167+
}
168+
142169
if (stringTypes.includes("X''")) {
143170
it('supports hex byte sequences', () => {
144171
expect(format("x'0E'")).toBe("x'0E'");

test/postgresql.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('PostgreSqlFormatter', () => {
5252
supportsOnConflict(format);
5353
supportsUpdate(format, { whereCurrentOf: true });
5454
supportsTruncateTable(format, { withoutTable: true });
55-
supportsStrings(format, ["''-qq", "U&''", "X''", "B''"]);
55+
supportsStrings(format, ["''-qq", "U&''", "X''", "B''", "E''"]);
5656
supportsIdentifiers(format, [`""-qq`, 'U&""']);
5757
supportsBetween(format);
5858
supportsSchema(format);

0 commit comments

Comments
 (0)