UNPKG

rawsql-ts

Version:

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

154 lines (153 loc) 6.15 kB
import { SqlPrintToken, SqlPrintTokenContainerType } from "../models/SqlPrintToken"; import { IndentCharOption, NewlineOption } from "./LinePrinter"; import { WithClauseStyle } from "./SqlFormatter"; /** * CommaBreakStyle determines how commas are placed in formatted SQL output. * - 'none': No line break for commas * - 'before': Line break before comma * - 'after': Line break after comma */ export type CommaBreakStyle = 'none' | 'before' | 'after'; /** * AndBreakStyle determines how AND operators are placed in formatted SQL output. * - 'none': No line break for AND * - 'before': Line break before AND * - 'after': Line break after AND */ export type AndBreakStyle = 'none' | 'before' | 'after'; /** * Options for configuring SqlPrinter formatting behavior */ export interface SqlPrinterOptions { /** Indent character (e.g., ' ' or '\t') */ indentChar?: IndentCharOption; /** Indent size (number of indentChar repetitions per level) */ indentSize?: number; /** Newline character (e.g., '\n' or '\r\n') */ newline?: NewlineOption; /** Comma break style: 'none', 'before', or 'after' */ commaBreak?: CommaBreakStyle; /** AND break style: 'none', 'before', or 'after' */ andBreak?: AndBreakStyle; /** Keyword case style: 'none', 'upper' | 'lower' */ keywordCase?: 'none' | 'upper' | 'lower'; /** Whether to export comments in the output (default: false for compatibility) */ exportComment?: boolean; /** Whether to use strict comment placement (only clause-level comments, default: false) */ strictCommentPlacement?: boolean; /** Container types that should increase indentation level */ indentIncrementContainerTypes?: SqlPrintTokenContainerType[]; /** WITH clause formatting style (default: 'standard') */ withClauseStyle?: WithClauseStyle; } /** * SqlPrinter formats a SqlPrintToken tree into a SQL string with flexible style options. * * This class provides various formatting options including: * - Indentation control (character and size) * - Line break styles for commas and AND operators * - Keyword case transformation * - Comment handling * - WITH clause formatting styles * * @example * const printer = new SqlPrinter({ * indentChar: ' ', * indentSize: 1, * keywordCase: 'upper', * commaBreak: 'after', * withClauseStyle: 'cte-oneline' * }); * const formatted = printer.print(sqlToken); */ export declare class SqlPrinter { /** Indent character (e.g., ' ' or '\\t') */ indentChar: IndentCharOption; /** Indent size (number of indentChar repetitions per level) */ indentSize: number; /** Newline character (e.g., '\\n' or '\\r\\n') */ newline: NewlineOption; /** Comma break style: 'none', 'before', or 'after' */ commaBreak: CommaBreakStyle; /** AND break style: 'none', 'before', or 'after' */ andBreak: AndBreakStyle; /** Keyword case style: 'none', 'upper' | 'lower' */ keywordCase: 'none' | 'upper' | 'lower'; /** Whether to export comments in the output (default: false for compatibility) */ exportComment: boolean; /** Whether to use strict comment placement (only clause-level comments, default: false) */ strictCommentPlacement: boolean; /** WITH clause formatting style (default: 'standard') */ withClauseStyle: WithClauseStyle; private linePrinter; private indentIncrementContainers; /** Track whether we are currently inside a WITH clause for full-oneline formatting */ private insideWithClause; /** * @param options Optional style settings for pretty printing */ constructor(options?: SqlPrinterOptions); /** * Converts a SqlPrintToken tree to a formatted SQL string. * @param token The root SqlPrintToken to format * @param level Initial indentation level (default: 0) * @returns Formatted SQL string * @example * const printer = new SqlPrinter({ indentChar: ' ', keywordCase: 'upper' }); * const formatted = printer.print(sqlToken); */ print(token: SqlPrintToken, level?: number): string; private appendToken; /** * Determines if a token should be skipped during printing. * Tokens are skipped if they have no content and no inner tokens, * except for special token types that have semantic meaning despite empty text. */ private shouldSkipToken; private applyKeywordCase; private handleKeywordToken; private handleCommaToken; private handleAndOperatorToken; private handleJoinClauseToken; /** * Handles space tokens with context-aware filtering. * Skips spaces in CommentBlocks when in specific CTE modes to prevent duplication. */ private handleSpaceToken; /** * Determines whether to skip space tokens in CommentBlocks. * Prevents duplicate spacing in CTE full-oneline mode only. */ private shouldSkipCommentBlockSpace; /** * Handles commentNewline tokens with conditional newline behavior. * In multiline mode (newline !== ' '), adds a newline after comments. * In oneliner mode (newline === ' '), does nothing to keep comments on same line. * Skips newlines in CTE modes (full-oneline, cte-oneline) to maintain one-line format. */ private handleCommentNewlineToken; /** * Determines whether to skip commentNewline tokens. * Skips in CTE modes to maintain one-line formatting. */ private shouldSkipCommentNewline; /** * Determines if the printer is in oneliner mode. * Oneliner mode uses single spaces instead of actual newlines. */ private isOnelineMode; /** * Handles CTE tokens with one-liner formatting. * Creates a nested SqlPrinter instance for proper CTE oneline formatting. */ private handleCteOnelineToken; /** * Creates a SqlPrinter instance configured for CTE oneline formatting. */ private createCteOnelinePrinter; /** * Removes duplicate consecutive spaces while preserving single spaces. * Simple and safe space normalization for CTE oneline mode. */ private cleanDuplicateSpaces; }