UNPKG

rawsql-ts

Version:

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

98 lines (97 loc) 3.93 kB
import { Lexeme } from "../models/Lexeme"; import { SelectQuery } from "../models/SelectQuery"; export interface ParseAnalysisResult { success: boolean; query?: SelectQuery; error?: string; errorPosition?: number; remainingTokens?: string[]; } export declare class SelectQueryParser { static parse(query: string): SelectQuery; /** * Analyzes SQL string for parsing without throwing errors. * Returns a result object containing the parsed query on success, * or error information if parsing fails. * * @param query SQL string to analyze * @returns Analysis result containing query, error information, and success status */ /** * Calculate character position from token index by finding token in original query */ private static calculateCharacterPosition; static analyze(query: string): ParseAnalysisResult; /** * Asynchronously parse SQL string to AST. * This method wraps the synchronous parse logic in a Promise for future extensibility. * @param query SQL string to parse * @returns Promise<SelectQuery> */ static parseAsync(query: string): Promise<SelectQuery>; private static unionCommandSet; private static selectCommandSet; /** * Transfer headerComments from source query to target query and clear from source * @param source Source query to transfer headerComments from * @param target Target query to receive headerComments */ private static transferHeaderComments; static parseFromLexeme(lexemes: Lexeme[], index: number): { value: SelectQuery; newIndex: number; }; private static parseSimpleSelectQuery; private static parseValuesQuery; /** * Get the CTE name at the specified cursor position. * * This method provides a simple interface for retrieving the CTE name * based on a 1D cursor position in the SQL text. * * @deprecated Use CTERegionDetector.getCursorCte() instead for better API consistency * @param sql - The SQL string to analyze * @param cursorPosition - The cursor position (0-based character offset) * @returns The CTE name if cursor is in a CTE, null otherwise * * @example * ```typescript * const sql = `WITH users AS (SELECT * FROM table) SELECT * FROM users`; * const cteName = SelectQueryParser.getCursorCte(sql, 25); * console.log(cteName); // "users" * ``` */ static getCursorCte(sql: string, cursorPosition: number): string | null; /** * Get the CTE name at the specified 2D coordinates (line, column). * * This method provides a convenient interface for editor integrations * that work with line/column coordinates instead of character positions. * * @deprecated Use CTERegionDetector.getCursorCteAt() instead for better API consistency * @param sql - The SQL string to analyze * @param line - The line number (1-based) * @param column - The column number (1-based) * @returns The CTE name if cursor is in a CTE, null otherwise * * @example * ```typescript * const sql = `WITH users AS (\n SELECT * FROM table\n) SELECT * FROM users`; * const cteName = SelectQueryParser.getCursorCteAt(sql, 2, 5); * console.log(cteName); // "users" * ``` */ static getCursorCteAt(sql: string, line: number, column: number): string | null; /** * Convert character position to line/column coordinates. * * @deprecated Use CTERegionDetector.positionToLineColumn() instead for better API consistency * @param text - The text to analyze * @param position - The character position (0-based) * @returns Object with line and column (1-based), or null if invalid position */ static positionToLineColumn(text: string, position: number): { line: number; column: number; } | null; }