rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
53 lines • 2.18 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParameterDetector = void 0;
const ParameterCollector_1 = require("../transformers/ParameterCollector");
/**
* Utility class for detecting hardcoded parameters in SQL queries.
*
* This class helps identify existing ParameterExpression nodes in parsed SQL,
* which represent hardcoded parameters like :param_name in the original SQL.
*/
class ParameterDetector {
/**
* Extracts all hardcoded parameter names from a parsed SQL query.
* @param query The parsed SQL query (must be a SqlComponent)
* @returns Array of parameter names found in the query
*/
static extractParameterNames(query) {
const params = ParameterCollector_1.ParameterCollector.collect(query);
return params.map(p => p.name.value);
}
/**
* Checks if a parameter with the given name exists in the query.
* @param query The parsed SQL query (must be a SqlComponent)
* @param parameterName The parameter name to check
* @returns True if the parameter exists, false otherwise
*/
static hasParameter(query, parameterName) {
const paramNames = this.extractParameterNames(query);
return paramNames.includes(parameterName);
}
/**
* Separates filter options into hardcoded parameters and dynamic column filters.
* @param query The parsed SQL query (must be a SqlComponent)
* @param filter The filter object from DynamicQueryBuilder options
* @returns Object with separated hardcoded and dynamic filters
*/
static separateFilters(query, filter) {
const hardcodedParamNames = this.extractParameterNames(query);
const hardcodedParams = {};
const dynamicFilters = {};
for (const [key, value] of Object.entries(filter)) {
if (hardcodedParamNames.includes(key)) {
hardcodedParams[key] = value;
}
else {
dynamicFilters[key] = value;
}
}
return { hardcodedParams, dynamicFilters };
}
}
exports.ParameterDetector = ParameterDetector;
//# sourceMappingURL=ParameterDetector.js.map
;