Skip to content

Commit 2c868d0

Browse files
committed
Adding a getMagicSqlSubQuery to the QueryFactory.
1 parent 06fa71d commit 2c868d0

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

src/QueryFactory/AbstractQueryFactory.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,18 @@ abstract class AbstractQueryFactory implements QueryFactory
3434
*/
3535
protected $orderBy;
3636

37+
/**
38+
* @var string|null
39+
*/
3740
protected $magicSql;
41+
/**
42+
* @var string|null
43+
*/
3844
protected $magicSqlCount;
45+
/**
46+
* @var string|null
47+
*/
48+
protected $magicSqlSubQuery;
3949
protected $columnDescList;
4050

4151
/**
@@ -214,6 +224,15 @@ public function getMagicSqlCount() : string
214224
return $this->magicSqlCount;
215225
}
216226

227+
public function getMagicSqlSubQuery() : string
228+
{
229+
if ($this->magicSqlSubQuery === null) {
230+
$this->compute();
231+
}
232+
233+
return $this->magicSqlSubQuery;
234+
}
235+
217236
public function getColumnDescriptors() : array
218237
{
219238
if ($this->columnDescList === null) {

src/QueryFactory/FindObjectsFromRawSqlQueryFactory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,4 +395,15 @@ protected function getTableGroupName(array $relatedTables): string
395395
sort($relatedTables);
396396
return implode('_``_', $relatedTables);
397397
}
398+
399+
/**
400+
* Returns a sub-query to be used in another query.
401+
* A sub-query is similar to a query except it returns only the primary keys of the table (to be used as filters)
402+
*
403+
* @return string
404+
*/
405+
public function getMagicSqlSubQuery(): string
406+
{
407+
throw new TDBMException('Using resultset generated from findFromRawSql as subqueries is unsupported for now.');
408+
}
398409
}

src/QueryFactory/FindObjectsFromSqlQueryFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use TheCodingMachine\TDBM\OrderByAnalyzer;
1212
use TheCodingMachine\TDBM\TDBMException;
1313
use TheCodingMachine\TDBM\TDBMService;
14+
use function implode;
1415

1516
/**
1617
* This class is in charge of creating the MagicQuery SQL based on parameters passed to findObjectsFromSql method.
@@ -55,6 +56,7 @@ protected function compute(): void
5556
}, $pkColumnNames);
5657

5758
$countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM '.$this->from;
59+
$subQuery = 'SELECT DISTINCT '.implode(', ', $pkColumnNames).' FROM '.$this->from;
5860

5961
// Add joins on inherited tables if necessary
6062
if (count($allFetchedTables) > 1) {
@@ -89,6 +91,7 @@ protected function compute(): void
8991
if (!empty($this->filterString)) {
9092
$sql .= ' WHERE '.$this->filterString;
9193
$countSql .= ' WHERE '.$this->filterString;
94+
$subQuery .= ' WHERE '.$this->filterString;
9295
}
9396

9497
if (!empty($orderString)) {
@@ -101,6 +104,7 @@ protected function compute(): void
101104

102105
$this->magicSql = $sql;
103106
$this->magicSqlCount = $countSql;
107+
$this->magicSqlSubQuery = $subQuery;
104108
$this->columnDescList = $columnDescList;
105109
}
106110

src/QueryFactory/FindObjectsQueryFactory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\DBAL\Schema\Schema;
99
use TheCodingMachine\TDBM\OrderByAnalyzer;
1010
use TheCodingMachine\TDBM\TDBMService;
11+
use function implode;
1112

1213
/**
1314
* This class is in charge of creating the MagicQuery SQL based on parameters passed to findObjects method.
@@ -52,15 +53,18 @@ protected function compute(): void
5253
return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn);
5354
}, $pkColumnNames);
5455

56+
$subQuery = 'SELECT DISTINCT '.implode(', ', $pkColumnNames).' FROM MAGICJOIN('.$this->mainTable.')';
57+
5558
if (count($pkColumnNames) === 1 || $this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform) {
5659
$countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM MAGICJOIN('.$this->mainTable.')';
5760
} else {
58-
$countSql = 'SELECT COUNT(*) FROM (SELECT DISTINCT '.implode(', ', $pkColumnNames).' FROM MAGICJOIN('.$this->mainTable.')) tmp';
61+
$countSql = 'SELECT COUNT(*) FROM ('.$subQuery.') tmp';
5962
}
6063

6164
if (!empty($this->filterString)) {
6265
$sql .= ' WHERE '.$this->filterString;
6366
$countSql .= ' WHERE '.$this->filterString;
67+
$subQuery .= ' WHERE '.$this->filterString;
6468
}
6569

6670
if (!empty($orderString)) {
@@ -69,6 +73,7 @@ protected function compute(): void
6973

7074
$this->magicSql = $sql;
7175
$this->magicSqlCount = $countSql;
76+
$this->magicSqlSubQuery = $subQuery;
7277
$this->columnDescList = $columnDescList;
7378

7479
$this->cache->save($key, [

src/QueryFactory/QueryFactory.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ public function getMagicSql() : string;
3131
public function getMagicSqlCount() : string;
3232

3333
/**
34-
* @return mixed[][] An array of column descriptors. The key is in the form "$tableName____$columnName". Value is an array with those keys: as, table, colum, type, tableGroup
34+
* Returns a sub-query to be used in another query.
35+
* A sub-query is similar to a query except it returns only the primary keys of the table (to be used as filters)
36+
*
37+
* @return string
38+
*/
39+
public function getMagicSqlSubQuery() : string;
40+
41+
/**
42+
* @return mixed[][] An array of column descriptors. The key is in the form "$tableName____$columnName". Value is an array with those keys: as, table, column, type, tableGroup
3543
*/
3644
public function getColumnDescriptors() : array;
3745
}

0 commit comments

Comments
 (0)