UNPKG

rawsql-ts

Version:

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

62 lines (61 loc) 2.58 kB
import { WithClause } from "../models/Clause"; import { Lexeme } from "../models/Lexeme"; /** * Parser for SQL WITH clauses (Common Table Expressions - CTEs). * Parses only the WITH clause portion of SQL, not the entire query. * * **Note**: For most use cases, use `SelectQueryParser` which provides more comprehensive SQL parsing. * This parser should only be used for the special case where you need to analyze only the WITH clause portion. * * @example * ```typescript * // Parses only the WITH clause, not the following SELECT * const sql = "WITH recursive_cte AS (SELECT 1 as n UNION SELECT n+1 FROM recursive_cte WHERE n < 10)"; * const withClause = WithClauseParser.parse(sql); * console.log(withClause.recursive); // true * console.log(withClause.tables.length); // 1 * ``` */ export declare class WithClauseParser { /** * Parses a SQL string containing only a WITH clause into a WithClause AST. * The input should contain only the WITH clause, not the subsequent main query. * * @param query - The SQL string containing only the WITH clause * @returns The parsed WithClause object * @throws Error if the syntax is invalid or there are unexpected tokens after the WITH clause * * @example * ```typescript * // Correct: Only the WITH clause * const sql = "WITH users_data AS (SELECT id, name FROM users)"; * const withClause = WithClauseParser.parse(sql); * * // Error: Contains SELECT after WITH clause * // const badSql = "WITH users_data AS (SELECT id, name FROM users) SELECT * FROM users_data"; * ``` */ static parse(query: string): WithClause; /** * Parses a WITH clause from an array of lexemes starting at the specified index. * * @param lexemes - Array of lexemes to parse from * @param index - Starting index in the lexemes array * @returns Object containing the parsed WithClause and the new index position * @throws Error if the syntax is invalid or WITH keyword is not found * * @example * ```typescript * const tokenizer = new SqlTokenizer("WITH cte AS (SELECT 1)"); * const lexemes = tokenizer.readLexmes(); * const result = WithClauseParser.parseFromLexeme(lexemes, 0); * console.log(result.value.tables.length); // 1 * console.log(result.newIndex); // position after the WITH clause * ``` */ static parseFromLexeme(lexemes: Lexeme[], index: number): { value: WithClause; newIndex: number; headerComments: string[] | null; }; }