UNPKG

rawsql-ts

Version:

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

158 lines (157 loc) 5.17 kB
/** * Represents a position in the SQL text */ export interface Position { line: number; column: number; } /** * Represents a scope range for identifier renaming */ export interface ScopeRange { start: number; end: number; } /** * Result of renameability check */ export interface Renameability { canRename: boolean; currentName?: string; type?: 'cte' | 'table_alias' | 'column_alias'; scopeRange?: ScopeRange; reason?: string; } /** * Handles safe renaming of SQL identifiers within plain SQL strings. * * @example * ```typescript * const renamer = new SqlIdentifierRenamer(); * const sql = 'SELECT u.id FROM users u'; * const result = renamer.renameIdentifier(sql, 'u', 'users_alias'); * ``` * Related tests: packages/core/tests/transformers/SqlIdentifierRenamer.test.ts */ export declare class SqlIdentifierRenamer { /** * Safely renames identifiers in SQL string while preserving context * @param sql SQL string to modify * @param renames Map of original identifiers to new identifiers * @returns Modified SQL string with renamed identifiers */ renameIdentifiers(sql: string, renames: Map<string, string>): string; /** * Renames a single identifier in SQL string * @param sql SQL string to modify * @param oldIdentifier Original identifier to replace * @param newIdentifier New identifier to replace with * @returns Modified SQL string */ renameIdentifier(sql: string, oldIdentifier: string, newIdentifier: string): string; /** * Renames a single identifier within a specified scope range * @param sql SQL string to modify * @param oldIdentifier Original identifier to replace * @param newIdentifier New identifier to replace with * @param scopeRange Optional scope range to limit replacement * @returns Modified SQL string */ renameIdentifierInScope(sql: string, oldIdentifier: string, newIdentifier: string, scopeRange?: ScopeRange): string; /** * Checks if an identifier at the given position can be renamed * @param sql SQL string * @param position Position in the SQL text * @returns Renameability result */ checkRenameability(sql: string, position: Position): Renameability; /** * Renames identifier at the specified position * @param sql SQL string * @param position Position in the SQL text * @param newName New identifier name * @returns Modified SQL string */ renameAtPosition(sql: string, position: Position, newName: string): string; /** * Convert line/column position to character index */ private positionToCharIndex; /** * Check if position is inside a string literal */ private isInsideStringLiteral; /** * Get identifier at the specified character position */ private getIdentifierAtPosition; /** * Determine the type of identifier (improved logic) */ private determineIdentifierType; /** * Calculate scope range for the identifier */ private calculateScopeRange; /** * Safely replaces SQL identifiers while preserving word boundaries and context * Uses character-by-character parsing instead of regex for better maintainability * @param sql SQL string to modify * @param oldIdentifier Original identifier to replace * @param newIdentifier New identifier to replace with * @returns Modified SQL string */ private replaceIdentifierSafely; /** * Validates that the rename operation was successful * @param originalSql Original SQL string * @param modifiedSql Modified SQL string after rename * @param oldIdentifier Old identifier that was replaced * @param newIdentifier New identifier that was added * @returns True if rename appears successful */ validateRename(originalSql: string, modifiedSql: string, oldIdentifier: string, newIdentifier: string): boolean; /** * Extract and potentially replace quoted identifiers */ private extractAndReplaceQuotedIdentifier; /** * Extract and potentially replace bracketed identifiers [identifier] */ private extractAndReplaceBracketedIdentifier; /** * Extract quoted string (handles quotes) */ private extractQuotedString; /** * Extract line comment */ private extractLineComment; /** * Extract block comment */ private extractBlockComment; /** * Check if character code can start an identifier */ private isIdentifierStartChar; /** * Check if character code can be part of an identifier */ private isIdentifierChar; /** * Check if the identifier matches at the given position (case-insensitive) */ private matchesIdentifierAt; /** * Validate word boundaries */ private hasValidWordBoundaries; /** * Counts word boundary occurrences of an identifier in SQL * @param sql SQL string to search * @param identifier Identifier to count * @returns Number of occurrences */ private countWordOccurrences; }