Skip to content
This repository has been archived by the owner on Mar 18, 2022. It is now read-only.

Commit

Permalink
feat: Added support for DISTINCT queries (typeorm#4109)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Dobell authored and pleerock committed Jun 30, 2019
1 parent c8a9ea0 commit 39a8e34
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/query-builder/QueryExpressionMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export class QueryExpressionMap {
*/
selects: SelectQuery[] = [];

/**
* Whether SELECT is DISTINCT.
*/
selectDistinct: boolean = false;

/**
* FROM-s to be selected.
*/
Expand Down
11 changes: 10 additions & 1 deletion src/query-builder/SelectQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> implements
return this;
}

/**
* Sets whether the selection is DISTINCT.
*/
distinct(distinct: boolean = true): this {
this.expressionMap.selectDistinct = distinct;
return this;
}

/**
* Specifies FROM which entity's table select/update/delete will be executed.
* Also sets a main string alias of the selection data.
Expand Down Expand Up @@ -1400,8 +1408,9 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> implements

return this.getTableName(alias.tablePath!) + " " + this.escape(alias.name);
});
const select = "SELECT " + (this.expressionMap.selectDistinct ? "DISTINCT " : "");
const selection = allSelects.map(select => select.selection + (select.aliasName ? " AS " + this.escape(select.aliasName) : "")).join(", ");
return "SELECT " + selection + " FROM " + froms.join(", ") + lock;
return select + selection + " FROM " + froms.join(", ") + lock;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions test/functional/query-builder/select/query-builder-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ describe("query builder > select", () => {
"FROM post post");
})));

it("should append all entity mapped columns from main selection to SELECT DISTINCT statement", () => Promise.all(connections.map(async connection => {
const sql = connection.manager.createQueryBuilder(Post, "post")
.distinct()
.disableEscaping()
.getSql();

expect(sql).to.equal("SELECT DISTINCT post.id AS post_id, " +
"post.title AS post_title, " +
"post.description AS post_description, " +
"post.rating AS post_rating, " +
"post.version AS post_version, " +
"post.categoryId AS post_categoryId " +
"FROM post post");
})));

it("should append all entity mapped columns from both main selection and join selections to select statement", () => Promise.all(connections.map(async connection => {
const sql = connection.createQueryBuilder(Post, "post")
.leftJoinAndSelect("category", "category")
Expand Down

0 comments on commit 39a8e34

Please sign in to comment.