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

Commit 39a8e34

Browse files
Benjamin-Dobellpleerock
authored andcommitted
feat: Added support for DISTINCT queries (typeorm#4109)
1 parent c8a9ea0 commit 39a8e34

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/query-builder/QueryExpressionMap.ts

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ export class QueryExpressionMap {
4646
*/
4747
selects: SelectQuery[] = [];
4848

49+
/**
50+
* Whether SELECT is DISTINCT.
51+
*/
52+
selectDistinct: boolean = false;
53+
4954
/**
5055
* FROM-s to be selected.
5156
*/

src/query-builder/SelectQueryBuilder.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> implements
158158
return this;
159159
}
160160

161+
/**
162+
* Sets whether the selection is DISTINCT.
163+
*/
164+
distinct(distinct: boolean = true): this {
165+
this.expressionMap.selectDistinct = distinct;
166+
return this;
167+
}
168+
161169
/**
162170
* Specifies FROM which entity's table select/update/delete will be executed.
163171
* Also sets a main string alias of the selection data.
@@ -1400,8 +1408,9 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> implements
14001408

14011409
return this.getTableName(alias.tablePath!) + " " + this.escape(alias.name);
14021410
});
1411+
const select = "SELECT " + (this.expressionMap.selectDistinct ? "DISTINCT " : "");
14031412
const selection = allSelects.map(select => select.selection + (select.aliasName ? " AS " + this.escape(select.aliasName) : "")).join(", ");
1404-
return "SELECT " + selection + " FROM " + froms.join(", ") + lock;
1413+
return select + selection + " FROM " + froms.join(", ") + lock;
14051414
}
14061415

14071416
/**

test/functional/query-builder/select/query-builder-select.ts

+15
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ describe("query builder > select", () => {
2727
"FROM post post");
2828
})));
2929

30+
it("should append all entity mapped columns from main selection to SELECT DISTINCT statement", () => Promise.all(connections.map(async connection => {
31+
const sql = connection.manager.createQueryBuilder(Post, "post")
32+
.distinct()
33+
.disableEscaping()
34+
.getSql();
35+
36+
expect(sql).to.equal("SELECT DISTINCT post.id AS post_id, " +
37+
"post.title AS post_title, " +
38+
"post.description AS post_description, " +
39+
"post.rating AS post_rating, " +
40+
"post.version AS post_version, " +
41+
"post.categoryId AS post_categoryId " +
42+
"FROM post post");
43+
})));
44+
3045
it("should append all entity mapped columns from both main selection and join selections to select statement", () => Promise.all(connections.map(async connection => {
3146
const sql = connection.createQueryBuilder(Post, "post")
3247
.leftJoinAndSelect("category", "category")

0 commit comments

Comments
 (0)