UNPKG

rawsql-ts

Version:

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

51 lines 4.01 kB
import { SqlPrintTokenParser, PRESETS } from '../parsers/SqlPrintTokenParser'; import { SqlPrinter } from './SqlPrinter'; import { resolveIdentifierEscapeOption } from './FormatOptionResolver'; // Define valid preset names as a union type export const VALID_PRESETS = ['mysql', 'postgres', 'sqlserver', 'sqlite']; /** * High level facade that parses a SqlComponent, applies formatting rules, and prints the final SQL text. * * @example * ```typescript * const formatter = new SqlFormatter({ keywordCase: 'lower', withClauseStyle: 'cte-oneline' }); * const query = SelectQueryParser.parse('WITH cte AS (SELECT id FROM users) SELECT * FROM cte'); * const { formattedSql } = formatter.format(query); * ``` * Related tests: packages/core/tests/transformers/SqlFormatter.case.test.ts */ export class SqlFormatter { constructor(options = {}) { var _a, _b, _c, _d, _e, _f; const presetConfig = options.preset ? PRESETS[options.preset] : undefined; if (options.preset && !presetConfig) { throw new Error(`Invalid preset: ${options.preset}`); // Throw error for invalid preset } // Normalize identifier escape names into actual delimiter pairs before configuring the parser. const resolvedIdentifierEscape = resolveIdentifierEscapeOption((_a = options.identifierEscape) !== null && _a !== void 0 ? _a : presetConfig === null || presetConfig === void 0 ? void 0 : presetConfig.identifierEscape); const parserOptions = Object.assign(Object.assign({}, presetConfig), { identifierEscape: resolvedIdentifierEscape !== null && resolvedIdentifierEscape !== void 0 ? resolvedIdentifierEscape : presetConfig === null || presetConfig === void 0 ? void 0 : presetConfig.identifierEscape, parameterSymbol: (_b = options.parameterSymbol) !== null && _b !== void 0 ? _b : presetConfig === null || presetConfig === void 0 ? void 0 : presetConfig.parameterSymbol, parameterStyle: (_c = options.parameterStyle) !== null && _c !== void 0 ? _c : presetConfig === null || presetConfig === void 0 ? void 0 : presetConfig.parameterStyle, castStyle: (_d = options.castStyle) !== null && _d !== void 0 ? _d : presetConfig === null || presetConfig === void 0 ? void 0 : presetConfig.castStyle, joinConditionOrderByDeclaration: options.joinConditionOrderByDeclaration }); const constraintStyle = (_f = (_e = options.constraintStyle) !== null && _e !== void 0 ? _e : presetConfig === null || presetConfig === void 0 ? void 0 : presetConfig.constraintStyle) !== null && _f !== void 0 ? _f : 'postgres'; const parserConfig = Object.assign(Object.assign({}, parserOptions), { constraintStyle }); this.parser = new SqlPrintTokenParser(Object.assign({}, parserConfig)); // Normalize legacy boolean comment export flags before configuring the printer. const normalizedExportComment = options.exportComment === true ? 'full' : options.exportComment === false ? 'none' : options.exportComment; const printerOptions = Object.assign(Object.assign({}, options), { exportComment: normalizedExportComment, parenthesesOneLine: options.parenthesesOneLine, betweenOneLine: options.betweenOneLine, valuesOneLine: options.valuesOneLine, joinOneLine: options.joinOneLine, caseOneLine: options.caseOneLine, subqueryOneLine: options.subqueryOneLine, indentNestedParentheses: options.indentNestedParentheses }); this.printer = new SqlPrinter(printerOptions); } /** * 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) { const { token, params } = this.parser.parse(sql); const formattedSql = this.printer.print(token); return { formattedSql, params }; } } //# sourceMappingURL=SqlFormatter.js.map