Skip to content

Commit

Permalink
Support PostgreSQL OPERATOR(...) syntax
Browse files Browse the repository at this point in the history
Fixes #711
  • Loading branch information
nene committed Nov 17, 2024
1 parent 8a4a988 commit f3b6cdb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/languages/postgresql/postgresql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ export const postgresql: DialectOptions = {
'::',
':',
],
operatorKeyword: true,
},
formatOptions: {
alwaysDenseOperators: ['::', ':'],
Expand Down
8 changes: 8 additions & 0 deletions src/lexer/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ export default class Tokenizer {
regex: cfg.supportsXor ? /XOR\b/iuy : undefined,
text: toCanonical,
},
...(cfg.operatorKeyword
? [
{
type: TokenType.OPERATOR,
regex: /OPERATOR *\([^)]+\)/iuy,
},
]
: []),
{
type: TokenType.RESERVED_FUNCTION_NAME,
regex: regex.reservedWord(cfg.reservedFunctionNames, cfg.identChars),
Expand Down
2 changes: 2 additions & 0 deletions src/lexer/TokenizerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export interface TokenizerOptions {
// Additional operators for property access, in addition to .
// Like in table.column
propertyAccessOperators?: string[];
// Enables PostgreSQL-specific OPERATOR(...) syntax
operatorKeyword?: boolean;
// Allows custom modifications on the token array.
// Called after the whole input string has been split into tokens.
// The result of this will be the output of the tokenizer.
Expand Down
12 changes: 12 additions & 0 deletions test/postgresql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,16 @@ describe('PostgreSqlFormatter', () => {
CREATE TABLE foo (text VARCHAR(100));
`);
});

// Issue #711
it('supports OPERATOR() syntax', () => {
expect(format(`SELECT foo OPERATOR(public.===) bar;`)).toBe(dedent`
SELECT
foo OPERATOR(public.===) bar;
`);
expect(format(`SELECT foo operator ( !== ) bar;`)).toBe(dedent`
SELECT
foo operator ( !== ) bar;
`);
});
});

0 comments on commit f3b6cdb

Please sign in to comment.