Skip to content

Commit

Permalink
Merge pull request #217 from moufmouf/fix_multiple_pk_in_oracle
Browse files Browse the repository at this point in the history
Fixing count query in Oracle when there are multiple PK
  • Loading branch information
moufmouf committed Jul 22, 2020
2 parents 4da6502 + 5d61660 commit 0093d98
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"require" : {
"php" : ">=7.2",
"mouf/magic-query" : "^1.4",
"mouf/magic-query" : "^1.4.3",
"mouf/schema-analyzer": "^1.1.4",
"doctrine/dbal": "^2.9.2",
"psr/log": "~1.0",
Expand Down
17 changes: 11 additions & 6 deletions src/QueryFactory/FindObjectsQueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Doctrine\Common\Cache\Cache;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Schema\Schema;
use TheCodingMachine\TDBM\OrderByAnalyzer;
use TheCodingMachine\TDBM\TDBMService;
Expand Down Expand Up @@ -55,18 +56,22 @@ protected function compute(): void

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

if (!empty($this->filterString)) {
$sql .= ' WHERE '.$this->filterString;
$subQuery .= ' WHERE '.$this->filterString;
}

if (count($pkColumnNames) === 1 || $this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform) {
$countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM MAGICJOIN('.$this->mainTable.')';
if (!empty($this->filterString)) {
$countSql .= ' WHERE '.$this->filterString;
}
} elseif ($this->tdbmService->getConnection()->getDatabasePlatform() instanceof OraclePlatform) {
$countSql = 'SELECT COUNT(*) FROM ('.$subQuery.')';
} else {
$countSql = 'SELECT COUNT(*) FROM ('.$subQuery.') tmp';
}

if (!empty($this->filterString)) {
$sql .= ' WHERE '.$this->filterString;
$countSql .= ' WHERE '.$this->filterString;
$subQuery .= ' WHERE '.$this->filterString;
}

if (!empty($orderString)) {
$sql .= ' ORDER BY '.$orderString;
}
Expand Down

0 comments on commit 0093d98

Please sign in to comment.