Skip to content

Commit f981ccc

Browse files
committed
Explorer, Selection, Structure: change of constructor dependencies (BC break)
1 parent 11f5d20 commit f981ccc

File tree

11 files changed

+58
-75
lines changed

11 files changed

+58
-75
lines changed

src/Bridges/DatabaseDI/DatabaseExtension.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Nette\Bridges\DatabaseDI;
1111

1212
use Nette;
13+
use Nette\DI\Definitions\Statement;
1314
use Nette\Schema\Expect;
1415
use Tracy;
1516

@@ -94,13 +95,16 @@ private function setupDatabase(\stdClass $config, string $name): void
9495
}
9596
}
9697

98+
$cacheId = 'Nette.Database.' . hash('xxh128', $name . $config->dsn);
99+
$cache = new Statement(Nette\Caching\Cache::class, [1 => $cacheId]);
100+
97101
$connection = $builder->addDefinition($this->prefix("$name.connection"))
98102
->setFactory(Nette\Database\Connection::class, [$config->dsn, $config->user, $config->password, $config->options])
99103
->setAutowired($config->autowired);
100104

101105
$structure = $builder->addDefinition($this->prefix("$name.structure"))
102106
->setFactory(Nette\Database\Structure::class)
103-
->setArguments([$connection])
107+
->setArguments([new Statement([$connection, 'getDatabaseEngine']), $cache])
104108
->setAutowired($config->autowired);
105109

106110
if (!$config->conventions) {
@@ -119,7 +123,7 @@ private function setupDatabase(\stdClass $config, string $name): void
119123
}
120124

121125
$builder->addDefinition($this->prefix("$name.explorer"))
122-
->setFactory(Nette\Database\Explorer::class, [$connection, $structure, $conventions])
126+
->setFactory(Nette\Database\Explorer::class, [$connection, $structure, $conventions, $cache])
123127
->setAutowired($config->autowired);
124128

125129
$builder->addAlias($this->prefix("$name.context"), $this->prefix("$name.explorer"));

src/Database/Explorer.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(
2626
private readonly Connection $connection,
2727
private readonly IStructure $structure,
2828
?Conventions $conventions = null,
29-
private readonly ?Nette\Caching\Storage $cacheStorage = null,
29+
private readonly ?Nette\Caching\Cache $cache = null,
3030
) {
3131
$this->conventions = $conventions ?: new StaticConventions;
3232
}
@@ -82,7 +82,7 @@ public function queryArgs(string $sql, array $params): Result
8282

8383
public function table(string $table): Table\Selection
8484
{
85-
return new Table\Selection($this, $this->conventions, $table, $this->cacheStorage);
85+
return new Table\Selection($this, $table);
8686
}
8787

8888

@@ -110,6 +110,12 @@ public function getConventions(): Conventions
110110
}
111111

112112

113+
public function getCache(): ?Nette\Caching\Cache
114+
{
115+
return $this->cache;
116+
}
117+
118+
113119
/********************* shortcuts ****************d*g**/
114120

115121

src/Database/Structure.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@
1717
*/
1818
class Structure implements IStructure
1919
{
20-
protected readonly Connection $connection;
21-
protected readonly Nette\Caching\Cache $cache;
22-
2320
/** @var array{tables: array, columns: array, primary: array, aliases: array, hasMany: array, belongsTo: array} */
2421
protected array $structure;
2522
protected bool $isRebuilt = false;
2623

2724

28-
public function __construct(Connection $connection, Nette\Caching\Storage $cacheStorage)
29-
{
30-
$this->connection = $connection;
31-
$this->cache = new Nette\Caching\Cache($cacheStorage, 'Nette.Database.Structure.' . hash('xxh128', $connection->getDsn()));
25+
public function __construct(
26+
protected readonly Drivers\Engine $engine,
27+
protected readonly Nette\Caching\Cache $cache,
28+
) {
3229
}
3330

3431

@@ -94,7 +91,7 @@ public function getPrimaryKeySequence(string $table): ?string
9491
$this->needStructure();
9592
$table = $this->resolveFQTableName($table);
9693

97-
if (!$this->connection->getDatabaseEngine()->isSupported(Drivers\Engine::SupportSequence)) {
94+
if (!$this->engine->isSupported(Drivers\Engine::SupportSequence)) {
9895
return null;
9996
}
10097

@@ -177,10 +174,8 @@ protected function needStructure(): void
177174

178175
protected function loadStructure(): array
179176
{
180-
$engine = $this->connection->getDatabaseEngine();
181-
182177
$structure = [];
183-
$structure['tables'] = $engine->getTables();
178+
$structure['tables'] = $this->engine->getTables();
184179

185180
foreach ($structure['tables'] as $tablePair) {
186181
if (isset($tablePair['fullName'])) {
@@ -190,7 +185,7 @@ protected function loadStructure(): array
190185
$table = $tablePair['name'];
191186
}
192187

193-
$structure['columns'][strtolower($table)] = $columns = $engine->getColumns($table);
188+
$structure['columns'][strtolower($table)] = $columns = $this->engine->getColumns($table);
194189

195190
if (!$tablePair['view']) {
196191
$structure['primary'][strtolower($table)] = $this->analyzePrimaryKey($columns);
@@ -233,7 +228,7 @@ protected function analyzeForeignKeys(array &$structure, string $table): void
233228
{
234229
$lowerTable = strtolower($table);
235230

236-
$foreignKeys = $this->connection->getDatabaseEngine()->getForeignKeys($table);
231+
$foreignKeys = $this->engine->getForeignKeys($table);
237232

238233
usort($foreignKeys, fn($a, $b): int => count($b['local']) <=> count($a['local']));
239234

src/Database/Table/GroupedSelection.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace Nette\Database\Table;
1111

1212
use Nette;
13-
use Nette\Database\Conventions;
1413
use Nette\Database\Explorer;
1514

1615

@@ -38,15 +37,13 @@ class GroupedSelection extends Selection
3837
*/
3938
public function __construct(
4039
Explorer $explorer,
41-
Conventions $conventions,
4240
string $tableName,
4341
string $column,
4442
Selection $refTable,
45-
?Nette\Caching\Storage $cacheStorage = null,
4643
) {
4744
$this->refTable = $refTable;
4845
$this->column = $column;
49-
parent::__construct($explorer, $conventions, $tableName, $cacheStorage);
46+
parent::__construct($explorer, $tableName);
5047
}
5148

5249

src/Database/Table/Selection.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace Nette\Database\Table;
1111

1212
use Nette;
13-
use Nette\Database\Conventions;
1413
use Nette\Database\Explorer;
1514

1615

@@ -24,10 +23,6 @@
2423
class Selection implements \Iterator, \ArrayAccess, \Countable
2524
{
2625
protected readonly Explorer $explorer;
27-
28-
/** back compatibility */
29-
protected Explorer $context;
30-
protected readonly Conventions $conventions;
3126
protected readonly ?Nette\Caching\Cache $cache;
3227
protected SqlBuilder $sqlBuilder;
3328

@@ -72,18 +67,12 @@ class Selection implements \Iterator, \ArrayAccess, \Countable
7267
*/
7368
public function __construct(
7469
Explorer $explorer,
75-
Conventions $conventions,
7670
string $tableName,
77-
?Nette\Caching\Storage $cacheStorage = null,
7871
) {
79-
$this->explorer = $this->context = $explorer;
80-
$this->conventions = $conventions;
72+
$this->explorer = $explorer;
8173
$this->name = $tableName;
82-
83-
$this->cache = $cacheStorage
84-
? new Nette\Caching\Cache($cacheStorage, 'Nette.Database.' . hash('xxh128', $explorer->getConnection()->getDsn()))
85-
: null;
86-
$this->primary = $conventions->getPrimary($tableName);
74+
$this->cache = $explorer->getCache();
75+
$this->primary = $explorer->getConventions()->getPrimary($tableName);
8776
$this->sqlBuilder = new SqlBuilder($tableName, $explorer);
8877
$this->refCache = &$this->getRefTable($refPath)->globalRefCache[$refPath];
8978
}
@@ -555,13 +544,13 @@ protected function createRow(array $row): ActiveRow
555544

556545
public function createSelectionInstance(?string $table = null): self
557546
{
558-
return new self($this->explorer, $this->conventions, $table ?: $this->name, $this->cache?->getStorage());
547+
return new self($this->explorer, $table ?: $this->name);
559548
}
560549

561550

562551
protected function createGroupedSelectionInstance(string $table, string $column): GroupedSelection
563552
{
564-
return new GroupedSelection($this->explorer, $this->conventions, $table, $column, $this, $this->cache?->getStorage());
553+
return new GroupedSelection($this->explorer, $table, $column, $this);
565554
}
566555

567556

@@ -886,7 +875,7 @@ public function delete(): int
886875
public function getReferencedTable(ActiveRow $row, ?string $table, ?string $column = null): ActiveRow|false|null
887876
{
888877
if (!$column) {
889-
$belongsTo = $this->conventions->getBelongsToReference($this->name, $table);
878+
$belongsTo = $this->explorer->getConventions()->getBelongsToReference($this->name, $table);
890879
if (!$belongsTo) {
891880
return false;
892881
}
@@ -939,7 +928,7 @@ public function getReferencingTable(
939928
if (str_contains($table, '.')) {
940929
[$table, $column] = explode('.', $table);
941930
} elseif (!$column) {
942-
$hasMany = $this->conventions->getHasManyReference($this->name, $table);
931+
$hasMany = $this->explorer->getConventions()->getHasManyReference($this->name, $table);
943932
if (!$hasMany) {
944933
return null;
945934
}

tests/Database/Explorer/Explorer.cache.observer.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ $connection = $explorer->getConnection();
1818
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../files/{$driverName}-nette_test1.sql");
1919

2020

21-
$cacheStorage = Mockery::mock(Nette\Caching\Istorage::class);
22-
$cacheStorage->shouldReceive('read')->withAnyArgs()->once()->andReturn(['id' => true]);
23-
$cacheStorage->shouldReceive('read')->withAnyArgs()->times(4)->andReturn(['id' => true, 'author_id' => true]);
24-
$cacheStorage->shouldReceive('write')->with(Mockery::any(), ['id' => true, 'author_id' => true, 'title' => true], []);
21+
$cache = Mockery::mock(Nette\Caching\Cache::class);
22+
$cache->shouldReceive('load')->withAnyArgs()->once()->andReturn(['id' => true]);
23+
$cache->shouldReceive('load')->withAnyArgs()->times(4)->andReturn(['id' => true, 'author_id' => true]);
24+
$cache->shouldReceive('save')->with(Mockery::any(), ['id' => true, 'author_id' => true, 'title' => true]);
2525

26-
$explorer = new Nette\Database\Explorer($connection, $explorer->getStructure(), $explorer->getConventions(), $cacheStorage);
26+
$explorer = new Nette\Database\Explorer($connection, $explorer->getStructure(), $explorer->getConventions(), $cache);
2727

2828
$queries = 0;
2929
$connection->onQuery[] = function ($dao, Result $result) use (&$queries) {

tests/Database/Explorer/Explorer.cache.observer2.phpt

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

88
declare(strict_types=1);
99

10+
use Nette\Caching\Cache;
1011
use Nette\Caching\Storages\MemoryStorage;
1112
use Tester\Assert;
1213

@@ -18,20 +19,20 @@ $connection = $explorer->getConnection();
1819
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../files/{$driverName}-nette_test1.sql");
1920

2021

21-
class CacheMock extends MemoryStorage
22+
class CacheMock extends Cache
2223
{
2324
public int $writes = 0;
2425

2526

26-
public function write(string $key, $data, array $dependencies): void
27+
public function save(mixed $key, mixed $data, ?array $dependencies = null): mixed
2728
{
2829
$this->writes++;
29-
parent::write($key, $data, $dependencies);
30+
return parent::save($key, $data, $dependencies);
3031
}
3132
}
3233

33-
$cacheStorage = new CacheMock;
34-
$explorer = new Nette\Database\Explorer($connection, $explorer->getStructure(), $explorer->getConventions(), $cacheStorage);
34+
$cache = new CacheMock(new MemoryStorage);
35+
$explorer = new Nette\Database\Explorer($connection, $explorer->getStructure(), $explorer->getConventions(), $cache);
3536

3637
for ($i = 0; $i < 2; ++$i) {
3738
$authors = $explorer->table('author');
@@ -52,4 +53,4 @@ for ($i = 0; $i < 2; ++$i) {
5253
}
5354

5455
Assert::same(reformat('SELECT [id], [name] FROM [author]'), $sql);
55-
Assert::same(2, $cacheStorage->writes);
56+
Assert::same(2, $cache->writes);

tests/Database/Explorer/bugs/staticReflection.undeclaredColumn.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ $explorer = connectToDB();
1515
$connection = $explorer->getConnection();
1616

1717
$conventions = new Nette\Database\Conventions\StaticConventions;
18-
$cacheStorage = new Nette\Caching\Storages\MemoryStorage;
19-
$explorer = new Nette\Database\Explorer($explorer->getConnection(), $explorer->getStructure(), $conventions, $cacheStorage);
18+
$cache = new Nette\Caching\Cache(new Nette\Caching\Storages\MemoryStorage);
19+
$explorer = new Nette\Database\Explorer($explorer->getConnection(), $explorer->getStructure(), $conventions, $cache);
2020

2121
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../../files/{$driverName}-nette_test1.sql");
2222

tests/Database/Structure.phpt

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

77
declare(strict_types=1);
88

9+
use Mockery\MockInterface;
910
use Nette\Database\Structure;
1011
use Tester\Assert;
1112
use Tester\TestCase;
@@ -29,21 +30,17 @@ class StructureMock extends Structure
2930
*/
3031
class StructureTestCase extends TestCase
3132
{
32-
private Nette\Database\Connection $connection;
33-
private Nette\Database\Drivers\Engine $engine;
34-
private Nette\Caching\Storage $storage;
35-
private Structure $structure;
33+
private Nette\Database\Drivers\Engine|MockInterface $engine;
34+
private Nette\Caching\Cache|MockInterface $cache;
35+
private Structure|MockInterface $structure;
3636

3737

3838
protected function setUp()
3939
{
4040
parent::setUp();
4141
$this->engine = Mockery::mock(Nette\Database\Drivers\Engine::class);
42-
$this->connection = Mockery::mock(Nette\Database\Connection::class);
43-
$this->storage = Mockery::mock(Nette\Caching\Storage::class);
42+
$this->cache = Mockery::mock(Nette\Caching\Cache::class);
4443

45-
$this->connection->shouldReceive('getDsn')->once()->andReturn('');
46-
$this->connection->shouldReceive('getDatabaseEngine')->once()->andReturn($this->engine);
4744
$this->engine->shouldReceive('getTables')->once()->andReturn([
4845
['name' => 'authors', 'view' => false],
4946
['name' => 'Books', 'view' => false],
@@ -71,7 +68,6 @@ class StructureTestCase extends TestCase
7168
['name' => 'id', 'primary' => false, 'autoIncrement' => false, 'vendor' => []],
7269
['name' => 'title', 'primary' => false, 'autoIncrement' => false, 'vendor' => []],
7370
]);
74-
$this->connection->shouldReceive('getDatabaseEngine')->times(4)->andReturn($this->engine);
7571
$this->engine->shouldReceive('getForeignKeys')->with('authors')->once()->andReturn([]);
7672
$this->engine->shouldReceive('getForeignKeys')->with('Books')->once()->andReturn([
7773
['local' => ['author_id'], 'table' => 'authors', 'foreign' => ['id'], 'name' => 'authors_fk1'],
@@ -83,7 +79,7 @@ class StructureTestCase extends TestCase
8379
['local' => ['tag_id'], 'table' => 'tags', 'foreign' => ['id'], 'name' => 'books_x_tags_fk2'],
8480
]);
8581

86-
$this->structure = new StructureMock($this->connection, $this->storage);
82+
$this->structure = new StructureMock($this->engine, $this->cache);
8783
}
8884

8985

@@ -132,7 +128,6 @@ class StructureTestCase extends TestCase
132128

133129
public function testGetPrimaryKeySequence()
134130
{
135-
$this->connection->shouldReceive('getDatabaseEngine')->times(4)->andReturn($this->engine);
136131
$this->engine->shouldReceive('isSupported')->with('sequence')->once()->andReturn(false);
137132
$this->engine->shouldReceive('isSupported')->with('sequence')->times(3)->andReturn(true);
138133

0 commit comments

Comments
 (0)