rawsql-ts
Version:
High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
47 lines • 1.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectQueryWithClauseHelper = void 0;
const SelectQuery_1 = require("../models/SelectQuery");
/**
* Utility to manage WITH clause placement for statements that promote the
* CTE definitions outside of the SELECT body (for example, INSERT).
*/
class SelectQueryWithClauseHelper {
static getWithClause(selectQuery) {
const owner = this.findClauseOwner(selectQuery);
if (!owner) {
return null;
}
return owner.withClause;
}
static setWithClause(selectQuery, withClause) {
const owner = this.findClauseOwner(selectQuery);
if (!owner) {
throw new Error("Cannot attach WITH clause to the provided select query.");
}
owner.withClause = withClause;
}
static detachWithClause(selectQuery) {
const owner = this.findClauseOwner(selectQuery);
if (!owner) {
return null;
}
const clause = owner.withClause;
owner.withClause = null;
return clause;
}
static findClauseOwner(selectQuery) {
if (!selectQuery) {
return null;
}
if (selectQuery instanceof SelectQuery_1.SimpleSelectQuery || selectQuery instanceof SelectQuery_1.ValuesQuery) {
return selectQuery;
}
if (selectQuery instanceof SelectQuery_1.BinarySelectQuery) {
return this.findClauseOwner(selectQuery.left);
}
throw new Error("Unsupported select query type for WITH clause management.");
}
}
exports.SelectQueryWithClauseHelper = SelectQueryWithClauseHelper;
//# sourceMappingURL=SelectQueryWithClauseHelper.js.map