rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
141 lines (140 loc) • 6.7 kB
TypeScript
import { SourceExpression } from "./Clause";
import type { SelectQuery, InsertQueryConversionOptions, UpdateQueryConversionOptions, DeleteQueryConversionOptions, MergeQueryConversionOptions } from "./SelectQuery";
import { SqlComponent } from "./SqlComponent";
import { RawString, SqlParameterValue } from "./ValueComponent";
import { SimpleSelectQuery } from "./SimpleSelectQuery";
import type { InsertQuery } from "./InsertQuery";
import type { UpdateQuery } from "./UpdateQuery";
import type { DeleteQuery } from "./DeleteQuery";
import type { MergeQuery } from "./MergeQuery";
/**
* Represents a binary SELECT expression (UNION/INTERSECT/EXCEPT) composed from two SelectQuery values.
*
* @example
* ```typescript
* const parts = [
* SelectQueryParser.parse('SELECT id, name FROM users').toSimpleQuery(),
* SelectQueryParser.parse('SELECT id, name FROM archived_users').toSimpleQuery()
* ];
* const unionQuery = QueryBuilder.buildBinaryQuery(parts, 'union');
* ```
* Related tests: packages/core/tests/models/SelectQueryUnion.test.ts
*/
export declare class BinarySelectQuery extends SqlComponent implements SelectQuery {
static kind: symbol;
readonly __selectQueryType: 'SelectQuery';
headerComments: string[] | null;
left: SelectQuery;
operator: RawString;
right: SelectQuery;
constructor(left: SelectQuery, operator: string, right: SelectQuery);
/**
* 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: SelectQuery): BinarySelectQuery;
/**
* 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: SelectQuery): BinarySelectQuery;
/**
* 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: SelectQuery): BinarySelectQuery;
/**
* 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: SelectQuery): BinarySelectQuery;
/**
* 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: SelectQuery): BinarySelectQuery;
/**
* 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: SelectQuery): BinarySelectQuery;
/**
* 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: string, query: SelectQuery): BinarySelectQuery;
/**
* 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: string): BinarySelectQuery;
unionAllRaw(sql: string): BinarySelectQuery;
intersectRaw(sql: string): BinarySelectQuery;
intersectAllRaw(sql: string): BinarySelectQuery;
exceptRaw(sql: string): BinarySelectQuery;
exceptAllRaw(sql: string): BinarySelectQuery;
/**
* Converts this query into an INSERT statement definition.
* @remarks The underlying simple query may be reordered so that column order matches the requested insert columns.
*/
toInsertQuery(options: InsertQueryConversionOptions): InsertQuery;
/**
* Converts this query into an UPDATE statement definition.
* @remarks The conversion can reorder the SELECT list produced by {@link toSimpleQuery}.
*/
toUpdateQuery(options: UpdateQueryConversionOptions): UpdateQuery;
/**
* Converts this query into a DELETE statement definition.
* @remarks The conversion can reorder the SELECT list produced by {@link toSimpleQuery}.
*/
toDeleteQuery(options: DeleteQueryConversionOptions): DeleteQuery;
/**
* Converts this query into a MERGE statement definition.
* @remarks The conversion can reorder the SELECT list produced by {@link toSimpleQuery}.
*/
toMergeQuery(options: MergeQueryConversionOptions): MergeQuery;
toSource(alias?: string): SourceExpression;
/**
* Sets the value of a parameter by name in this query.
* @param name Parameter name
* @param value Value to set
*/
setParameter(name: string, value: SqlParameterValue): this;
/**
* Converts this BinarySelectQuery to a SimpleSelectQuery using QueryBuilder.
* This enables CTE management on binary queries by wrapping them as subqueries.
* @returns A SimpleSelectQuery representation of this binary query
*/
toSimpleQuery(): SimpleSelectQuery;
}