rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
77 lines • 4.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SqlFormatter = exports.VALID_PRESETS = void 0;
const SqlPrintTokenParser_1 = require("../parsers/SqlPrintTokenParser");
const SqlPrinter_1 = require("./SqlPrinter");
const FormatOptionResolver_1 = require("./FormatOptionResolver");
// Define valid preset names as a union type
exports.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
*/
class SqlFormatter {
constructor(options = {}) {
var _a, _b, _c, _d, _e, _f;
const presetConfig = options.preset ? SqlPrintTokenParser_1.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 = (0, FormatOptionResolver_1.resolveIdentifierEscapeOption)((_a = options.identifierEscape) !== null && _a !== void 0 ? _a : presetConfig === null || presetConfig === void 0 ? void 0 : presetConfig.identifierEscape);
const parserOptions = {
...presetConfig, // Apply preset configuration
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 = {
...parserOptions,
constraintStyle,
};
this.parser = new SqlPrintTokenParser_1.SqlPrintTokenParser({
...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 = {
...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_1.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 };
}
}
exports.SqlFormatter = SqlFormatter;
//# sourceMappingURL=SqlFormatter.js.map