import type { SQLParserDialect } from '@memberjunction/sql-dialect'; /** * Result of analyzing a SQL string for top-level ORDER BY clauses. */ export interface OrderByAnalysis { /** Character positions of all top-level ORDER BY keywords in the original SQL */ Positions: number[]; /** SQL with the last top-level ORDER BY clause stripped (or original if none found) */ SqlWithoutOrderBy: string; /** The extracted ORDER BY clause text (everything after "ORDER BY "), or null if none */ OrderByClause: string | null; /** Whether the ORDER BY is legal in CTE context (TOP, OFFSET, or FOR XML present) */ IsLegalInCTE: boolean; } /** * Shared ORDER BY analysis for both the composition engine and the paging engine. * * Uses a two-tier strategy: * **Tier 1**: `node-sql-parser` AST parsing (with Nunjucks placeholder preprocessing * if MJ tokens are detected). Most accurate; handles UNION/EXCEPT chains, window * functions, and CTE-internal ORDER BYs via the AST. * * **Tier 2**: `MJLexer.Tokenize` → scan `SQL_TEXT` tokens with **carried** * `inString` / `inBlockComment` / `inLineComment` state across MJ token boundaries. * Fixes Bug D (Nunjucks expression inside SQL string literal breaks ORDER BY detection). * * This utility is the single implementation of ORDER BY analysis shared by both * `queryCompositionEngine.ts` and `queryPagingEngine.ts`. Both engines delegate * here instead of maintaining separate scanners. */ /** * Analyzes SQL for top-level ORDER BY clauses, handling both SQL+Nunjucks * (composition engine's input) and clean SQL (paging engine's input). * * @param sql - SQL to analyze (may contain MJ template tokens) * @param parserDialect - node-sql-parser dialect ('TransactSQL' or 'PostgresQL') * @returns Full analysis including positions, stripped SQL, and CTE legality */ export declare function AnalyzeTopLevelOrderBy(sql: string, dialect: SQLParserDialect): OrderByAnalysis; /** * Checks whether the given SQL has any top-level ORDER BY clause. * Convenience wrapper around {@link AnalyzeTopLevelOrderBy}. */ export declare function HasTopLevelOrderBy(sql: string, dialect: SQLParserDialect): boolean; /** * Extracts the ORDER BY clause and returns SQL without it. * Convenience wrapper around {@link AnalyzeTopLevelOrderBy}. */ export declare function ExtractOrderBy(sql: string, dialect: SQLParserDialect): { sqlWithoutOrder: string; orderByClause: string | null; }; /** * Walks the `_next` chain (UNION/EXCEPT/INTERSECT) to find the statement * that carries the ORDER BY clause. This is the single shared implementation * replacing the duplicated versions in both engines. */ export declare function findOrderByStatement(stmt: Record): Record | null; /** * Checks AST properties to determine if ORDER BY is legal in a CTE context. * TOP, OFFSET/LIMIT, or FOR XML make ORDER BY legal inside a CTE. */ export declare function isOrderByLegalInCTE(stmt: Record): boolean; //# sourceMappingURL=orderByAnalyzer.d.ts.map