Skip to content

Commit

Permalink
Remove duplication of postProcess() function in MariaDB-like formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Aug 31, 2023
1 parent c1c3ca1 commit 6c52e60
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 51 deletions.
19 changes: 19 additions & 0 deletions src/languages/mariadb/likeMariaDb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { EOF_TOKEN, isToken, Token, TokenType } from '../../lexer/token.js';

// Shared functionality used by all MariaDB-like SQL dialects.

export function postProcess(tokens: Token[]) {
return tokens.map((token, i) => {
const nextToken = tokens[i + 1] || EOF_TOKEN;
if (isToken.SET(token) && nextToken.text === '(') {
// This is SET datatype, not SET statement
return { ...token, type: TokenType.RESERVED_FUNCTION_NAME };
}
const prevToken = tokens[i - 1] || EOF_TOKEN;
if (isToken.VALUES(token) && prevToken.text === '=') {
// This is VALUES() function, not VALUES clause
return { ...token, type: TokenType.RESERVED_FUNCTION_NAME };
}
return token;
});
}
18 changes: 1 addition & 17 deletions src/languages/mariadb/mariadb.formatter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DialectOptions } from '../../dialect.js';
import { expandPhrases } from '../../expandPhrases.js';
import { EOF_TOKEN, isToken, Token, TokenType } from '../../lexer/token.js';
import { postProcess } from './likeMariaDb.js';
import { keywords } from './mariadb.keywords.js';
import { functions } from './mariadb.functions.js';

Expand Down Expand Up @@ -297,19 +297,3 @@ export const mariadb: DialectOptions = {
onelineClauses,
},
};

function postProcess(tokens: Token[]) {
return tokens.map((token, i) => {
const nextToken = tokens[i + 1] || EOF_TOKEN;
if (isToken.SET(token) && nextToken.text === '(') {
// This is SET datatype, not SET statement
return { ...token, type: TokenType.RESERVED_FUNCTION_NAME };
}
const prevToken = tokens[i - 1] || EOF_TOKEN;
if (isToken.VALUES(token) && prevToken.text === '=') {
// This is VALUES() function, not VALUES clause
return { ...token, type: TokenType.RESERVED_FUNCTION_NAME };
}
return token;
});
}
18 changes: 1 addition & 17 deletions src/languages/mysql/mysql.formatter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DialectOptions } from '../../dialect.js';
import { expandPhrases } from '../../expandPhrases.js';
import { EOF_TOKEN, isToken, Token, TokenType } from '../../lexer/token.js';
import { postProcess } from '../mariadb/likeMariaDb.js';
import { keywords } from './mysql.keywords.js';
import { functions } from './mysql.functions.js';

Expand Down Expand Up @@ -264,19 +264,3 @@ export const mysql: DialectOptions = {
onelineClauses,
},
};

function postProcess(tokens: Token[]) {
return tokens.map((token, i) => {
const nextToken = tokens[i + 1] || EOF_TOKEN;
if (isToken.SET(token) && nextToken.text === '(') {
// This is SET datatype, not SET statement
return { ...token, type: TokenType.RESERVED_FUNCTION_NAME };
}
const prevToken = tokens[i - 1] || EOF_TOKEN;
if (isToken.VALUES(token) && prevToken.text === '=') {
// This is VALUES() function, not VALUES clause
return { ...token, type: TokenType.RESERVED_FUNCTION_NAME };
}
return token;
});
}
18 changes: 1 addition & 17 deletions src/languages/singlestoredb/singlestoredb.formatter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DialectOptions } from '../../dialect.js';
import { expandPhrases } from '../../expandPhrases.js';
import { EOF_TOKEN, isToken, Token, TokenType } from '../../lexer/token.js';
import { postProcess } from '../mariadb/likeMariaDb.js';
import { keywords } from './singlestoredb.keywords.js';
import { functions } from './singlestoredb.functions.js';

Expand Down Expand Up @@ -278,19 +278,3 @@ export const singlestoredb: DialectOptions = {
onelineClauses,
},
};

function postProcess(tokens: Token[]) {
return tokens.map((token, i) => {
const nextToken = tokens[i + 1] || EOF_TOKEN;
if (isToken.SET(token) && nextToken.text === '(') {
// This is SET datatype, not SET statement
return { ...token, type: TokenType.RESERVED_FUNCTION_NAME };
}
const prevToken = tokens[i - 1] || EOF_TOKEN;
if (isToken.VALUES(token) && prevToken.text === '=') {
// This is VALUES() function, not VALUES clause
return { ...token, type: TokenType.RESERVED_FUNCTION_NAME };
}
return token;
});
}

0 comments on commit 6c52e60

Please sign in to comment.