rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
320 lines • 10.4 kB
JavaScript
import { SqlComponent } from "./SqlComponent";
export class InlineQuery extends SqlComponent {
constructor(selectQuery) {
super();
this.selectQuery = selectQuery;
}
}
InlineQuery.kind = Symbol("InlineQuery");
export class ValueList extends SqlComponent {
constructor(values) {
super();
this.values = values;
}
}
ValueList.kind = Symbol("ValueList");
export class ColumnReference extends 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 '';
}
}
}
ColumnReference.kind = Symbol("ColumnReference");
export class FunctionCall extends 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;
}
}
FunctionCall.kind = Symbol("FunctionCall");
export var WindowFrameType;
(function (WindowFrameType) {
WindowFrameType["Rows"] = "rows";
WindowFrameType["Range"] = "range";
WindowFrameType["Groups"] = "groups";
})(WindowFrameType || (WindowFrameType = {}));
export var WindowFrameBound;
(function (WindowFrameBound) {
WindowFrameBound["UnboundedPreceding"] = "unbounded preceding";
WindowFrameBound["UnboundedFollowing"] = "unbounded following";
WindowFrameBound["CurrentRow"] = "current row";
})(WindowFrameBound || (WindowFrameBound = {}));
export class WindowFrameBoundStatic extends SqlComponent {
constructor(bound) {
super();
this.bound = bound;
}
}
WindowFrameBoundStatic.kind = Symbol("WindowFrameStaticBound");
export class WindowFrameBoundaryValue extends SqlComponent {
constructor(value, isFollowing) {
super();
this.value = value;
this.isFollowing = isFollowing;
}
}
WindowFrameBoundaryValue.kind = Symbol("WindowFrameBoundary");
export class WindowFrameSpec extends SqlComponent {
constructor(frameType, startBound, endBound) {
super();
this.frameType = frameType;
this.startBound = startBound;
this.endBound = endBound;
}
}
WindowFrameSpec.kind = Symbol("WindowFrameSpec");
export class WindowFrameExpression extends SqlComponent {
constructor(partition, order, frameSpec = null) {
super();
this.partition = partition;
this.order = order;
this.frameSpec = frameSpec;
}
}
WindowFrameExpression.kind = Symbol("WindowFrameExpression");
export class UnaryExpression extends SqlComponent {
constructor(operator, expression) {
super();
this.operator = new RawString(operator);
this.expression = expression;
}
}
UnaryExpression.kind = Symbol("UnaryExpression");
export class BinaryExpression extends SqlComponent {
constructor(left, operator, right) {
super();
this.left = left;
this.operator = new RawString(operator);
this.right = right;
}
}
BinaryExpression.kind = Symbol("BinaryExpression");
export class LiteralValue extends SqlComponent {
constructor(value) {
super();
this.value = value;
}
}
LiteralValue.kind = Symbol("LiteralExpression");
export class ParameterExpression extends 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;
}
}
ParameterExpression.kind = Symbol("ParameterExpression");
export class SwitchCaseArgument extends SqlComponent {
constructor(cases, elseValue = null) {
super();
this.cases = cases;
this.elseValue = elseValue;
}
}
SwitchCaseArgument.kind = Symbol("SwitchCaseArgument");
export class CaseKeyValuePair extends SqlComponent {
constructor(key, value) {
super();
this.key = key;
this.value = value;
}
}
CaseKeyValuePair.kind = Symbol("CaseKeyValuePair");
/*
* Values that must be hard-coded, such as type names and function names.
* A simple check is performed when decoding.
*/
export class RawString extends SqlComponent {
constructor(value) {
super();
this.value = value;
}
}
RawString.kind = Symbol("RawString");
export class IdentifierString extends SqlComponent {
constructor(alias) {
super();
this.name = alias;
}
}
IdentifierString.kind = Symbol("IdentifierString");
export class ParenExpression extends SqlComponent {
constructor(expression) {
super();
this.expression = expression;
}
}
ParenExpression.kind = Symbol("ParenExpression");
export class CastExpression extends SqlComponent {
constructor(input, castType) {
super();
this.input = input;
this.castType = castType;
}
}
CastExpression.kind = Symbol("CastExpression");
export class CaseExpression extends SqlComponent {
constructor(condition, switchCase) {
super();
this.condition = condition;
this.switchCase = switchCase;
}
}
CaseExpression.kind = Symbol("CaseExpression");
export class ArrayExpression extends SqlComponent {
constructor(expression) {
super();
this.expression = expression;
}
}
ArrayExpression.kind = Symbol("ArrayExpression");
export class BetweenExpression extends SqlComponent {
constructor(expression, lower, upper, negated) {
super();
this.expression = expression;
this.lower = lower;
this.upper = upper;
this.negated = negated;
}
}
BetweenExpression.kind = Symbol("BetweenExpression");
export class StringSpecifierExpression extends SqlComponent {
constructor(specifier, value) {
super();
this.specifier = new RawString(specifier);
this.value = new LiteralValue(value);
}
}
StringSpecifierExpression.kind = Symbol("StringSpecifierExpression");
// other
export class TypeValue extends 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;
}
}
}
TypeValue.kind = Symbol("TypeValue");
export class TupleExpression extends SqlComponent {
constructor(values) {
super();
this.values = values;
}
}
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)
*/
export class QualifiedName extends 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;
}
}
}
QualifiedName.kind = Symbol("QualifiedName");
//# sourceMappingURL=ValueComponent.js.map