rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
145 lines • 5.92 kB
JavaScript
import { SourceExpression, SubQuerySource, SourceAliasExpression } from "./Clause";
import { SqlComponent } from "./SqlComponent";
import { RawString } from "./ValueComponent";
import { CTENormalizer } from "../transformers/CTENormalizer";
import { SelectQueryParser } from "../parsers/SelectQueryParser";
import { ParameterHelper } from "../utils/ParameterHelper";
/**
* Represents a binary SELECT query (e.g., UNION, INTERSECT, EXCEPT).
*/
export class BinarySelectQuery extends SqlComponent {
constructor(left, operator, right) {
super();
this.left = left;
this.operator = new 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 RawString(operator);
this.right = query;
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.parse(sql);
return this.union(parsedQuery);
}
unionAllRaw(sql) {
const parsedQuery = SelectQueryParser.parse(sql);
return this.unionAll(parsedQuery);
}
intersectRaw(sql) {
const parsedQuery = SelectQueryParser.parse(sql);
return this.intersect(parsedQuery);
}
intersectAllRaw(sql) {
const parsedQuery = SelectQueryParser.parse(sql);
return this.intersectAll(parsedQuery);
}
exceptRaw(sql) {
const parsedQuery = SelectQueryParser.parse(sql);
return this.except(parsedQuery);
}
exceptAllRaw(sql) {
const parsedQuery = 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 SourceExpression(new SubQuerySource(this), new 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.set(this, name, value);
return this;
}
}
BinarySelectQuery.kind = Symbol("BinarySelectQuery");
//# sourceMappingURL=BinarySelectQuery.js.map