UNPKG

rawsql-ts

Version:

[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.

149 lines 6.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BinarySelectQuery = void 0; const Clause_1 = require("./Clause"); const SqlComponent_1 = require("./SqlComponent"); const ValueComponent_1 = require("./ValueComponent"); const CTENormalizer_1 = require("../transformers/CTENormalizer"); const SelectQueryParser_1 = require("../parsers/SelectQueryParser"); const ParameterHelper_1 = require("../utils/ParameterHelper"); /** * Represents a binary SELECT query (e.g., UNION, INTERSECT, EXCEPT). */ class BinarySelectQuery extends SqlComponent_1.SqlComponent { constructor(left, operator, right) { super(); this.left = left; this.operator = new ValueComponent_1.RawString(operator); this.right = right; } /** * Appends another query to this binary query using UNION as the operator. * This creates a new BinarySelectQuery where the left side is this binary query * and the right side is the provided query. * * @param query The query to append with UNION * @returns A new BinarySelectQuery representing "(this) UNION query" */ union(query) { return this.appendSelectQuery('union', query); } /** * Appends another query to this binary query using UNION ALL as the operator. * This creates a new BinarySelectQuery where the left side is this binary query * and the right side is the provided query. * * @param query The query to append with UNION ALL * @returns A new BinarySelectQuery representing "(this) UNION ALL query" */ unionAll(query) { return this.appendSelectQuery('union all', query); } /** * Appends another query to this binary query using INTERSECT as the operator. * This creates a new BinarySelectQuery where the left side is this binary query * and the right side is the provided query. * * @param query The query to append with INTERSECT * @returns A new BinarySelectQuery representing "(this) INTERSECT query" */ intersect(query) { return this.appendSelectQuery('intersect', query); } /** * Appends another query to this binary query using INTERSECT ALL as the operator. * This creates a new BinarySelectQuery where the left side is this binary query * and the right side is the provided query. * * @param query The query to append with INTERSECT ALL * @returns A new BinarySelectQuery representing "(this) INTERSECT ALL query" */ intersectAll(query) { return this.appendSelectQuery('intersect all', query); } /** * Appends another query to this binary query using EXCEPT as the operator. * This creates a new BinarySelectQuery where the left side is this binary query * and the right side is the provided query. * * @param query The query to append with EXCEPT * @returns A new BinarySelectQuery representing "(this) EXCEPT query" */ except(query) { return this.appendSelectQuery('except', query); } /** * Appends another query to this binary query using EXCEPT ALL as the operator. * This creates a new BinarySelectQuery where the left side is this binary query * and the right side is the provided query. * * @param query The query to append with EXCEPT ALL * @returns A new BinarySelectQuery representing "(this) EXCEPT ALL query" */ exceptAll(query) { return this.appendSelectQuery('except all', query); } /** * Appends another query to this binary query using the specified operator. * This creates a new BinarySelectQuery where the left side is this binary query * and the right side is the provided query. * * @param operator SQL operator to use (e.g. 'union', 'union all', 'intersect', 'except') * @param query The query to append with the specified operator * @returns A new BinarySelectQuery representing "(this) [operator] query" */ appendSelectQuery(operator, query) { this.left = new BinarySelectQuery(this.left, this.operator.value, this.right); this.operator = new ValueComponent_1.RawString(operator); this.right = query; CTENormalizer_1.CTENormalizer.normalize(this); return this; } /** * Appends another query to this binary query using UNION as the operator, accepting a raw SQL string. * This method parses the SQL string and appends the resulting query using UNION. * @param sql The SQL string to parse and union * @returns A new BinarySelectQuery representing "(this) UNION (parsed query)" */ unionRaw(sql) { const parsedQuery = SelectQueryParser_1.SelectQueryParser.parse(sql); return this.union(parsedQuery); } unionAllRaw(sql) { const parsedQuery = SelectQueryParser_1.SelectQueryParser.parse(sql); return this.unionAll(parsedQuery); } intersectRaw(sql) { const parsedQuery = SelectQueryParser_1.SelectQueryParser.parse(sql); return this.intersect(parsedQuery); } intersectAllRaw(sql) { const parsedQuery = SelectQueryParser_1.SelectQueryParser.parse(sql); return this.intersectAll(parsedQuery); } exceptRaw(sql) { const parsedQuery = SelectQueryParser_1.SelectQueryParser.parse(sql); return this.except(parsedQuery); } exceptAllRaw(sql) { const parsedQuery = SelectQueryParser_1.SelectQueryParser.parse(sql); return this.exceptAll(parsedQuery); } // Returns a SourceExpression wrapping this query as a subquery source. // Optionally takes an alias name (default: "subq") toSource(alias = "subq") { return new Clause_1.SourceExpression(new Clause_1.SubQuerySource(this), new Clause_1.SourceAliasExpression(alias, null)); } /** * Sets the value of a parameter by name in this query. * @param name Parameter name * @param value Value to set */ setParameter(name, value) { ParameterHelper_1.ParameterHelper.set(this, name, value); return this; } } exports.BinarySelectQuery = BinarySelectQuery; BinarySelectQuery.kind = Symbol("BinarySelectQuery"); //# sourceMappingURL=BinarySelectQuery.js.map