UNPKG

rawsql-ts

Version:

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

492 lines 17.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InsertClause = exports.UpdateClause = exports.SetClauseItem = exports.SetClause = exports.ReturningClause = exports.SourceAliasExpression = exports.ForClause = exports.LockMode = exports.FetchExpression = exports.FetchClause = exports.OffsetClause = exports.FetchUnit = exports.FetchType = exports.LimitClause = exports.WithClause = exports.CommonTable = exports.FromClause = exports.JoinClause = exports.JoinUsingClause = exports.JoinOnClause = exports.SourceExpression = exports.SubQuerySource = exports.ParenSource = exports.FunctionSource = exports.TableSource = exports.HavingClause = exports.GroupByClause = exports.OrderByItem = exports.OrderByClause = exports.NullsSortDirection = exports.SortDirection = exports.WindowsClause = exports.WindowFrameClause = exports.PartitionByClause = exports.WhereClause = exports.DistinctOn = exports.Distinct = exports.SelectClause = exports.SelectItem = void 0; const SqlComponent_1 = require("./SqlComponent"); const ValueComponent_1 = require("./ValueComponent"); class SelectItem extends SqlComponent_1.SqlComponent { constructor(value, name = null) { super(); this.value = value; this.identifier = name ? new ValueComponent_1.IdentifierString(name) : null; } } exports.SelectItem = SelectItem; SelectItem.kind = Symbol("SelectItem"); class SelectClause extends SqlComponent_1.SqlComponent { constructor(items, distinct = null) { super(); this.items = items; this.distinct = distinct; } } exports.SelectClause = SelectClause; SelectClause.kind = Symbol("SelectClause"); class Distinct extends SqlComponent_1.SqlComponent { constructor() { super(); } } exports.Distinct = Distinct; Distinct.kind = Symbol("Distinct"); class DistinctOn extends SqlComponent_1.SqlComponent { constructor(value) { super(); this.value = value; } } exports.DistinctOn = DistinctOn; DistinctOn.kind = Symbol("DistinctOn"); class WhereClause extends SqlComponent_1.SqlComponent { constructor(condition) { super(); this.condition = condition; } } exports.WhereClause = WhereClause; WhereClause.kind = Symbol("WhereClause"); class PartitionByClause extends SqlComponent_1.SqlComponent { constructor(value) { super(); this.value = value; } } exports.PartitionByClause = PartitionByClause; PartitionByClause.kind = Symbol("PartitionByClause"); class WindowFrameClause extends SqlComponent_1.SqlComponent { constructor(name, expression) { super(); this.name = new ValueComponent_1.IdentifierString(name); this.expression = expression; } } exports.WindowFrameClause = WindowFrameClause; WindowFrameClause.kind = Symbol("WindowFrameClause"); /** * Represents a collection of window definitions (WINDOW clause in SQL). * @param windows Array of WindowFrameClause */ class WindowsClause extends SqlComponent_1.SqlComponent { constructor(windows) { super(); this.windows = windows; } } exports.WindowsClause = WindowsClause; WindowsClause.kind = Symbol("WindowsClause"); var SortDirection; (function (SortDirection) { SortDirection["Ascending"] = "asc"; SortDirection["Descending"] = "desc"; })(SortDirection || (exports.SortDirection = SortDirection = {})); var NullsSortDirection; (function (NullsSortDirection) { NullsSortDirection["First"] = "first"; NullsSortDirection["Last"] = "last"; })(NullsSortDirection || (exports.NullsSortDirection = NullsSortDirection = {})); class OrderByClause extends SqlComponent_1.SqlComponent { constructor(items) { super(); this.order = items; } } exports.OrderByClause = OrderByClause; OrderByClause.kind = Symbol("OrderByClause"); class OrderByItem extends SqlComponent_1.SqlComponent { constructor(expression, sortDirection, nullsPosition) { super(); this.value = expression; this.sortDirection = sortDirection === null ? SortDirection.Ascending : sortDirection; this.nullsPosition = nullsPosition; } } exports.OrderByItem = OrderByItem; OrderByItem.kind = Symbol("OrderByItem"); class GroupByClause extends SqlComponent_1.SqlComponent { constructor(expression) { super(); this.grouping = expression; } } exports.GroupByClause = GroupByClause; GroupByClause.kind = Symbol("GroupByClause"); class HavingClause extends SqlComponent_1.SqlComponent { constructor(condition) { super(); this.condition = condition; } } exports.HavingClause = HavingClause; HavingClause.kind = Symbol("HavingClause"); class TableSource 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 table name as IdentifierString (readonly) */ get table() { // If the name is RawString, convert to IdentifierString for compatibility if (this.qualifiedName.name instanceof ValueComponent_1.IdentifierString) { return this.qualifiedName.name; } else { return new ValueComponent_1.IdentifierString(this.qualifiedName.name.value); } } /** * For backward compatibility: returns the table name as IdentifierString (readonly) */ get identifier() { return this.table; } constructor(namespaces, table) { super(); // Convert the table name to an IdentifierString if it is provided as a string. const tbl = typeof table === "string" ? new ValueComponent_1.IdentifierString(table) : table; // Wrap the namespaces and table name in a QualifiedName object. // This design choice ensures backward compatibility by allowing the namespaces // and table name to be accessed in a way consistent with the previous implementation. this.qualifiedName = new ValueComponent_1.QualifiedName(namespaces, tbl); } getSourceName() { if (this.qualifiedName.namespaces && this.qualifiedName.namespaces.length > 0) { return this.qualifiedName.namespaces.map((namespace) => namespace.name).join(".") + "." + (this.qualifiedName.name instanceof ValueComponent_1.RawString ? this.qualifiedName.name.value : this.qualifiedName.name.name); } else { return this.qualifiedName.name instanceof ValueComponent_1.RawString ? this.qualifiedName.name.value : this.qualifiedName.name.name; } } } exports.TableSource = TableSource; TableSource.kind = Symbol("TableSource"); class FunctionSource extends SqlComponent_1.SqlComponent { constructor(name, argument) { super(); if (typeof name === "object" && name !== null && "name" in name) { // Accepts { namespaces, name } const nameObj = name; this.qualifiedName = new ValueComponent_1.QualifiedName(nameObj.namespaces, nameObj.name); } else { this.qualifiedName = new ValueComponent_1.QualifiedName(null, 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 function name as RawString | IdentifierString (readonly) */ get name() { return this.qualifiedName.name; } } exports.FunctionSource = FunctionSource; FunctionSource.kind = Symbol("FunctionSource"); class ParenSource extends SqlComponent_1.SqlComponent { constructor(source) { super(); this.source = source; } } exports.ParenSource = ParenSource; ParenSource.kind = Symbol("ParenSource"); class SubQuerySource extends SqlComponent_1.SqlComponent { constructor(query) { super(); this.query = query; } } exports.SubQuerySource = SubQuerySource; SubQuerySource.kind = Symbol("SubQuerySource"); class SourceExpression extends SqlComponent_1.SqlComponent { constructor(datasource, aliasExpression) { super(); this.datasource = datasource; this.aliasExpression = aliasExpression; } getAliasName() { if (this.aliasExpression) { return this.aliasExpression.table.name; } else if (this.datasource instanceof TableSource) { return this.datasource.getSourceName(); } return null; } } exports.SourceExpression = SourceExpression; SourceExpression.kind = Symbol("SourceExpression"); class JoinOnClause extends SqlComponent_1.SqlComponent { constructor(condition) { super(); this.condition = condition; } } exports.JoinOnClause = JoinOnClause; JoinOnClause.kind = Symbol("JoinOnClause"); class JoinUsingClause extends SqlComponent_1.SqlComponent { constructor(condition) { super(); this.condition = condition; } } exports.JoinUsingClause = JoinUsingClause; JoinUsingClause.kind = Symbol("JoinUsingClause"); class JoinClause extends SqlComponent_1.SqlComponent { constructor(joinType, source, condition, lateral) { super(); this.joinType = new ValueComponent_1.RawString(joinType); this.source = source; this.condition = condition; this.lateral = lateral; } getSourceAliasName() { if (this.source.aliasExpression) { return this.source.aliasExpression.table.name; } else if (this.source instanceof TableSource) { return this.source.table.name; } return null; } } exports.JoinClause = JoinClause; JoinClause.kind = Symbol("JoinItem"); class FromClause extends SqlComponent_1.SqlComponent { constructor(source, join) { super(); this.source = source; this.joins = join; } getSourceAliasName() { if (this.source.aliasExpression) { return this.source.aliasExpression.table.name; } else if (this.source.datasource instanceof TableSource) { return this.source.datasource.table.name; } return null; } /** * Returns all SourceExpression objects in this FROM clause, including main source and all JOIN sources. */ getSources() { const sources = [this.source]; if (this.joins) { for (const join of this.joins) { sources.push(join.source); } } return sources; } } exports.FromClause = FromClause; FromClause.kind = Symbol("FromClause"); class CommonTable extends SqlComponent_1.SqlComponent { constructor(query, aliasExpression, materialized) { super(); this.query = query; this.materialized = materialized; if (typeof aliasExpression === "string") { this.aliasExpression = new SourceAliasExpression(aliasExpression, null); } else { this.aliasExpression = aliasExpression; } } getSourceAliasName() { return this.aliasExpression.table.name; } } exports.CommonTable = CommonTable; CommonTable.kind = Symbol("CommonTable"); class WithClause extends SqlComponent_1.SqlComponent { constructor(recursive, tables) { super(); this.recursive = recursive; this.tables = tables; } } exports.WithClause = WithClause; WithClause.kind = Symbol("WithClause"); class LimitClause extends SqlComponent_1.SqlComponent { constructor(limit) { super(); this.value = limit; } } exports.LimitClause = LimitClause; LimitClause.kind = Symbol("LimitClause"); var FetchType; (function (FetchType) { FetchType["Next"] = "next"; FetchType["First"] = "first"; })(FetchType || (exports.FetchType = FetchType = {})); var FetchUnit; (function (FetchUnit) { FetchUnit["RowsOnly"] = "rows only"; FetchUnit["Percent"] = "percent"; FetchUnit["PercentWithTies"] = "percent with ties"; })(FetchUnit || (exports.FetchUnit = FetchUnit = {})); class OffsetClause extends SqlComponent_1.SqlComponent { constructor(value) { super(); this.value = value; } } exports.OffsetClause = OffsetClause; OffsetClause.kind = Symbol("OffsetClause"); class FetchClause extends SqlComponent_1.SqlComponent { constructor(expression) { super(); this.expression = expression; } } exports.FetchClause = FetchClause; FetchClause.kind = Symbol("FetchClause"); class FetchExpression extends SqlComponent_1.SqlComponent { constructor(type, count, unit) { super(); this.type = type; this.count = count; this.unit = unit; } } exports.FetchExpression = FetchExpression; FetchExpression.kind = Symbol("FetchExpression"); var LockMode; (function (LockMode) { LockMode["Update"] = "update"; LockMode["Share"] = "share"; LockMode["KeyShare"] = "key share"; LockMode["NokeyUpdate"] = "no key update"; })(LockMode || (exports.LockMode = LockMode = {})); class ForClause extends SqlComponent_1.SqlComponent { constructor(lockMode) { super(); this.lockMode = lockMode; } } exports.ForClause = ForClause; ForClause.kind = Symbol("ForClause"); class SourceAliasExpression extends SqlComponent_1.SqlComponent { constructor(alias, columnAlias) { super(); this.table = new ValueComponent_1.IdentifierString(alias); this.columns = columnAlias !== null ? columnAlias.map((alias) => new ValueComponent_1.IdentifierString(alias)) : null; } } exports.SourceAliasExpression = SourceAliasExpression; SourceAliasExpression.kind = Symbol("SourceAliasExpression"); class ReturningClause extends SqlComponent_1.SqlComponent { /** * Constructs a ReturningClause. * @param columns Array of IdentifierString or string representing column names. */ constructor(columns) { super(); this.columns = columns.map(col => typeof col === "string" ? new ValueComponent_1.IdentifierString(col) : col); } } exports.ReturningClause = ReturningClause; ReturningClause.kind = Symbol("ReturningClause"); class SetClause extends SqlComponent_1.SqlComponent { constructor(items) { super(); this.items = items.map(item => item instanceof SetClauseItem ? item : new SetClauseItem(item.column, item.value)); } } exports.SetClause = SetClause; SetClause.kind = Symbol("SetClause"); /** * Represents a single SET clause item in an UPDATE statement. */ /** * Represents a single SET clause item in an UPDATE statement. * Now supports namespaces for fully qualified column names (e.g. schema.table.column). */ /** * Represents a single SET clause item in an UPDATE statement. * Now supports namespaces for fully qualified column names (e.g. schema.table.column). * Refactored to use QualifiedName for unified name/namespace handling. */ class SetClauseItem extends SqlComponent_1.SqlComponent { constructor(column, value) { super(); // Accepts { namespaces, column } or just column if (typeof column === "object" && column !== null && "column" in column) { const colObj = column; const col = typeof colObj.column === "string" ? new ValueComponent_1.IdentifierString(colObj.column) : colObj.column; this.qualifiedName = new ValueComponent_1.QualifiedName(colObj.namespaces, col); } else { const col = typeof column === "string" ? new ValueComponent_1.IdentifierString(column) : column; this.qualifiedName = new ValueComponent_1.QualifiedName(null, col); } this.value = value; } /** * 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 (this.qualifiedName.name instanceof ValueComponent_1.IdentifierString) { return this.qualifiedName.name; } else { return new ValueComponent_1.IdentifierString(this.qualifiedName.name.value); } } /** * Returns the fully qualified column name as a string. */ getFullName() { return this.qualifiedName.toString(); } } exports.SetClauseItem = SetClauseItem; SetClauseItem.kind = Symbol("SetClauseItem"); class UpdateClause extends SqlComponent_1.SqlComponent { constructor(source) { super(); this.source = source; } getSourceAliasName() { if (this.source.aliasExpression) { return this.source.aliasExpression.table.name; } else if (this.source.datasource instanceof TableSource) { return this.source.datasource.table.name; } return null; } } exports.UpdateClause = UpdateClause; UpdateClause.kind = Symbol("UpdateClause"); /** * Represents the target table (with optional alias/schema) and columns for an INSERT statement. * @param source The target table as a SourceExpression (can include schema, alias, etc.) * @param columns Array of column names (as strings) */ class InsertClause extends SqlComponent_1.SqlComponent { constructor(source, columns) { super(); this.source = source; this.columns = columns.map((col) => new ValueComponent_1.IdentifierString(col)); } } exports.InsertClause = InsertClause; //# sourceMappingURL=Clause.js.map