From 4e31ba04ba6f79157f24a333723aa37d811fc7b1 Mon Sep 17 00:00:00 2001 From: temnov98 Date: Sat, 19 Sep 2020 15:51:52 +0300 Subject: [PATCH 1/5] 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. --- src/query-builder/SelectQueryBuilder.ts | 6 +++++ ...b-count-query-for-multiple-primary-keys.ts | 23 +++++++++++++++++++ .../entity/Test.ts | 13 +++++++++++ 3 files changed, 42 insertions(+) create mode 100644 test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/cockroachdb-count-query-for-multiple-primary-keys.ts create mode 100644 test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/entity/Test.ts diff --git a/src/query-builder/SelectQueryBuilder.ts b/src/query-builder/SelectQueryBuilder.ts index 821be3186c..7273ed19a9 100644 --- a/src/query-builder/SelectQueryBuilder.ts +++ b/src/query-builder/SelectQueryBuilder.ts @@ -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. @@ -1787,6 +1788,11 @@ export class SelectQueryBuilder extends QueryBuilder 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 { countSql = `COUNT(DISTINCT(CONCAT(` + metadata.primaryColumns.map((primaryColumn, index) => { const propertyName = this.escape(primaryColumn.databaseName); diff --git a/test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/cockroachdb-count-query-for-multiple-primary-keys.ts b/test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/cockroachdb-count-query-for-multiple-primary-keys.ts new file mode 100644 index 0000000000..d9849609e8 --- /dev/null +++ b/test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/cockroachdb-count-query-for-multiple-primary-keys.ts @@ -0,0 +1,23 @@ +import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils"; +import {Connection} from "../../../src/connection/Connection"; +import {expect} from 'chai'; +import {Test} from './entity/Test'; + +describe("other issues > cockroachdb count query for multiple primary keys", () => { + + let connections: Connection[]; + before(async () => connections = await createTestingConnections({ + entities: [Test], + schemaCreate: true, + dropSchema: true, + enabledDrivers: ["cockroachdb"] + })); + 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); + }))); + +}); diff --git a/test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/entity/Test.ts b/test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/entity/Test.ts new file mode 100644 index 0000000000..ce4699b09e --- /dev/null +++ b/test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/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; +} From 758d858fb1658c303c601e12a6d7af2b0cb42b71 Mon Sep 17 00:00:00 2001 From: temnov98 Date: Sat, 19 Sep 2020 16:45:51 +0300 Subject: [PATCH 2/5] fix: add doublequote --- .../cockroachdb-count-query-for-multiple-primary-keys.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/cockroachdb-count-query-for-multiple-primary-keys.ts b/test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/cockroachdb-count-query-for-multiple-primary-keys.ts index d9849609e8..7966b6e73f 100644 --- a/test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/cockroachdb-count-query-for-multiple-primary-keys.ts +++ b/test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/cockroachdb-count-query-for-multiple-primary-keys.ts @@ -1,7 +1,7 @@ import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils"; import {Connection} from "../../../src/connection/Connection"; -import {expect} from 'chai'; -import {Test} from './entity/Test'; +import {expect} from "chai"; +import {Test} from "./entity/Test"; describe("other issues > cockroachdb count query for multiple primary keys", () => { From f36e9420665eff654ede086f240bcbfe4be3b11f Mon Sep 17 00:00:00 2001 From: temnov98 Date: Mon, 21 Sep 2020 01:33:40 +0300 Subject: [PATCH 3/5] fix: add doublequote --- src/query-builder/SelectQueryBuilder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/query-builder/SelectQueryBuilder.ts b/src/query-builder/SelectQueryBuilder.ts index 7273ed19a9..940522c888 100644 --- a/src/query-builder/SelectQueryBuilder.ts +++ b/src/query-builder/SelectQueryBuilder.ts @@ -36,7 +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'; +import {CockroachDriver} from "../driver/cockroachdb/CockroachDriver"; /** * Allows to build complex sql queries in a fashion way and execute those queries. From e3df5d9dd398473020c37574dab9d8bd54927f1c Mon Sep 17 00:00:00 2001 From: temnov98 Date: Mon, 21 Sep 2020 01:53:00 +0300 Subject: [PATCH 4/5] test: update and move tests for count() method for multiple primary keys --- .../query-builder/count}/entity/Test.ts | 2 +- .../query-builder/count/query-builder-count.ts} | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) rename test/{other-issues/cockroachdb-count-query-for-multiple-primary-keys => functional/query-builder/count}/entity/Test.ts (75%) rename test/{other-issues/cockroachdb-count-query-for-multiple-primary-keys/cockroachdb-count-query-for-multiple-primary-keys.ts => functional/query-builder/count/query-builder-count.ts} (73%) diff --git a/test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/entity/Test.ts b/test/functional/query-builder/count/entity/Test.ts similarity index 75% rename from test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/entity/Test.ts rename to test/functional/query-builder/count/entity/Test.ts index ce4699b09e..360fce0ac0 100644 --- a/test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/entity/Test.ts +++ b/test/functional/query-builder/count/entity/Test.ts @@ -1,4 +1,4 @@ -import {Entity, PrimaryColumn} from "../../../../src"; +import {Entity, PrimaryColumn} from "../../../../../src"; @Entity("tests") export class Test { diff --git a/test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/cockroachdb-count-query-for-multiple-primary-keys.ts b/test/functional/query-builder/count/query-builder-count.ts similarity index 73% rename from test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/cockroachdb-count-query-for-multiple-primary-keys.ts rename to test/functional/query-builder/count/query-builder-count.ts index 7966b6e73f..39990f9d38 100644 --- a/test/other-issues/cockroachdb-count-query-for-multiple-primary-keys/cockroachdb-count-query-for-multiple-primary-keys.ts +++ b/test/functional/query-builder/count/query-builder-count.ts @@ -1,16 +1,15 @@ -import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils"; -import {Connection} from "../../../src/connection/Connection"; +import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../utils/test-utils"; +import {Connection} from "../../../../src/connection/Connection"; import {expect} from "chai"; import {Test} from "./entity/Test"; -describe("other issues > cockroachdb count query for multiple primary keys", () => { +describe("query builder > count", () => { let connections: Connection[]; before(async () => connections = await createTestingConnections({ entities: [Test], schemaCreate: true, dropSchema: true, - enabledDrivers: ["cockroachdb"] })); beforeEach(() => reloadTestingDatabases(connections)); after(() => closeTestingConnections(connections)); From 246670b86856f595860fa4fbf74ca3f396401fa0 Mon Sep 17 00:00:00 2001 From: temnov98 Date: Mon, 21 Sep 2020 03:03:17 +0300 Subject: [PATCH 5/5] 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(). --- src/query-builder/SelectQueryBuilder.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/query-builder/SelectQueryBuilder.ts b/src/query-builder/SelectQueryBuilder.ts index 940522c888..11cb61b0a4 100644 --- a/src/query-builder/SelectQueryBuilder.ts +++ b/src/query-builder/SelectQueryBuilder.ts @@ -1793,6 +1793,11 @@ export class SelectQueryBuilder extends QueryBuilder implements 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);