Skip to content

Commit 0093d98

Browse files
authored
Merge pull request #217 from moufmouf/fix_multiple_pk_in_oracle
Fixing count query in Oracle when there are multiple PK
2 parents 4da6502 + 5d61660 commit 0093d98

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
],
2020
"require" : {
2121
"php" : ">=7.2",
22-
"mouf/magic-query" : "^1.4",
22+
"mouf/magic-query" : "^1.4.3",
2323
"mouf/schema-analyzer": "^1.1.4",
2424
"doctrine/dbal": "^2.9.2",
2525
"psr/log": "~1.0",

src/QueryFactory/FindObjectsQueryFactory.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Doctrine\Common\Cache\Cache;
77
use Doctrine\DBAL\Platforms\MySqlPlatform;
8+
use Doctrine\DBAL\Platforms\OraclePlatform;
89
use Doctrine\DBAL\Schema\Schema;
910
use TheCodingMachine\TDBM\OrderByAnalyzer;
1011
use TheCodingMachine\TDBM\TDBMService;
@@ -55,18 +56,22 @@ protected function compute(): void
5556

5657
$subQuery = 'SELECT DISTINCT '.implode(', ', $pkColumnNames).' FROM MAGICJOIN('.$this->mainTable.')';
5758

59+
if (!empty($this->filterString)) {
60+
$sql .= ' WHERE '.$this->filterString;
61+
$subQuery .= ' WHERE '.$this->filterString;
62+
}
63+
5864
if (count($pkColumnNames) === 1 || $this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform) {
5965
$countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM MAGICJOIN('.$this->mainTable.')';
66+
if (!empty($this->filterString)) {
67+
$countSql .= ' WHERE '.$this->filterString;
68+
}
69+
} elseif ($this->tdbmService->getConnection()->getDatabasePlatform() instanceof OraclePlatform) {
70+
$countSql = 'SELECT COUNT(*) FROM ('.$subQuery.')';
6071
} else {
6172
$countSql = 'SELECT COUNT(*) FROM ('.$subQuery.') tmp';
6273
}
6374

64-
if (!empty($this->filterString)) {
65-
$sql .= ' WHERE '.$this->filterString;
66-
$countSql .= ' WHERE '.$this->filterString;
67-
$subQuery .= ' WHERE '.$this->filterString;
68-
}
69-
7075
if (!empty($orderString)) {
7176
$sql .= ' ORDER BY '.$orderString;
7277
}

0 commit comments

Comments
 (0)