UNPKG

rawsql-ts

Version:

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

455 lines 14.7 kB
import { SqlComponent } from "./SqlComponent"; import { IdentifierString, RawString, QualifiedName } from "./ValueComponent"; export class SelectItem extends SqlComponent { constructor(value, name = null) { super(); this.value = value; this.identifier = name ? new IdentifierString(name) : null; } } SelectItem.kind = Symbol("SelectItem"); export class SelectClause extends SqlComponent { constructor(items, distinct = null) { super(); this.items = items; this.distinct = distinct; } } SelectClause.kind = Symbol("SelectClause"); export class Distinct extends SqlComponent { constructor() { super(); } } Distinct.kind = Symbol("Distinct"); export class DistinctOn extends SqlComponent { constructor(value) { super(); this.value = value; } } DistinctOn.kind = Symbol("DistinctOn"); export class WhereClause extends SqlComponent { constructor(condition) { super(); this.condition = condition; } } WhereClause.kind = Symbol("WhereClause"); export class PartitionByClause extends SqlComponent { constructor(value) { super(); this.value = value; } } PartitionByClause.kind = Symbol("PartitionByClause"); export class WindowFrameClause extends SqlComponent { constructor(name, expression) { super(); this.name = new IdentifierString(name); this.expression = expression; } } WindowFrameClause.kind = Symbol("WindowFrameClause"); /** * Represents a collection of window definitions (WINDOW clause in SQL). * @param windows Array of WindowFrameClause */ export class WindowsClause extends SqlComponent { constructor(windows) { super(); this.windows = windows; } } WindowsClause.kind = Symbol("WindowsClause"); export var SortDirection; (function (SortDirection) { SortDirection["Ascending"] = "asc"; SortDirection["Descending"] = "desc"; })(SortDirection || (SortDirection = {})); export var NullsSortDirection; (function (NullsSortDirection) { NullsSortDirection["First"] = "first"; NullsSortDirection["Last"] = "last"; })(NullsSortDirection || (NullsSortDirection = {})); export class OrderByClause extends SqlComponent { constructor(items) { super(); this.order = items; } } OrderByClause.kind = Symbol("OrderByClause"); export class OrderByItem extends SqlComponent { constructor(expression, sortDirection, nullsPosition) { super(); this.value = expression; this.sortDirection = sortDirection === null ? SortDirection.Ascending : sortDirection; this.nullsPosition = nullsPosition; } } OrderByItem.kind = Symbol("OrderByItem"); export class GroupByClause extends SqlComponent { constructor(expression) { super(); this.grouping = expression; } } GroupByClause.kind = Symbol("GroupByClause"); export class HavingClause extends SqlComponent { constructor(condition) { super(); this.condition = condition; } } HavingClause.kind = Symbol("HavingClause"); export class TableSource extends 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 IdentifierString) { return this.qualifiedName.name; } else { return new 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 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 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 RawString ? this.qualifiedName.name.value : this.qualifiedName.name.name); } else { return this.qualifiedName.name instanceof RawString ? this.qualifiedName.name.value : this.qualifiedName.name.name; } } } TableSource.kind = Symbol("TableSource"); export class FunctionSource extends SqlComponent { constructor(name, argument) { super(); if (typeof name === "object" && name !== null && "name" in name) { // Accepts { namespaces, name } const nameObj = name; this.qualifiedName = new QualifiedName(nameObj.namespaces, nameObj.name); } else { this.qualifiedName = new 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; } } FunctionSource.kind = Symbol("FunctionSource"); export class ParenSource extends SqlComponent { constructor(source) { super(); this.source = source; } } ParenSource.kind = Symbol("ParenSource"); export class SubQuerySource extends SqlComponent { constructor(query) { super(); this.query = query; } } SubQuerySource.kind = Symbol("SubQuerySource"); export class SourceExpression extends 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; } } SourceExpression.kind = Symbol("SourceExpression"); export class JoinOnClause extends SqlComponent { constructor(condition) { super(); this.condition = condition; } } JoinOnClause.kind = Symbol("JoinOnClause"); export class JoinUsingClause extends SqlComponent { constructor(condition) { super(); this.condition = condition; } } JoinUsingClause.kind = Symbol("JoinUsingClause"); export class JoinClause extends SqlComponent { constructor(joinType, source, condition, lateral) { super(); this.joinType = new 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; } } JoinClause.kind = Symbol("JoinItem"); export class FromClause extends 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; } } FromClause.kind = Symbol("FromClause"); export class CommonTable extends 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; } } CommonTable.kind = Symbol("CommonTable"); export class WithClause extends SqlComponent { constructor(recursive, tables) { super(); this.recursive = recursive; this.tables = tables; } } WithClause.kind = Symbol("WithClause"); export class LimitClause extends SqlComponent { constructor(limit) { super(); this.value = limit; } } LimitClause.kind = Symbol("LimitClause"); export var FetchType; (function (FetchType) { FetchType["Next"] = "next"; FetchType["First"] = "first"; })(FetchType || (FetchType = {})); export var FetchUnit; (function (FetchUnit) { FetchUnit["RowsOnly"] = "rows only"; FetchUnit["Percent"] = "percent"; FetchUnit["PercentWithTies"] = "percent with ties"; })(FetchUnit || (FetchUnit = {})); export class OffsetClause extends SqlComponent { constructor(value) { super(); this.value = value; } } OffsetClause.kind = Symbol("OffsetClause"); export class FetchClause extends SqlComponent { constructor(expression) { super(); this.expression = expression; } } FetchClause.kind = Symbol("FetchClause"); export class FetchExpression extends SqlComponent { constructor(type, count, unit) { super(); this.type = type; this.count = count; this.unit = unit; } } FetchExpression.kind = Symbol("FetchExpression"); export var LockMode; (function (LockMode) { LockMode["Update"] = "update"; LockMode["Share"] = "share"; LockMode["KeyShare"] = "key share"; LockMode["NokeyUpdate"] = "no key update"; })(LockMode || (LockMode = {})); export class ForClause extends SqlComponent { constructor(lockMode) { super(); this.lockMode = lockMode; } } ForClause.kind = Symbol("ForClause"); export class SourceAliasExpression extends SqlComponent { constructor(alias, columnAlias) { super(); this.table = new IdentifierString(alias); this.columns = columnAlias !== null ? columnAlias.map((alias) => new IdentifierString(alias)) : null; } } SourceAliasExpression.kind = Symbol("SourceAliasExpression"); export class ReturningClause extends 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 IdentifierString(col) : col); } } ReturningClause.kind = Symbol("ReturningClause"); export class SetClause extends SqlComponent { constructor(items) { super(); this.items = items.map(item => item instanceof SetClauseItem ? item : new SetClauseItem(item.column, item.value)); } } 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. */ export class SetClauseItem extends 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 IdentifierString(colObj.column) : colObj.column; this.qualifiedName = new QualifiedName(colObj.namespaces, col); } else { const col = typeof column === "string" ? new IdentifierString(column) : column; this.qualifiedName = new 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 IdentifierString) { return this.qualifiedName.name; } else { return new IdentifierString(this.qualifiedName.name.value); } } /** * Returns the fully qualified column name as a string. */ getFullName() { return this.qualifiedName.toString(); } } SetClauseItem.kind = Symbol("SetClauseItem"); export class UpdateClause extends 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; } } 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) */ export class InsertClause extends SqlComponent { constructor(source, columns) { super(); this.source = source; this.columns = columns.map((col) => new IdentifierString(col)); } } //# sourceMappingURL=Clause.js.map