declare enum ParameterType { NULL = "NULL", INTEGER = "INTEGER", STRING = "STRING", LARGE_OBJECT = "LARGE_OBJECT", BOOLEAN = "BOOLEAN", BINARY = "BINARY", ASCII = "ASCII" } declare enum ArrayParameterType { INTEGER = "ARRAY_INTEGER", STRING = "ARRAY_STRING", ASCII = "ARRAY_ASCII", BINARY = "ARRAY_BINARY" } declare namespace ArrayParameterType { /** * Maps ArrayParameterType to corresponding ParameterType. */ function toElementParameterType(type: ArrayParameterType): ParameterType; } declare enum ConflictResolutionMode { ORDINARY = 0, SKIP_LOCKED = 1 } declare class ForUpdate { readonly conflictResolutionMode: ConflictResolutionMode; constructor(conflictResolutionMode: ConflictResolutionMode); getConflictResolutionMode(): ConflictResolutionMode; } declare class Limit { readonly maxResults: number | null; readonly firstResult: number; constructor(maxResults: number | null, firstResult?: number); isDefined(): boolean; getMaxResults(): number | null; getFirstResult(): number; } declare class SelectQuery { readonly distinct: boolean; readonly columns: string[]; readonly from: string[]; readonly where: string | null; readonly groupBy: string[]; readonly having: string | null; readonly orderBy: string[]; readonly limit: Limit; readonly forUpdate: ForUpdate | null; constructor(distinct: boolean, columns: string[], from: string[], where: string | null, groupBy: string[], having: string | null, orderBy: string[], limit: Limit, forUpdate: ForUpdate | null); isDistinct(): boolean; getColumns(): string[]; getFrom(): string[]; getWhere(): string | null; getGroupBy(): string[]; getHaving(): string | null; getOrderBy(): string[]; getLimit(): Limit; getForUpdate(): ForUpdate | null; } interface SelectSQLBuilder { buildSQL(query: SelectQuery): string; } declare class CommonTableExpression { readonly name: string; readonly query: string | QueryBuilder; readonly columns: string[] | null; constructor(name: string, query: string | QueryBuilder, columns: string[] | null); } interface QueryBuilderPlatform { createSelectSQLBuilder(): { buildSQL(query: SelectQuery): string; }; createUnionSQLBuilder(): { buildSQL(query: UnionQuery): string; }; createWithSQLBuilder(): { buildSQL(firstExpression: CommonTableExpression, ...otherExpressions: CommonTableExpression[]): string; }; quoteStringLiteral?(value: string): string; } type QueryParameterType = string | ParameterType | ArrayParameterType; type QueryParameters = unknown[] | Record; type QueryParameterTypes = QueryParameterType[] | Record; declare class CompositeExpression { private readonly type; static readonly TYPE_AND = "AND"; static readonly TYPE_OR = "OR"; private readonly parts; constructor(type: "AND" | "OR", part: CompositeExpression | string, ...rest: (CompositeExpression | string)[]); static and(part: CompositeExpression | string, ...parts: (CompositeExpression | string)[]): CompositeExpression; static or(part: CompositeExpression | string, ...parts: (CompositeExpression | string)[]): CompositeExpression; with(part: CompositeExpression | string, ...parts: (CompositeExpression | string)[]): CompositeExpression; getType(): "AND" | "OR"; count(): number; toString(): string; } interface ExpressionBuilderQuoter { quote(value: string): Promise; } declare class ExpressionBuilder { private readonly quoter; static readonly EQ = "="; static readonly NEQ = "<>"; static readonly LT = "<"; static readonly LTE = "<="; static readonly GT = ">"; static readonly GTE = ">="; constructor(quoter: ExpressionBuilderQuoter); /** * Creates a conjunction of the given expressions. */ and(expr: string | CompositeExpression, ...rest: (string | CompositeExpression)[]): CompositeExpression; /** * Creates a disjunction of the given expressions. */ or(expr: string | CompositeExpression, ...rest: (string | CompositeExpression)[]): CompositeExpression; /** * Creates a comparison expression with the given arguments. */ comparison(x: string, operator: string, y: string): string; /** * Creates an equal comparison expression with the given arguments. * First argument is considered the left expression and the second is the right expression. * When converted to string, it will generate a = . */ eq(x: string, y: string): string; /** * Creates a not-equal comparison expression with the given arguments. * First argument is considered the left expression and the second is the right expression. * When converted to string, it will generate a <> . */ neq(x: string, y: string): string; /** * Creates a lower-than comparison expression with the given arguments. * First argument is considered the left expression and the second is the right expression. * When converted to string, it will generate a < . */ lt(x: string, y: string): string; /** * Creates a lower-than-equal comparison expression with the given arguments. * First argument is considered the left expression and the second is the right expression. * When converted to string, it will generate a <= . */ lte(x: string, y: string): string; /** * Creates a greater-than comparison expression with the given arguments. * First argument is considered the left expression and the second is the right expression. * When converted to string, it will generate a > . */ gt(x: string, y: string): string; /** * Creates a greater-than-equal comparison expression with the given arguments. * First argument is considered the left expression and the second is the right expression. * When converted to string, it will generate a >= . */ gte(x: string, y: string): string; /** * Creates an IS NULL expression with the given arguments. * * @param x The expression to be restricted by IS NULL. */ isNull(x: string): string; /** * Creates an IS NOT NULL expression with the given arguments. * * @param x The expression to be restricted by IS NOT NULL. */ isNotNull(x: string): string; /** * Creates a LIKE comparison expression * * @param expr The expression to be inspected by the LIKE comparison * @param pattern The pattern to compare against * @param escapeChar Optional escape character for special characters in the pattern. */ like(expr: string, pattern: string, escapeChar?: string): string; /** * Creates a NOT LIKE comparison expression * * @param expr The expression to be inspected by the NOT LIKE comparison * @param pattern The pattern to compare against * @param escapeChar Optional escape character for special characters in the pattern. */ notLike(expr: string, pattern: string, escapeChar?: string): string; /** * Creates an IN () comparison expression with the given arguments. * * @param x The SQL expression to be matched against the set. * @param y The SQL expression or an array of SQL expressions representing the set. */ in(x: string, y: string | string[]): string; /** * Creates a NOT IN () comparison expression with the given arguments. * * @param x The SQL expression to be matched against the set. * @param y The SQL expression or an array of SQL expressions representing the set. */ notIn(x: string, y: string | string[]): string; /** * Creates an SQL literal expression from the string. * * The usage of this method is discouraged. Use prepared statements */ literal(input: string): Promise; } declare enum UnionType { ALL = 0, DISTINCT = 1 } type AssociativeRow = Record; type ParamType = string | ParameterType | ArrayParameterType; declare enum PlaceHolder { NAMED = "named", POSITIONAL = "positional" } declare class QueryBuilder { private readonly platform; private sql; private compiledPlatform; private params; private types; private type; private boundCounter; private firstResult; private maxResults; private table; private unionParts; private commonTableExpressions; private _select; private _distinct; private _from; private _join; private _set; private _where; private _groupBy; private _having; private _orderBy; private _forUpdate; private _values; constructor(platform?: QueryBuilderPlatform); expr(): ExpressionBuilder; sub(): QueryBuilder; getSQL(platform?: QueryBuilderPlatform): string; setParameter(key: string | number, value: any, type?: ParamType): this; setParameters(params: QueryParameters, types?: QueryParameterTypes): this; getParameters(): QueryParameters; getParameter(key: string | number): any; getParameterTypes(): QueryParameterTypes; getParameterType(key: string | number): ParamType; setFirstResult(firstResult: number): this; getFirstResult(): number; setMaxResults(maxResults: number | null): this; getMaxResults(): number | null; forUpdate(mode?: ConflictResolutionMode): this; union(part: string | QueryBuilder): this; addUnion(part: string | QueryBuilder, type?: UnionType): this; with(name: string, part: string | QueryBuilder, columns?: string[] | null): this; select(...expressions: string[]): this; distinct(distinct?: boolean): this; addSelect(expression: string, ...expressions: string[]): this; delete(table: string): this; update(table: string): this; insert(table: string): this; insertWith(table: string, data: AssociativeRow, placeHolder?: PlaceHolder): this; updateWith(table: string, data: AssociativeRow, placeHolder?: PlaceHolder): this; from(table: string, alias?: string | null): this; join(fromAlias: string, join: string, alias: string, condition?: string | null): this; innerJoin(fromAlias: string, join: string, alias: string, condition?: string | null): this; leftJoin(fromAlias: string, join: string, alias: string, condition?: string | null): this; rightJoin(fromAlias: string, join: string, alias: string, condition?: string | null): this; set(key: string, value: string): this; where(predicate: string | CompositeExpression, ...predicates: (string | CompositeExpression)[]): this; andWhere(predicate: string | CompositeExpression, ...predicates: (string | CompositeExpression)[]): this; orWhere(predicate: string | CompositeExpression, ...predicates: (string | CompositeExpression)[]): this; groupBy(expression: string, ...expressions: string[]): this; addGroupBy(expression: string, ...expressions: string[]): this; setValue(column: string, value: string): this; values(values: Record): this; having(predicate: string | CompositeExpression, ...predicates: (string | CompositeExpression)[]): this; andHaving(predicate: string | CompositeExpression, ...predicates: (string | CompositeExpression)[]): this; orHaving(predicate: string | CompositeExpression, ...predicates: (string | CompositeExpression)[]): this; orderBy(sort: string, order?: string): this; addOrderBy(sort: string, order?: string): this; resetWhere(): this; resetGroupBy(): this; resetHaving(): this; resetOrderBy(): this; toString(): string; createNamedParameter(value: unknown, type?: ParamType, placeHolder?: string | null): string; createPositionalParameter(value: unknown, type?: ParamType): string; private createPredicate; private appendToPredicate; private getSQLForSelect; private getFromClauses; private verifyAllAliasesAreKnown; private getSQLForInsert; private getSQLForDelete; private getSQLForUpdate; private getSQLForUnion; private getSQLForJoins; private ensurePositionalParams; private ensurePositionalTypes; private ensureNamedParams; private ensureNamedTypes; } declare class Union { readonly query: QueryBuilder | string; readonly type: UnionType | null; constructor(query: QueryBuilder | string, type?: UnionType | null); } declare class UnionQuery { readonly unionParts: Union[]; readonly orderBy: string[]; readonly limit: Limit; constructor(unionParts: Union[], orderBy: string[], limit: Limit); getUnionParts(): Union[]; getOrderBy(): string[]; getLimit(): Limit; } interface UnionSQLBuilder { buildSQL(query: UnionQuery): string; } declare class WithSQLBuilder { buildSQL(firstExpression: CommonTableExpression, ...otherExpressions: CommonTableExpression[]): string; } declare abstract class AbstractPlatform { /** * Quotes a single identifier (no dot chain separation). */ quoteSingleIdentifier(str: string): string; /** * Adds a driver-specific LIMIT clause to the query. */ modifyLimitQuery(query: string, limit: number | null, offset?: number): string; /** * Adds a platform-specific LIMIT clause to the query. */ protected doModifyLimitQuery(query: string, limit: number | null, offset: number): string; /** * Quotes a literal string. * This method is NOT meant to fix SQL injections! * It is only meant to escape this platform's string literal * quote character inside the given literal string. */ quoteStringLiteral(str: string): string; getUnionSelectPartSQL(subQuery: string): string; /** * Returns the `UNION ALL` keyword. */ getUnionAllSQL(): string; /** * Returns the compatible `UNION DISTINCT` keyword. */ getUnionDistinctSQL(): string; createSelectSQLBuilder(): SelectSQLBuilder; createUnionSQLBuilder(): UnionSQLBuilder; createWithSQLBuilder(): WithSQLBuilder; } declare abstract class AbstractMySQLPlatform extends AbstractPlatform { protected doModifyLimitQuery(query: string, limit: number | null, offset: number): string; quoteSingleIdentifier(str: string): string; } declare class DB2Platform extends AbstractPlatform { createSelectSQLBuilder(): SelectSQLBuilder; protected doModifyLimitQuery(query: string, limit: number | null, offset: number): string; } declare class MySQLPlatform extends AbstractMySQLPlatform { createSelectSQLBuilder(): SelectSQLBuilder; } declare class MySQL80Platform extends MySQLPlatform { createSelectSQLBuilder(): SelectSQLBuilder; } declare class OraclePlatform extends AbstractPlatform { protected doModifyLimitQuery(query: string, limit: number | null, offset: number): string; } declare class SQLServerPlatform extends AbstractPlatform { createSelectSQLBuilder(): SelectSQLBuilder; protected doModifyLimitQuery(query: string, limit: number | null, offset: number): string; private shouldAddOrderBy; /** * Helper function to count occurrences of a character after a given index */ private countChar; quoteSingleIdentifier(str: string): string; } interface Exception extends Error { } interface PlatformException extends Exception { } declare class NotSupported extends Error implements PlatformException { constructor(method: string); static new(method: string): NotSupported; } declare class QueryException extends Error implements Exception { constructor(message: string); } declare class NonUniqueAlias extends QueryException { static new(alias: string, registeredAliases: string[]): NonUniqueAlias; } declare class UnknownAlias extends QueryException { static new(alias: string, registeredAliases: string[]): UnknownAlias; } declare class From { readonly table: string; readonly alias: string | null; constructor(table: string, alias?: string | null); } declare class Join { readonly type: "INNER" | "LEFT" | "RIGHT"; readonly table: string; readonly alias: string; readonly condition: string | null; private constructor(); static inner(table: string, alias: string, condition: string | null): Join; static left(table: string, alias: string, condition: string | null): Join; static right(table: string, alias: string, condition: string | null): Join; } declare enum QueryType { SELECT = 0, INSERT = 1, UPDATE = 2, DELETE = 3, UNION = 4 } interface LimitSQLDialect { modifyLimitQuery(query: string, limit: number | null, offset?: number): string; } interface UnionSQLDialect extends LimitSQLDialect { getUnionSelectPartSQL(subQuery: string): string; getUnionAllSQL(): string; getUnionDistinctSQL(): string; } declare class DefaultSelectSQLBuilder implements SelectSQLBuilder { private readonly platform; private readonly forUpdateSQL; private readonly skipLockedSQL; constructor(platform: LimitSQLDialect, forUpdateSQL: string | null, skipLockedSQL: string | null); buildSQL(query: SelectQuery): string; } declare class DefaultUnionSQLBuilder implements UnionSQLBuilder { private readonly platform; constructor(platform: UnionSQLDialect); buildSQL(query: UnionQuery): string; } declare class SQLServerSelectSQLBuilder implements SelectSQLBuilder { private readonly platform; constructor(platform: LimitSQLDialect); buildSQL(query: SelectQuery): string; } export { AbstractMySQLPlatform, AbstractPlatform, ArrayParameterType, CommonTableExpression, CompositeExpression, ConflictResolutionMode, DB2Platform, DefaultSelectSQLBuilder, DefaultUnionSQLBuilder, ExpressionBuilder, type ExpressionBuilderQuoter, ForUpdate, From, Join, Limit, type LimitSQLDialect, MySQL80Platform, MySQLPlatform, NonUniqueAlias, NotSupported, OraclePlatform, ParameterType, PlaceHolder, type PlatformException, QueryBuilder, type QueryBuilderPlatform, QueryException, type QueryParameterType, type QueryParameterTypes, type QueryParameters, QueryType, SQLServerPlatform, SQLServerSelectSQLBuilder, SelectQuery, type SelectSQLBuilder, Union, UnionQuery, type UnionSQLBuilder, type UnionSQLDialect, UnionType, UnknownAlias, WithSQLBuilder };