UNPKG

rawsql-ts

Version:

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

110 lines (109 loc) 4.48 kB
/** * Enum for duplicate detection modes in SelectableColumnCollector. * Determines how duplicates are identified during column collection. */ export declare enum DuplicateDetectionMode { /** * Detect duplicates based only on column names. * This mode ignores the table name, so columns with the same name * from different tables are considered duplicates. */ ColumnNameOnly = "columnNameOnly", /** * Detect duplicates based on both table and column names. * This mode ensures that columns with the same name from different * tables are treated as distinct. */ FullName = "fullName" } import { SqlComponent, SqlComponentVisitor } from "../models/SqlComponent"; import { ValueComponent } from "../models/ValueComponent"; import { TableColumnResolver } from "./TableColumnResolver"; /** * A visitor that collects all ColumnReference instances from a SQL query structure. * This visitor scans through all clauses and collects all unique ColumnReference objects. * It does not scan Common Table Expressions (CTEs) or subqueries. * * Important: Only collects column references to tables defined in the root FROM/JOIN clauses, * as these are the only columns that can be directly referenced in the query. */ export declare class SelectableColumnCollector implements SqlComponentVisitor<void> { private handlers; private selectValues; private visitedNodes; private isRootVisit; private tableColumnResolver; private commonTableCollector; private commonTables; private includeWildCard; private duplicateDetection; private options; /** * Creates a new instance of SelectableColumnCollector. * * @param {TableColumnResolver | null} [tableColumnResolver=null] - The resolver used to resolve column references to their respective tables. * @param {boolean} [includeWildCard=false] - If true, wildcard columns (e.g., `*`) are included in the collection. * @param {DuplicateDetectionMode} [duplicateDetection=DuplicateDetectionMode.ColumnNameOnly] - Specifies the duplicate detection mode: 'columnNameOnly' (default, only column name is used), or 'fullName' (table name + column name). * @param {Object} [options={}] - Additional options for the collector. * @param {boolean} [options.ignoreCaseAndUnderscore=false] - If true, column names are compared without considering case and underscores. */ constructor(tableColumnResolver?: TableColumnResolver | null, includeWildCard?: boolean, duplicateDetection?: DuplicateDetectionMode, options?: { ignoreCaseAndUnderscore?: boolean; }); getValues(): { name: string; value: ValueComponent; }[]; collect(arg: SqlComponent): { name: string; value: ValueComponent; }[]; /** * Reset the collection of ColumnReferences */ private reset; /** * Add a select value as unique, according to the duplicate detection option. * If duplicateDetection is 'columnNameOnly', only column name is checked. * If duplicateDetection is 'fullName', both table and column name are checked. */ private addSelectValueAsUnique; /** * Main entry point for the visitor pattern. * Implements the shallow visit pattern to distinguish between root and recursive visits. */ visit(arg: SqlComponent): void; /** * Internal visit method used for all nodes. * This separates the visit flag management from the actual node visitation logic. */ private visitNode; /** * Process a SimpleSelectQuery to collect ColumnReferences from all its clauses */ private visitSimpleSelectQuery; private visitSelectClause; private visitFromClause; private visitWhereClause; private visitGroupByClause; private visitHavingClause; private visitOrderByClause; private visitWindowFrameClause; private visitWindowFrameExpression; private visitLimitClause; private offsetClause; private visitFetchClause; private visitJoinOnClause; private visitJoinUsingClause; private visitColumnReference; private visitBinaryExpression; private visitUnaryExpression; private visitFunctionCall; private visitParenExpression; private visitCaseExpression; private visitCastExpression; private visitBetweenExpression; private visitArrayExpression; private visitValueList; private visitPartitionByClause; }