rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
99 lines (98 loc) • 3.85 kB
TypeScript
import { SqlComponent, SqlComponentVisitor } from '../models/SqlComponent';
import { TableColumnResolver } from './TableColumnResolver';
export declare class TableSchema {
name: string;
columns: string[];
constructor(name: string, columns: string[]);
}
export interface SchemaAnalysisResult {
success: boolean;
schemas: TableSchema[];
unresolvedColumns: string[];
error?: string;
}
/**
* Collects schema information (table names and resolved columns) from SelectQuery instances.
*
* @example
* ```typescript
* const collector = new SchemaCollector((table) => ['id', 'name']);
* const query = SelectQueryParser.parse('SELECT id, name FROM users');
* const schemas = collector.collect(query);
* ```
* Related tests: packages/core/tests/transformers/SchemaCollector.test.ts
*/
export declare class SchemaCollector implements SqlComponentVisitor<void> {
private tableColumnResolver;
private allowWildcardWithoutResolver;
private handlers;
private tableSchemas;
private visitedNodes;
private commonTables;
private running;
private unresolvedColumns;
private analysisError;
private isAnalyzeMode;
constructor(tableColumnResolver?: TableColumnResolver | null, allowWildcardWithoutResolver?: boolean);
/**
* Collects schema information (table names and column names) from a SQL query structure.
* This method ensures that the collected schema information is unique and sorted.
* The resulting schemas and columns are sorted alphabetically to ensure deterministic ordering.
*
* @param arg The SQL query structure to analyze.
*/
collect(arg: SqlComponent): TableSchema[];
/**
* Analyzes schema information from a SQL query structure without throwing errors.
* Returns a result object containing successfully resolved schemas, unresolved columns,
* and error information if any issues were encountered.
*
* @param arg The SQL query structure to analyze.
* @returns Analysis result containing schemas, unresolved columns, and success status.
*/
analyze(arg: SqlComponent): SchemaAnalysisResult;
/**
* Main entry point for the visitor pattern.
* Implements the shallow visit pattern to distinguish between root and recursive visits.
*
* This method ensures that schema information is collected uniquely and sorted.
* The resulting schemas and columns are sorted alphabetically to ensure deterministic ordering.
*
* @param arg The SQL component to visit.
*/
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;
/**
* Resets the state of the collector for a new root visit.
*/
private reset;
/**
* Consolidates table schemas by merging columns for tables with the same name.
* This ensures that each table name appears only once in the final schema list,
* with all its columns combined while removing duplicates.
*
* Note: The resulting schemas and columns are sorted alphabetically to ensure deterministic ordering.
*/
private consolidateTableSchemas;
private handleSourceExpression;
private visitSimpleSelectQuery;
private visitBinarySelectQuery;
/**
* Extract column references from the SELECT clause only
*/
private getSelectClauseColumns;
private processCollectTableSchema;
private processCTETableSchema;
private getCTEColumns;
private extractColumnsFromSelectItems;
private resolveWildcardInCTE;
private resolveQualifiedWildcard;
private resolveUnqualifiedWildcard;
private resolveTableWildcard;
private extractColumnsUsingCollector;
private removeDuplicates;
}