UNPKG

rawsql-ts

Version:

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

93 lines 2.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IdentifierDecorator = void 0; const SqlSpecialValueKeywords_1 = require("../utils/SqlSpecialValueKeywords"); const Lexeme_1 = require("../models/Lexeme"); const SqlTokenizer_1 = require("./SqlTokenizer"); 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_1.SqlTokenizer(text).readLexmes(); return lexemes.length === 1 && lexemes[0].type === Lexeme_1.TokenType.Identifier && lexemes[0].value === text; } escapeIdentifierText(text) { if (!this.end) { return text; } return text.split(this.end).join(this.end + this.end); } } exports.IdentifierDecorator = IdentifierDecorator; 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. ...SqlSpecialValueKeywords_1.SQL_SPECIAL_VALUE_KEYWORDS ]); //# sourceMappingURL=IdentifierDecorator.js.map