Skip to content

Commit f3b6cdb

Browse files
committed
Support PostgreSQL OPERATOR(...) syntax
Fixes #711
1 parent 8a4a988 commit f3b6cdb

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

src/languages/postgresql/postgresql.formatter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ export const postgresql: DialectOptions = {
368368
'::',
369369
':',
370370
],
371+
operatorKeyword: true,
371372
},
372373
formatOptions: {
373374
alwaysDenseOperators: ['::', ':'],

src/lexer/Tokenizer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ export default class Tokenizer {
130130
regex: cfg.supportsXor ? /XOR\b/iuy : undefined,
131131
text: toCanonical,
132132
},
133+
...(cfg.operatorKeyword
134+
? [
135+
{
136+
type: TokenType.OPERATOR,
137+
regex: /OPERATOR *\([^)]+\)/iuy,
138+
},
139+
]
140+
: []),
133141
{
134142
type: TokenType.RESERVED_FUNCTION_NAME,
135143
regex: regex.reservedWord(cfg.reservedFunctionNames, cfg.identChars),

src/lexer/TokenizerOptions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ export interface TokenizerOptions {
9898
// Additional operators for property access, in addition to .
9999
// Like in table.column
100100
propertyAccessOperators?: string[];
101+
// Enables PostgreSQL-specific OPERATOR(...) syntax
102+
operatorKeyword?: boolean;
101103
// Allows custom modifications on the token array.
102104
// Called after the whole input string has been split into tokens.
103105
// The result of this will be the output of the tokenizer.

test/postgresql.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,16 @@ describe('PostgreSqlFormatter', () => {
365365
CREATE TABLE foo (text VARCHAR(100));
366366
`);
367367
});
368+
369+
// Issue #711
370+
it('supports OPERATOR() syntax', () => {
371+
expect(format(`SELECT foo OPERATOR(public.===) bar;`)).toBe(dedent`
372+
SELECT
373+
foo OPERATOR(public.===) bar;
374+
`);
375+
expect(format(`SELECT foo operator ( !== ) bar;`)).toBe(dedent`
376+
SELECT
377+
foo operator ( !== ) bar;
378+
`);
379+
});
368380
});

0 commit comments

Comments
 (0)