rawsql-ts
Version:
High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
43 lines • 1.53 kB
JavaScript
import { BinarySelectQuery, SimpleSelectQuery, ValuesQuery } from '../models/SelectQuery';
/**
* Utility to manage WITH clause placement for statements that promote the
* CTE definitions outside of the SELECT body (for example, INSERT).
*/
export 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 SimpleSelectQuery || selectQuery instanceof ValuesQuery) {
return selectQuery;
}
if (selectQuery instanceof BinarySelectQuery) {
return this.findClauseOwner(selectQuery.left);
}
throw new Error("Unsupported select query type for WITH clause management.");
}
}
//# sourceMappingURL=SelectQueryWithClauseHelper.js.map