UNPKG

rawsql-ts

Version:

High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.

89 lines 2.36 kB
import { SQL_SPECIAL_VALUE_KEYWORDS } from "../utils/SqlSpecialValueKeywords"; import { TokenType } from "../models/Lexeme"; import { SqlTokenizer } from "./SqlTokenizer"; export class IdentifierDecorator { constructor(identifierEscape) { var _a, _b, _c; this.start = (_a = identifierEscape === null || identifierEscape === void 0 ? void 0 : identifierEscape.start) !== null && _a !== void 0 ? _a : '"'; this.end = (_b = identifierEscape === null || identifierEscape === void 0 ? void 0 : identifierEscape.end) !== null && _b !== void 0 ? _b : '"'; this.target = (_c = identifierEscape === null || identifierEscape === void 0 ? void 0 : identifierEscape.target) !== null && _c !== void 0 ? _c : 'all'; } decorate(text) { if (this.target === 'minimal' && this.canRenderBare(text)) { return text; } return this.start + this.escapeIdentifierText(text) + this.end; } canRenderBare(text) { return /^[a-z_][a-z0-9_]*$/.test(text) && !UNSAFE_BARE_IDENTIFIERS.has(text) && this.isPlainIdentifierToken(text); } isPlainIdentifierToken(text) { const lexemes = new SqlTokenizer(text).readLexmes(); return lexemes.length === 1 && lexemes[0].type === TokenType.Identifier && lexemes[0].value === text; } escapeIdentifierText(text) { if (!this.end) { return text; } return text.split(this.end).join(this.end + this.end); } } const UNSAFE_BARE_IDENTIFIERS = new Set([ // Core SQL syntax and literals. 'all', 'and', 'any', 'as', 'between', 'by', 'case', 'cross', 'delete', 'distinct', 'else', 'end', 'except', 'exists', 'false', 'fetch', 'for', 'from', 'full', 'group', 'having', 'in', 'inner', 'insert', 'intersect', 'into', 'is', 'join', 'left', 'like', 'limit', 'not', 'null', 'offset', 'on', 'or', 'order', 'outer', 'right', 'select', 'set', 'table', 'then', 'true', 'union', 'update', 'using', 'values', 'when', 'where', 'with', // SQL value keywords / special bare expressions. ...SQL_SPECIAL_VALUE_KEYWORDS ]); //# sourceMappingURL=IdentifierDecorator.js.map