Skip to content

Commit

Permalink
fix: count() method for multiple primary keys for cockroachdb (#6745)
Browse files Browse the repository at this point in the history
* fix: count() method for multiple primary keys for cockroachdb

Cockroachdb does not support concat() for different types at the moment.
To fix this problem, each primary key is cast to the text type.

* fix: add doublequote

* fix: add doublequote

* test: update and move tests for count() method for multiple primary keys

* fix: count() method for multiple primary keys for oracle

Oracle does not support CONCAT() for more than 2 arguments at the moment.
To solve this problem, operator || is used instead of CONCAT().
  • Loading branch information
temnov98 committed Sep 21, 2020
1 parent c714867 commit dfe8259
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/query-builder/SelectQueryBuilder.ts
Expand Up @@ -36,6 +36,7 @@ import {SelectQueryBuilderOption} from "./SelectQueryBuilderOption";
import {ObjectUtils} from "../util/ObjectUtils";
import {DriverUtils} from "../driver/DriverUtils";
import {AuroraDataApiDriver} from "../driver/aurora-data-api/AuroraDataApiDriver";
import {CockroachDriver} from "../driver/cockroachdb/CockroachDriver";

/**
* Allows to build complex sql queries in a fashion way and execute those queries.
Expand Down Expand Up @@ -1787,6 +1788,16 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> implements
return `${distinctAlias}.${propertyName}`;
}).join(" || ") + ")) as \"cnt\"";

} else if (this.connection.driver instanceof CockroachDriver) {
countSql = `COUNT(DISTINCT(CONCAT(` + metadata.primaryColumns.map((primaryColumn, index) => {
const propertyName = this.escape(primaryColumn.databaseName);
return `${distinctAlias}.${propertyName}::text`;
}).join(", ") + "))) as \"cnt\"";
} else if (this.connection.driver instanceof OracleDriver) {
countSql = `COUNT(DISTINCT(` + metadata.primaryColumns.map((primaryColumn, index) => {
const propertyName = this.escape(primaryColumn.databaseName);
return `${distinctAlias}.${propertyName}`;
}).join(" || ") + ")) as \"cnt\"";
} else {
countSql = `COUNT(DISTINCT(CONCAT(` + metadata.primaryColumns.map((primaryColumn, index) => {
const propertyName = this.escape(primaryColumn.databaseName);
Expand Down
13 changes: 13 additions & 0 deletions test/functional/query-builder/count/entity/Test.ts
@@ -0,0 +1,13 @@
import {Entity, PrimaryColumn} from "../../../../../src";

@Entity("tests")
export class Test {
@PrimaryColumn()
varcharField: string;

@PrimaryColumn("uuid")
uuidField: string;

@PrimaryColumn()
intField: number;
}
22 changes: 22 additions & 0 deletions test/functional/query-builder/count/query-builder-count.ts
@@ -0,0 +1,22 @@
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../utils/test-utils";
import {Connection} from "../../../../src/connection/Connection";
import {expect} from "chai";
import {Test} from "./entity/Test";

describe("query builder > count", () => {

let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [Test],
schemaCreate: true,
dropSchema: true,
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("Count query should be completed successfully", () => Promise.all(connections.map(async connection => {
const count = await connection.getRepository(Test).count();
expect(count).to.be.equal(0);
})));

});

0 comments on commit dfe8259

Please sign in to comment.