rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
115 lines (114 loc) • 3.84 kB
TypeScript
import { LineColumn } from "../utils/LexemeCursor";
/**
* Result of smart rename operation
*/
export interface SmartRenameResult {
success: boolean;
originalSql: string;
newSql?: string;
renamerType: 'cte' | 'alias' | 'unknown';
originalName: string;
newName: string;
error?: string;
formattingPreserved?: boolean;
formattingMethod?: 'sql-identifier-renamer' | 'smart-renamer-only';
}
/**
* Smart renamer that detects whether a cursor points to a CTE or table alias and routes to the correct renamer.
*
* - CTE targets use CTERenamer so dependency graphs stay consistent.
* - Table aliases use AliasRenamer with scope detection.
* - Optional formatting preservation uses SqlIdentifierRenamer.
*
* @example
* ```typescript
* const renamer = new SmartRenamer();
* const sql = `WITH user_data AS (SELECT * FROM users) SELECT * FROM user_data`;
*
* const result = renamer.rename(sql, { line: 1, column: 8 }, 'customer_data');
*
* if (result.success) {
* console.log(result.newSql);
* }
* ```
* Related tests: packages/core/tests/transformers/SmartRenamer.demo.test.ts
*/
export declare class SmartRenamer {
private cteRenamer;
private aliasRenamer;
private identifierRenamer;
constructor();
/**
* Check if the token at the given position is renameable (CTE name or table alias).
* This is a lightweight check for GUI applications to determine if a rename context menu
* should be shown when right-clicking.
*
* @param sql - The complete SQL string
* @param position - Line and column position where user clicked (1-based)
* @returns Object indicating if renameable and what type of renamer would be used
*/
isRenameable(sql: string, position: LineColumn): {
renameable: boolean;
renamerType: 'cte' | 'alias' | 'none';
tokenName?: string;
reason?: string;
};
/**
* Automatically detect and rename CTE names or table aliases based on cursor position.
*
* @param sql - The complete SQL string
* @param position - Line and column position where user clicked (1-based)
* @param newName - The new name to assign
* @param options - Optional configuration { preserveFormatting?: boolean }
* @returns Result object with success status and details
*/
rename(sql: string, position: LineColumn, newName: string, options?: {
preserveFormatting?: boolean;
}): SmartRenameResult;
/**
* Detect whether an identifier is a CTE name or table alias.
* @private
*/
private detectRenamerType;
/**
* Check if identifier is a CTE name in the query.
* @private
*/
private isCTEName;
/**
* Attempts to perform rename using SqlIdentifierRenamer to preserve formatting.
* @private
*/
private attemptFormattingPreservationRename;
/**
* Perform standard rename without formatting preservation
* @private
*/
private performStandardRename;
/**
* Validates that the rename operation was successful
* @private
*/
private validateRenameResult;
/**
* Counts word boundary occurrences of a name in SQL
* @private
*/
private countWordOccurrences;
/**
* Create error result object.
* @private
*/
private createErrorResult;
/**
* Batch rename multiple identifiers with optional formatting preservation.
*
* @param sql - The complete SQL string
* @param renames - Map of old names to new names
* @param options - Optional configuration { preserveFormatting?: boolean }
* @returns Result with success status and details
*/
batchRename(sql: string, renames: Record<string, string>, options?: {
preserveFormatting?: boolean;
}): SmartRenameResult;
}