rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
220 lines (219 loc) • 10.6 kB
TypeScript
import { SqlComponent } from "./SqlComponent";
import { ForClause, FromClause, GroupByClause, HavingClause, LimitClause, OrderByClause, SelectClause, SourceExpression, WhereClause, WindowsClause as WindowClause, WithClause, CommonTable, OffsetClause, FetchClause } from "./Clause";
import { ValueComponent } from "./ValueComponent";
import { BinarySelectQuery } from "./BinarySelectQuery";
import type { SelectQuery } from "./SelectQuery";
import { TableColumnResolver } from "../transformers/TableColumnResolver";
/**
* Represents a simple SELECT query in SQL.
*/
export declare class SimpleSelectQuery extends SqlComponent implements SelectQuery {
static kind: symbol;
withClause: WithClause | null;
selectClause: SelectClause;
fromClause: FromClause | null;
whereClause: WhereClause | null;
groupByClause: GroupByClause | null;
havingClause: HavingClause | null;
orderByClause: OrderByClause | null;
windowClause: WindowClause | null;
limitClause: LimitClause | null;
offsetClause: OffsetClause | null;
fetchClause: FetchClause | null;
forClause: ForClause | null;
constructor(params: {
selectClause: SelectClause;
fromClause?: FromClause | null;
whereClause?: WhereClause | null;
groupByClause?: GroupByClause | null;
havingClause?: HavingClause | null;
orderByClause?: OrderByClause | null;
windowClause?: WindowClause | null;
limitClause?: LimitClause | null;
offsetClause?: OffsetClause | null;
fetchClause?: FetchClause | null;
forClause?: ForClause | null;
withClause?: WithClause | null;
});
/**
* Creates a new BinarySelectQuery with this query as the left side and the provided query as the right side,
* using UNION as the operator.
*
* @param rightQuery The right side of the UNION
* @returns A new BinarySelectQuery representing "this UNION rightQuery"
*/
toUnion(rightQuery: SelectQuery): BinarySelectQuery;
/**
* Creates a new BinarySelectQuery with this query as the left side and the provided query as the right side,
* using UNION ALL as the operator.
*
* @param rightQuery The right side of the UNION ALL
* @returns A new BinarySelectQuery representing "this UNION ALL rightQuery"
*/
toUnionAll(rightQuery: SelectQuery): BinarySelectQuery;
/**
* Creates a new BinarySelectQuery with this query as the left side and the provided query as the right side,
* using INTERSECT as the operator.
*
* @param rightQuery The right side of the INTERSECT
* @returns A new BinarySelectQuery representing "this INTERSECT rightQuery"
*/
toIntersect(rightQuery: SelectQuery): BinarySelectQuery;
/**
* Creates a new BinarySelectQuery with this query as the left side and the provided query as the right side,
* using INTERSECT ALL as the operator.
*
* @param rightQuery The right side of the INTERSECT ALL
* @returns A new BinarySelectQuery representing "this INTERSECT ALL rightQuery"
*/
toIntersectAll(rightQuery: SelectQuery): BinarySelectQuery;
/**
* Creates a new BinarySelectQuery with this query as the left side and the provided query as the right side,
* using EXCEPT as the operator.
*
* @param rightQuery The right side of the EXCEPT
* @returns A new BinarySelectQuery representing "this EXCEPT rightQuery"
*/
toExcept(rightQuery: SelectQuery): BinarySelectQuery;
/**
* Creates a new BinarySelectQuery with this query as the left side and the provided query as the right side,
* using EXCEPT ALL as the operator.
*
* @param rightQuery The right side of the EXCEPT ALL
* @returns A new BinarySelectQuery representing "this EXCEPT ALL rightQuery"
*/
toExceptAll(rightQuery: SelectQuery): BinarySelectQuery;
/**
* Creates a new BinarySelectQuery with this query as the left side and the provided query as the right side,
* using the specified operator.
*
* @param operator SQL operator to use (e.g. 'union', 'union all', 'intersect', 'except')
* @param rightQuery The right side of the binary operation
* @returns A new BinarySelectQuery representing "this [operator] rightQuery"
*/
toBinaryQuery(operator: string, rightQuery: SelectQuery): BinarySelectQuery;
/**
* Appends a new condition to the query's WHERE clause using AND logic.
* The condition is provided as a raw SQL string which is parsed internally.
*
* @param rawCondition Raw SQL string representing the condition (e.g. "status = 'active'")
*/
appendWhereRaw(rawCondition: string): void;
/**
* Appends a new condition to the query's WHERE clause using AND logic.
* The condition is provided as a ValueComponent object.
*
* @param condition ValueComponent representing the condition
*/
appendWhere(condition: ValueComponent): void;
/**
* Appends a new condition to the query's HAVING clause using AND logic.
* The condition is provided as a raw SQL string which is parsed internally.
*
* @param rawCondition Raw SQL string representing the condition (e.g. "count(*) > 5")
*/
appendHavingRaw(rawCondition: string): void;
/**
* Appends a new condition to the query's HAVING clause using AND logic.
* The condition is provided as a ValueComponent object.
*
* @param condition ValueComponent representing the condition
*/
appendHaving(condition: ValueComponent): void;
/**
* Appends an INNER JOIN clause to the query.
* @param joinSourceRawText The table source text to join (e.g., "my_table", "schema.my_table")
* @param alias The alias for the joined table
* @param columns The columns to use for the join condition (e.g. ["user_id"] or "user_id")
*/
innerJoinRaw(joinSourceRawText: string, alias: string, columns: string | string[], resolver?: TableColumnResolver | null): void;
/**
* Appends a LEFT JOIN clause to the query.
* @param joinSourceRawText The table source text to join
* @param alias The alias for the joined table
* @param columns The columns to use for the join condition
*/
leftJoinRaw(joinSourceRawText: string, alias: string, columns: string | string[], resolver?: TableColumnResolver | null): void;
/**
* Appends a RIGHT JOIN clause to the query.
* @param joinSourceRawText The table source text to join
* @param alias The alias for the joined table
* @param columns The columns to use for the join condition
*/
rightJoinRaw(joinSourceRawText: string, alias: string, columns: string | string[], resolver?: TableColumnResolver | null): void;
/**
* Appends an INNER JOIN clause to the query using a SourceExpression.
* @param sourceExpr The source expression to join
* @param columns The columns to use for the join condition
*/
innerJoin(sourceExpr: SourceExpression, columns: string | string[], resolver?: TableColumnResolver | null): void;
/**
* Appends a LEFT JOIN clause to the query using a SourceExpression.
* @param sourceExpr The source expression to join
* @param columns The columns to use for the join condition
*/
leftJoin(sourceExpr: SourceExpression, columns: string | string[], resolver?: TableColumnResolver | null): void;
/**
* Appends a RIGHT JOIN clause to the query using a SourceExpression.
* @param sourceExpr The source expression to join
* @param columns The columns to use for the join condition
*/
rightJoin(sourceExpr: SourceExpression, columns: string | string[], resolver?: TableColumnResolver | null): void;
/**
* Internal helper to append a JOIN clause.
* Parses the table source, finds the corresponding columns in the existing query context,
* and builds the JOIN condition.
* @param joinType Type of join (e.g., 'inner join', 'left join')
* @param joinSourceRawText Raw text for the table/source to join (e.g., "my_table", "schema.another_table")
* @param alias Alias for the table/source being joined
* @param columns Array or string of column names to join on
*/
private joinSourceRaw;
/**
* Internal helper to append a JOIN clause using a SourceExpression.
* @param joinType Type of join (e.g., 'inner join', 'left join')
* @param sourceExpr The source expression to join
* @param columns Array or string of column names to join on
*/
private joinSource;
toSource(alias: string): SourceExpression;
appendWith(commonTable: CommonTable | CommonTable[]): void;
/**
* Appends a CommonTable (CTE) to the WITH clause from raw SQL text and alias.
* If alias is provided, it will be used as the CTE name.
*
* @param rawText Raw SQL string representing the CTE body (e.g. '(SELECT ...)')
* @param alias Optional alias for the CTE (e.g. 'cte_name')
*/
appendWithRaw(rawText: string, alias: string): void;
/**
* Overrides a select item using a template literal function.
* The callback receives the SQL string of the original expression and must return a new SQL string.
* The result is parsed and set as the new select item value.
*
* Example usage:
* query.overrideSelectItemRaw("journal_date", expr => `greatest(${expr}, DATE '2025-01-01')`)
*
* @param columnName The name of the column to override
* @param fn Callback that receives the SQL string of the original expression and returns a new SQL string
*/
overrideSelectItemExpr(columnName: string, fn: (expr: string) => string): void;
/**
* Appends a WHERE clause using the expression for the specified column.
* If `options.upstream` is true, applies to all upstream queries containing the column.
* If false or omitted, applies only to the current query.
*
* @param columnName The name of the column to target.
* @param exprBuilder Function that receives the column expression as a string and returns the WHERE condition string.
* @param options Optional settings. If `upstream` is true, applies to upstream queries.
*/
appendWhereExpr(columnName: string, exprBuilder: (expr: string) => string, options?: {
upstream?: boolean;
}): void;
/**
* Sets the value of a parameter by name in this query.
* @param name Parameter name
* @param value Value to set
*/
setParameter(name: string, value: any): this;
}