UNPKG

rawsql-ts

Version:

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

120 lines (119 loc) 5.93 kB
import { UpdateQuery } from '../models/UpdateQuery'; import { DeleteQuery } from '../models/DeleteQuery'; import { MergeQuery } from '../models/MergeQuery'; import { BinarySelectQuery, SelectQuery, SimpleSelectQuery } from "../models/SelectQuery"; import { CreateTableQuery } from "../models/CreateTableQuery"; import { InsertQuery } from "../models/InsertQuery"; import type { InsertQueryConversionOptions, UpdateQueryConversionOptions, DeleteQueryConversionOptions, MergeQueryConversionOptions } from "../models/SelectQuery"; /** * QueryBuilder provides static methods to build or convert various SQL query objects. */ export declare class QueryBuilder { /** * Builds a BinarySelectQuery by combining an array of SelectQuery using the specified operator. * Throws if less than two queries are provided. * @param queries Array of SelectQuery to combine * @param operator SQL operator to use (e.g. 'union', 'union all', 'intersect', 'except') * @returns BinarySelectQuery */ static buildBinaryQuery(queries: SelectQuery[], operator: string): BinarySelectQuery; private constructor(); /** * Converts a SELECT query to a standard SimpleSelectQuery form. * @param query The query to convert * @returns A SimpleSelectQuery */ static buildSimpleQuery(query: SelectQuery): SimpleSelectQuery; private static buildSimpleBinaryQuery; /** * Extracts ORDER BY clause from the rightmost query in a binary query tree and removes it. * This clarifies the semantics by moving the ORDER BY from the ambiguous position * in the UNION to the explicit outer SimpleSelectQuery level. * * NOTE: ORDER BY in UNION context applies to the entire result set, not individual subqueries. * Therefore, table prefixes (e.g., "a.column") in ORDER BY are invalid SQL and would cause * syntax errors. Valid ORDER BY clauses should only reference column names without prefixes * or use positional notation (ORDER BY 1, 2). Since we only process valid SQL, the current * implementation correctly handles legitimate cases without additional prefix processing. * * @param query BinarySelectQuery to process * @returns Extracted OrderByClause or null if none found */ private static extractAndRemoveOrderByFromBinaryQuery; /** * Recursively finds and removes ORDER BY from the rightmost query in a binary tree. * * @param query Current query being processed * @returns Extracted OrderByClause or null */ private static findAndRemoveRightmostOrderBy; /** * Converts a ValuesQuery to a SimpleSelectQuery with sequentially numbered columns or user-specified columns * * @param query The VALUES query to convert * @param columns Optional: column names * @returns A SimpleSelectQuery */ private static buildSimpleValuesQuery; /** * Creates a SELECT clause with a single * (all columns) item * * @returns A SELECT clause with * */ private static createSelectAllClause; /** * Converts a SELECT query to a CREATE TABLE query (CREATE [TEMPORARY] TABLE ... AS SELECT ...) * @param query The SELECT query to use as the source * @param tableName The name of the table to create * @param isTemporary If true, creates a temporary table * @returns A CreateTableQuery instance */ static buildCreateTableQuery(query: SelectQuery, tableName: string, isTemporary?: boolean, ifNotExists?: boolean): CreateTableQuery; /** * Converts a SELECT query to an INSERT query (INSERT INTO ... SELECT ...). */ static buildInsertQuery(selectQuery: SimpleSelectQuery, targetOrOptions: string | InsertQueryConversionOptions, explicitColumns?: string[]): InsertQuery; /** * Converts an INSERT ... VALUES query into INSERT ... SELECT form using UNION ALL. * @param insertQuery The VALUES-based InsertQuery to convert. * @returns A new InsertQuery that selects rows instead of using VALUES. */ static convertInsertValuesToSelect(insertQuery: InsertQuery): InsertQuery; /** * Converts an INSERT ... SELECT (optionally with UNION ALL) into INSERT ... VALUES form. * @param insertQuery The SELECT-based InsertQuery to convert. * @returns A new InsertQuery that uses VALUES tuples. */ static convertInsertSelectToValues(insertQuery: InsertQuery): InsertQuery; /** * Builds an UPDATE query from a SELECT query and conversion options. */ static buildUpdateQuery(selectQuery: SimpleSelectQuery, selectSourceOrOptions: string | UpdateQueryConversionOptions, updateTableExprRaw?: string, primaryKeys?: string | string[]): UpdateQuery; /** * Builds a DELETE query that deletes the rows matched by the SELECT query output. */ static buildDeleteQuery(selectQuery: SimpleSelectQuery, options: DeleteQueryConversionOptions): DeleteQuery; /** * Builds a MERGE query (upsert) that coordinates actions based on row matches. */ static buildMergeQuery(selectQuery: SimpleSelectQuery, options: MergeQueryConversionOptions): MergeQuery; private static normalizeInsertOptions; private static normalizeUpdateOptions; private static normalizeDeleteOptions; private static normalizeMergeOptions; private static normalizeColumnArray; private static collectSelectItems; private static collectSelectColumnNames; private static ensurePrimaryKeys; private static prepareInsertColumns; private static prepareUpdateColumns; private static prepareDeleteColumns; private static prepareMergeColumns; private static rebuildSelectClause; private static getSelectItemName; private static ensureSelectClauseSize; private static extractWithClause; private static buildEqualityPredicate; private static toColumnReference; private static mergeUniqueColumns; }