Skip to content

Commit

Permalink
refactor: represent wheres internally as conditions (#7854)
Browse files Browse the repository at this point in the history
this updates the way `QueryBuilder` works to only build the expression
strings when we are actually building queries.  this means we can more
easily separate the concerns between compiling the query and building
queries.  it also allows us to support database specific `where` features
more readily
  • Loading branch information
imnotjames committed Jul 7, 2021
1 parent 88fb441 commit e922f35
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 172 deletions.
2 changes: 1 addition & 1 deletion src/find-options/FindOperator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class FindOperator<T> {
/**
* Gets the Type of this FindOperator
*/
get type(): string {
get type(): FindOperatorType {
return this._type;
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export {InsertQueryBuilder} from "./query-builder/InsertQueryBuilder";
export {UpdateQueryBuilder} from "./query-builder/UpdateQueryBuilder";
export {RelationQueryBuilder} from "./query-builder/RelationQueryBuilder";
export {Brackets} from "./query-builder/Brackets";
export {WhereExpression} from "./query-builder/WhereExpression";
export {WhereExpressionBuilder} from "./query-builder/WhereExpressionBuilder";
export {InsertResult} from "./query-builder/result/InsertResult";
export {UpdateResult} from "./query-builder/result/UpdateResult";
export {DeleteResult} from "./query-builder/result/DeleteResult";
Expand Down
8 changes: 4 additions & 4 deletions src/query-builder/Brackets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {WhereExpression} from "./WhereExpression";
import {WhereExpressionBuilder} from "./WhereExpressionBuilder";

/**
* Syntax sugar.
Expand All @@ -9,13 +9,13 @@ export class Brackets {
/**
* WHERE expression that will be taken into brackets.
*/
whereFactory: (qb: WhereExpression) => any;
whereFactory: (qb: WhereExpressionBuilder) => any;

/**
* Given WHERE query builder that will build a WHERE expression that will be taken into brackets.
*/
constructor(whereFactory: (qb: WhereExpression) => any) {
constructor(whereFactory: (qb: WhereExpressionBuilder) => any) {
this.whereFactory = whereFactory;
}

}
}
10 changes: 5 additions & 5 deletions src/query-builder/DeleteQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {Connection} from "../connection/Connection";
import {QueryRunner} from "../query-runner/QueryRunner";
import {SqlServerDriver} from "../driver/sqlserver/SqlServerDriver";
import {PostgresDriver} from "../driver/postgres/PostgresDriver";
import {WhereExpression} from "./WhereExpression";
import {WhereExpressionBuilder} from "./WhereExpressionBuilder";
import {Brackets} from "./Brackets";
import {DeleteResult} from "./result/DeleteResult";
import {ReturningStatementNotSupportedError} from "../error/ReturningStatementNotSupportedError";
Expand All @@ -20,7 +20,7 @@ import {BetterSqlite3Driver} from "../driver/better-sqlite3/BetterSqlite3Driver"
/**
* Allows to build complex sql queries in a fashion way and execute those queries.
*/
export class DeleteQueryBuilder<Entity> extends QueryBuilder<Entity> implements WhereExpression {
export class DeleteQueryBuilder<Entity> extends QueryBuilder<Entity> implements WhereExpressionBuilder {

// -------------------------------------------------------------------------
// Constructor
Expand Down Expand Up @@ -149,7 +149,7 @@ export class DeleteQueryBuilder<Entity> extends QueryBuilder<Entity> implements
*/
where(where: Brackets|string|((qb: this) => string)|ObjectLiteral|ObjectLiteral[], parameters?: ObjectLiteral): this {
this.expressionMap.wheres = []; // don't move this block below since computeWhereParameter can add where expressions
const condition = this.computeWhereParameter(where);
const condition = this.getWhereCondition(where);
if (condition)
this.expressionMap.wheres = [{ type: "simple", condition: condition }];
if (parameters)
Expand All @@ -162,7 +162,7 @@ export class DeleteQueryBuilder<Entity> extends QueryBuilder<Entity> implements
* Additionally you can add parameters used in where expression.
*/
andWhere(where: Brackets|string|((qb: this) => string)|ObjectLiteral|ObjectLiteral[], parameters?: ObjectLiteral): this {
this.expressionMap.wheres.push({ type: "and", condition: this.computeWhereParameter(where) });
this.expressionMap.wheres.push({ type: "and", condition: this.getWhereCondition(where) });
if (parameters) this.setParameters(parameters);
return this;
}
Expand All @@ -172,7 +172,7 @@ export class DeleteQueryBuilder<Entity> extends QueryBuilder<Entity> implements
* Additionally you can add parameters used in where expression.
*/
orWhere(where: Brackets|string|((qb: this) => string)|ObjectLiteral|ObjectLiteral[], parameters?: ObjectLiteral): this {
this.expressionMap.wheres.push({ type: "or", condition: this.computeWhereParameter(where) });
this.expressionMap.wheres.push({ type: "or", condition: this.getWhereCondition(where) });
if (parameters) this.setParameters(parameters);
return this;
}
Expand Down

0 comments on commit e922f35

Please sign in to comment.