UNPKG

rawsql-ts

Version:

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

68 lines (67 loc) 2.67 kB
import { CommaBreakStyle, AndBreakStyle } from './SqlPrinter'; import { IndentCharOption, NewlineOption } from './LinePrinter'; import { SqlComponent } from '../models/SqlComponent'; export declare const VALID_PRESETS: readonly ["mysql", "postgres", "sqlserver", "sqlite"]; export type PresetName = (typeof VALID_PRESETS)[number]; /** * WithClauseStyle determines how WITH clauses are formatted. * - 'standard': Normal formatting with proper indentation * - 'cte-oneline': Individual CTEs are formatted as one-liners * - 'full-oneline': Entire WITH clause is formatted as one line */ export type WithClauseStyle = 'standard' | 'cte-oneline' | 'full-oneline'; /** * Options for SqlFormatter configuration * @public */ export interface SqlFormatterOptions { /** Database preset for formatting style ('mysql', 'postgres', 'sqlserver', 'sqlite') */ preset?: PresetName; /** Custom identifier escape characters (e.g., {start: '"', end: '"'} for PostgreSQL) */ identifierEscape?: { start: string; end: string; }; /** Parameter symbol configuration for SQL parameters */ parameterSymbol?: string | { start: string; end: string; }; /** Style for parameter formatting */ parameterStyle?: 'anonymous' | 'indexed' | 'named'; /** Number of spaces for indentation */ indentSize?: number; /** Character to use for indentation ('space' or 'tab') */ indentChar?: IndentCharOption; /** Newline character style */ newline?: NewlineOption; /** Case transformation for SQL keywords */ keywordCase?: 'none' | 'upper' | 'lower'; /** Style for comma line breaks */ commaBreak?: CommaBreakStyle; /** Style for AND/OR line breaks */ andBreak?: AndBreakStyle; /** Whether to export comments in formatted output */ exportComment?: boolean; /** Whether to only export comments from clause-level keywords */ strictCommentPlacement?: boolean; /** Formatting style for WITH clauses */ withClauseStyle?: WithClauseStyle; } /** * SqlFormatter class combines parsing and printing of SQL queries into a single interface. */ export declare class SqlFormatter { private parser; private printer; constructor(options?: SqlFormatterOptions); /** * Formats a SQL query string with the given parameters. * @param sqlText The SQL query string to format. * @param parameters A dictionary of parameters to replace in the query. * @returns An object containing the formatted SQL string and the parameters. */ format(sql: SqlComponent): { formattedSql: string; params: any[] | Record<string, any>; }; }