UNPKG

rawsql-ts

Version:

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

348 lines 12.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.QualifiedName = exports.TupleExpression = exports.TypeValue = exports.StringSpecifierExpression = exports.BetweenExpression = exports.ArrayExpression = exports.CaseExpression = exports.CastExpression = exports.ParenExpression = exports.IdentifierString = exports.RawString = exports.CaseKeyValuePair = exports.SwitchCaseArgument = exports.ParameterExpression = exports.LiteralValue = exports.BinaryExpression = exports.UnaryExpression = exports.WindowFrameExpression = exports.WindowFrameSpec = exports.WindowFrameBoundaryValue = exports.WindowFrameBoundStatic = exports.WindowFrameBound = exports.WindowFrameType = exports.FunctionCall = exports.ColumnReference = exports.ValueList = exports.InlineQuery = void 0; const SqlComponent_1 = require("./SqlComponent"); class InlineQuery extends SqlComponent_1.SqlComponent { constructor(selectQuery) { super(); this.selectQuery = selectQuery; } } exports.InlineQuery = InlineQuery; InlineQuery.kind = Symbol("InlineQuery"); class ValueList extends SqlComponent_1.SqlComponent { constructor(values) { super(); this.values = values; } } exports.ValueList = ValueList; ValueList.kind = Symbol("ValueList"); class ColumnReference extends SqlComponent_1.SqlComponent { /** * For backward compatibility: returns the namespaces as IdentifierString[] | null (readonly) */ get namespaces() { return this.qualifiedName.namespaces; } /** * For backward compatibility: returns the column name as IdentifierString (readonly) */ get column() { // If the name is RawString, convert to IdentifierString for compatibility if (this.qualifiedName.name instanceof IdentifierString) { return this.qualifiedName.name; } else { return new IdentifierString(this.qualifiedName.name.value); } } constructor(namespaces, column) { super(); const col = typeof column === "string" ? new IdentifierString(column) : column; this.qualifiedName = new QualifiedName(toIdentifierStringArray(namespaces), col); } toString() { return this.qualifiedName.toString(); } getNamespace() { if (this.qualifiedName.namespaces) { return this.qualifiedName.namespaces.map((namespace) => namespace.name).join("."); } else { return ''; } } } exports.ColumnReference = ColumnReference; ColumnReference.kind = Symbol("ColumnReference"); class FunctionCall extends SqlComponent_1.SqlComponent { constructor(namespaces, name, argument, over) { super(); this.qualifiedName = new QualifiedName(namespaces, name); this.argument = argument; this.over = over; } /** * For backward compatibility: returns the namespaces as IdentifierString[] | null (readonly) */ get namespaces() { return this.qualifiedName.namespaces; } /** * For backward compatibility: returns the function name as RawString | IdentifierString (readonly) */ get name() { return this.qualifiedName.name; } } exports.FunctionCall = FunctionCall; FunctionCall.kind = Symbol("FunctionCall"); var WindowFrameType; (function (WindowFrameType) { WindowFrameType["Rows"] = "rows"; WindowFrameType["Range"] = "range"; WindowFrameType["Groups"] = "groups"; })(WindowFrameType || (exports.WindowFrameType = WindowFrameType = {})); var WindowFrameBound; (function (WindowFrameBound) { WindowFrameBound["UnboundedPreceding"] = "unbounded preceding"; WindowFrameBound["UnboundedFollowing"] = "unbounded following"; WindowFrameBound["CurrentRow"] = "current row"; })(WindowFrameBound || (exports.WindowFrameBound = WindowFrameBound = {})); class WindowFrameBoundStatic extends SqlComponent_1.SqlComponent { constructor(bound) { super(); this.bound = bound; } } exports.WindowFrameBoundStatic = WindowFrameBoundStatic; WindowFrameBoundStatic.kind = Symbol("WindowFrameStaticBound"); class WindowFrameBoundaryValue extends SqlComponent_1.SqlComponent { constructor(value, isFollowing) { super(); this.value = value; this.isFollowing = isFollowing; } } exports.WindowFrameBoundaryValue = WindowFrameBoundaryValue; WindowFrameBoundaryValue.kind = Symbol("WindowFrameBoundary"); class WindowFrameSpec extends SqlComponent_1.SqlComponent { constructor(frameType, startBound, endBound) { super(); this.frameType = frameType; this.startBound = startBound; this.endBound = endBound; } } exports.WindowFrameSpec = WindowFrameSpec; WindowFrameSpec.kind = Symbol("WindowFrameSpec"); class WindowFrameExpression extends SqlComponent_1.SqlComponent { constructor(partition, order, frameSpec = null) { super(); this.partition = partition; this.order = order; this.frameSpec = frameSpec; } } exports.WindowFrameExpression = WindowFrameExpression; WindowFrameExpression.kind = Symbol("WindowFrameExpression"); class UnaryExpression extends SqlComponent_1.SqlComponent { constructor(operator, expression) { super(); this.operator = new RawString(operator); this.expression = expression; } } exports.UnaryExpression = UnaryExpression; UnaryExpression.kind = Symbol("UnaryExpression"); class BinaryExpression extends SqlComponent_1.SqlComponent { constructor(left, operator, right) { super(); this.left = left; this.operator = new RawString(operator); this.right = right; } } exports.BinaryExpression = BinaryExpression; BinaryExpression.kind = Symbol("BinaryExpression"); class LiteralValue extends SqlComponent_1.SqlComponent { constructor(value) { super(); this.value = value; } } exports.LiteralValue = LiteralValue; LiteralValue.kind = Symbol("LiteralExpression"); class ParameterExpression extends SqlComponent_1.SqlComponent { constructor(name, value = null) { super(); this.name = new RawString(name); this.value = value; // Value is now accepted as a second argument (optional) this.index = null; } } exports.ParameterExpression = ParameterExpression; ParameterExpression.kind = Symbol("ParameterExpression"); class SwitchCaseArgument extends SqlComponent_1.SqlComponent { constructor(cases, elseValue = null) { super(); this.cases = cases; this.elseValue = elseValue; } } exports.SwitchCaseArgument = SwitchCaseArgument; SwitchCaseArgument.kind = Symbol("SwitchCaseArgument"); class CaseKeyValuePair extends SqlComponent_1.SqlComponent { constructor(key, value) { super(); this.key = key; this.value = value; } } exports.CaseKeyValuePair = CaseKeyValuePair; CaseKeyValuePair.kind = Symbol("CaseKeyValuePair"); /* * Values ​​that must be hard-coded, such as type names and function names. * A simple check is performed when decoding. */ class RawString extends SqlComponent_1.SqlComponent { constructor(value) { super(); this.value = value; } } exports.RawString = RawString; RawString.kind = Symbol("RawString"); class IdentifierString extends SqlComponent_1.SqlComponent { constructor(alias) { super(); this.name = alias; } } exports.IdentifierString = IdentifierString; IdentifierString.kind = Symbol("IdentifierString"); class ParenExpression extends SqlComponent_1.SqlComponent { constructor(expression) { super(); this.expression = expression; } } exports.ParenExpression = ParenExpression; ParenExpression.kind = Symbol("ParenExpression"); class CastExpression extends SqlComponent_1.SqlComponent { constructor(input, castType) { super(); this.input = input; this.castType = castType; } } exports.CastExpression = CastExpression; CastExpression.kind = Symbol("CastExpression"); class CaseExpression extends SqlComponent_1.SqlComponent { constructor(condition, switchCase) { super(); this.condition = condition; this.switchCase = switchCase; } } exports.CaseExpression = CaseExpression; CaseExpression.kind = Symbol("CaseExpression"); class ArrayExpression extends SqlComponent_1.SqlComponent { constructor(expression) { super(); this.expression = expression; } } exports.ArrayExpression = ArrayExpression; ArrayExpression.kind = Symbol("ArrayExpression"); class BetweenExpression extends SqlComponent_1.SqlComponent { constructor(expression, lower, upper, negated) { super(); this.expression = expression; this.lower = lower; this.upper = upper; this.negated = negated; } } exports.BetweenExpression = BetweenExpression; BetweenExpression.kind = Symbol("BetweenExpression"); class StringSpecifierExpression extends SqlComponent_1.SqlComponent { constructor(specifier, value) { super(); this.specifier = new RawString(specifier); this.value = new LiteralValue(value); } } exports.StringSpecifierExpression = StringSpecifierExpression; StringSpecifierExpression.kind = Symbol("StringSpecifierExpression"); // other class TypeValue extends SqlComponent_1.SqlComponent { constructor(namespaces, name, argument = null) { super(); this.qualifiedName = new QualifiedName(namespaces, name); this.argument = argument; } /** * For backward compatibility: returns the namespaces as IdentifierString[] | null (readonly) */ get namespaces() { return this.qualifiedName.namespaces; } /** * For backward compatibility: returns the type name as RawString | IdentifierString (readonly) */ get name() { return this.qualifiedName.name; } getTypeName() { const nameValue = this.qualifiedName.name instanceof RawString ? this.qualifiedName.name.value : this.qualifiedName.name.name; if (this.qualifiedName.namespaces && this.qualifiedName.namespaces.length > 0) { return this.qualifiedName.namespaces.map(ns => ns.name).join(".") + "." + nameValue; } else { return nameValue; } } } exports.TypeValue = TypeValue; TypeValue.kind = Symbol("TypeValue"); class TupleExpression extends SqlComponent_1.SqlComponent { constructor(values) { super(); this.values = values; } } exports.TupleExpression = TupleExpression; TupleExpression.kind = Symbol("TupleExpression"); function toIdentifierStringArray(input) { if (input == null) return null; if (typeof input === "string") { // Empty string should be treated as null return input.trim() === "" ? null : [new IdentifierString(input)]; } if (Array.isArray(input)) { if (input.length === 0) return null; if (typeof input[0] === "string") { // Filter out empty strings from string array const filteredStrings = input.filter(ns => ns.trim() !== ""); return filteredStrings.length === 0 ? null : filteredStrings.map(ns => new IdentifierString(ns)); } else { // Filter out empty IdentifierStrings from IdentifierString array const filteredIdentifiers = input.filter(ns => ns.name.trim() !== ""); return filteredIdentifiers.length === 0 ? null : filteredIdentifiers; } } return null; } /** * Represents a qualified name with optional namespaces (e.g. schema.table, db.schema.function) */ class QualifiedName extends SqlComponent_1.SqlComponent { constructor(namespaces, name) { super(); this.namespaces = toIdentifierStringArray(namespaces); if (typeof name === "string") { this.name = new RawString(name); } else { this.name = name; } } /** Returns the full qualified name as a string (dot-separated) */ toString() { const nameValue = this.name instanceof RawString ? this.name.value : this.name.name; if (this.namespaces && this.namespaces.length > 0) { return this.namespaces.map(ns => ns.name).join(".") + "." + nameValue; } else { return nameValue; } } } exports.QualifiedName = QualifiedName; QualifiedName.kind = Symbol("QualifiedName"); //# sourceMappingURL=ValueComponent.js.map