Skip to content

Commit de3f2f6

Browse files
committed
QueryFactory: Cache compute result for findFrom and findFromRaw
1 parent 36c690d commit de3f2f6

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

src/QueryFactory/FindObjectsFromRawSqlQueryFactory.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace TheCodingMachine\TDBM\QueryFactory;
55

6+
use Doctrine\Common\Cache\Cache;
67
use Doctrine\DBAL\Platforms\MySqlPlatform;
78
use Doctrine\DBAL\Schema\Schema;
89
use TheCodingMachine\TDBM\TDBMException;
@@ -39,20 +40,17 @@ class FindObjectsFromRawSqlQueryFactory implements QueryFactory
3940
* @var string
4041
*/
4142
private $mainTable;
42-
4343
/**
44-
* FindObjectsFromRawSqlQueryFactory constructor.
45-
* @param TDBMService $tdbmService
46-
* @param Schema $schema
47-
* @param string $mainTable
48-
* @param string $sql
49-
* @param string $sqlCount
44+
* @var Cache
5045
*/
51-
public function __construct(TDBMService $tdbmService, Schema $schema, string $mainTable, string $sql, string $sqlCount = null)
46+
private $cache;
47+
48+
public function __construct(TDBMService $tdbmService, Schema $schema, string $mainTable, string $sql, ?string $sqlCount, Cache $cache)
5249
{
5350
$this->tdbmService = $tdbmService;
5451
$this->schema = $schema;
5552
$this->mainTable = $mainTable;
53+
$this->cache = $cache;
5654

5755
[$this->processedSql, $this->processedSqlCount, $this->columnDescriptors] = $this->compute($sql, $sqlCount);
5856
}
@@ -85,6 +83,10 @@ public function getColumnDescriptors(): array
8583
*/
8684
private function compute(string $sql, ?string $sqlCount): array
8785
{
86+
$key = 'FindObjectsFromRawSqlQueryFactory_' . dechex(crc32(var_export($sqlCount, true) . $sql));
87+
if ($this->cache->contains($key)) {
88+
return $this->cache->fetch($key);
89+
}
8890
$parser = new PHPSQLParser();
8991
$parsedSql = $parser->parse($sql);
9092

@@ -95,7 +97,7 @@ private function compute(string $sql, ?string $sqlCount): array
9597
} else {
9698
throw new TDBMException('Unable to analyze query "'.$sql.'"');
9799
}
98-
100+
$this->cache->save($key, [$processedSql, $processedSqlCount, $columnDescriptors]);
99101
return [$processedSql, $processedSqlCount, $columnDescriptors];
100102
}
101103

src/QueryFactory/FindObjectsFromSqlQueryFactory.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ public function __construct(string $mainTable, string $from, $filterString, $ord
3636

3737
protected function compute(): void
3838
{
39+
$key = 'FindObjectsFromSqlQueryFactory_' . dechex(crc32($this->mainTable.'__'.$this->from.'__'.$this->filterString.'__'.$this->orderBy));
40+
if ($this->cache->contains($key)) {
41+
[
42+
$this->magicSql,
43+
$this->magicSqlCount,
44+
$this->magicSqlSubQuery,
45+
$this->columnDescList
46+
] = $this->cache->fetch($key);
47+
return;
48+
}
49+
3950
// We quote in MySQL because of MagicQuery that will be applied.
4051
$mySqlPlatform = new MySqlPlatform();
4152

@@ -104,6 +115,13 @@ protected function compute(): void
104115
$this->magicSqlCount = $countSql;
105116
$this->magicSqlSubQuery = $subQuery;
106117
$this->columnDescList = $columnDescList;
118+
119+
$this->cache->save($key, [
120+
$this->magicSql,
121+
$this->magicSqlCount,
122+
$this->magicSqlSubQuery,
123+
$this->columnDescList,
124+
]);
107125
}
108126

109127
/**
@@ -161,12 +179,10 @@ private function getChildrenRelationshipForeignKeysWithoutCache(string $tableNam
161179
return $this->getChildrenRelationshipForeignKeys($fk->getLocalTableName());
162180
}, $children);
163181

164-
$fks = array_merge($children, call_user_func_array('array_merge', $fksTables));
165-
166-
return $fks;
167-
} else {
168-
return [];
182+
return array_merge($children, ...$fksTables);
169183
}
184+
185+
return [];
170186
}
171187

172188
/**

src/TDBMService.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
use Doctrine\Common\Cache\Cache;
2525
use Doctrine\Common\Cache\ClearableCache;
26-
use Doctrine\Common\Cache\VoidCache;
2726
use Doctrine\DBAL\Connection;
2827
use Doctrine\DBAL\DBALException;
2928
use Doctrine\DBAL\Platforms\AbstractPlatform;
@@ -1083,9 +1082,11 @@ private function exploreChildrenTablesRelationships(SchemaAnalyzer $schemaAnalyz
10831082
$tables = [$table];
10841083
$keys = $schemaAnalyzer->getChildrenRelationships($table);
10851084

1085+
$tmpTables = [];
10861086
foreach ($keys as $key) {
1087-
$tables = array_merge($tables, $this->exploreChildrenTablesRelationships($schemaAnalyzer, $key->getLocalTableName()));
1087+
$tmpTables[] = $this->exploreChildrenTablesRelationships($schemaAnalyzer, $key->getLocalTableName());
10881088
}
1089+
$tables = array_merge($tables, ...$tmpTables);
10891090

10901091
return $tables;
10911092
}
@@ -1349,7 +1350,7 @@ public function findObjectsFromRawSql(string $mainTable, string $sql, array $par
13491350

13501351
$mode = $mode ?: $this->mode;
13511352

1352-
$queryFactory = new FindObjectsFromRawSqlQueryFactory($this, $this->tdbmSchemaAnalyzer->getSchema(), $mainTable, $sql, $sqlCount);
1353+
$queryFactory = new FindObjectsFromRawSqlQueryFactory($this, $this->tdbmSchemaAnalyzer->getSchema(), $mainTable, $sql, $sqlCount, $this->cache);
13531354

13541355
return $resultIteratorClass::createResultIterator($queryFactory, $parameters, $this->objectStorage, $className, $this, $this->magicQuery, $mode, $this->logger);
13551356
}

tests/QueryFactory/FindObjectsFromRawSqlQueryFactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
namespace TheCodingMachine\TDBM\QueryFactory;
44

5-
use Doctrine\DBAL\Schema\Schema;
5+
use Doctrine\Common\Cache\VoidCache;
66
use TheCodingMachine\TDBM\TDBMAbstractServiceTest;
77
use TheCodingMachine\TDBM\TDBMException;
88

99
class FindObjectsFromRawSqlQueryFactoryTest extends TDBMAbstractServiceTest
1010
{
1111
public function testGetSubQueryColumnDescriptors(): void
1212
{
13-
$queryFactory = new FindObjectsFromRawSqlQueryFactory($this->tdbmService, $this->tdbmService->getConnection()->getSchemaManager()->createSchema(), 'country', 'SELECT country.* FROM country');
13+
$queryFactory = new FindObjectsFromRawSqlQueryFactory($this->tdbmService, $this->tdbmService->getConnection()->getSchemaManager()->createSchema(), 'country', 'SELECT country.* FROM country', null, new VoidCache());
1414
$this->expectException(TDBMException::class);
1515
$queryFactory->getSubQueryColumnDescriptors();
1616
}
1717

1818
public function testGetMagicSqlSubQuery(): void
1919
{
20-
$queryFactory = new FindObjectsFromRawSqlQueryFactory($this->tdbmService, $this->tdbmService->getConnection()->getSchemaManager()->createSchema(), 'country', 'SELECT country.* FROM country');
20+
$queryFactory = new FindObjectsFromRawSqlQueryFactory($this->tdbmService, $this->tdbmService->getConnection()->getSchemaManager()->createSchema(), 'country', 'SELECT country.* FROM country', null, new VoidCache());
2121
$this->expectException(TDBMException::class);
2222
$queryFactory->getMagicSqlSubQuery();
2323
}

0 commit comments

Comments
 (0)