rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
79 lines • 3.31 kB
JavaScript
import { BinarySelectQuery, SimpleSelectQuery } from "../models/SelectQuery";
import { CTECollector } from "./CTECollector";
import { CTEBuilder } from "./CTEBuilder";
/**
* CTEInjector accepts a SelectQuery object and an array of CommonTables,
* and inserts Common Table Expressions into the query.
* For BinarySelectQuery, CTEs are inserted into the left query.
*
* Uses CTENameConflictResolver to resolve naming conflicts between CTEs.
*/
export class CTEInjector {
constructor() {
this.nameConflictResolver = new CTEBuilder();
this.cteCollector = new CTECollector();
}
/**
* Inserts Common Table Expressions into a SelectQuery object.
*
* @param query The query to inject CTEs into
* @param commonTables Array of CommonTables to be inserted
* @returns A new query with the injected CTEs
*/
inject(query, commonTables) {
// If the array is empty, return the query as is
if (commonTables.length === 0) {
return query;
}
// Collect CTEs from the query
commonTables.push(...this.cteCollector.collect(query));
// Use CTENameConflictResolver to resolve duplicates and sort in appropriate order
const resolvedWithCaluse = this.nameConflictResolver.build(commonTables);
// Process based on query type
if (query instanceof SimpleSelectQuery) {
return this.injectIntoSimpleQuery(query, resolvedWithCaluse);
}
else if (query instanceof BinarySelectQuery) {
return this.injectIntoBinaryQuery(query, resolvedWithCaluse);
}
// Unsupported query type
throw new Error("Unsupported query type");
}
/**
* Inserts Common Table Expressions into a SimpleSelectQuery.
*
* @param query The SimpleSelectQuery to inject CTEs into
* @param commonTables Array of CommonTables to be inserted
* @param needRecursive Boolean indicating if recursive WITH clause is needed
* @returns A new SimpleSelectQuery with the injected CTEs
*/
injectIntoSimpleQuery(query, withClause) {
if (query.withClause) {
throw new Error("The query already has a WITH clause. Please remove it before injecting new CTEs.");
}
// If the query doesn't have a WITH clause, set the new one
query.withClause = withClause;
return query;
}
/**
* Inserts Common Table Expressions into the left query of a BinarySelectQuery.
*
* @param query The BinarySelectQuery to inject CTEs into
* @param commonTables Array of CommonTables to be inserted
* @param needRecursive Boolean indicating if recursive WITH clause is needed
* @returns A new BinarySelectQuery with the injected CTEs
*/
injectIntoBinaryQuery(query, withClause) {
// Insert CTEs into the left query
if (query.left instanceof SimpleSelectQuery) {
this.injectIntoSimpleQuery(query.left, withClause);
return query;
}
else if (query.left instanceof BinarySelectQuery) {
this.injectIntoBinaryQuery(query.left, withClause);
return query;
}
throw new Error("Unsupported query type for BinarySelectQuery left side");
}
}
//# sourceMappingURL=CTEInjector.js.map