Skip to content

Commit 97d7d28

Browse files
committed
merging Connection & Explorer classes into one (BC break)
1 parent f74a23a commit 97d7d28

File tree

106 files changed

+295
-293
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+295
-293
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Database Core
5959
To create a new database connection just create a new instance of `Nette\Database\Connection` class:
6060

6161
```php
62-
$database = new Nette\Database\Connection($dsn, $user, $password); // the same arguments as uses PDO
62+
$database = new Nette\Database\Explorer($dsn, $user, $password); // the same arguments as uses PDO
6363
```
6464

6565
Connection allows you to easily query your database by calling `query` method:

src/Bridges/DatabaseDI/DatabaseExtension.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,38 +98,30 @@ private function setupDatabase(\stdClass $config, string $name): void
9898
$cacheId = 'Nette.Database.' . hash('xxh128', $name . $config->dsn);
9999
$cache = new Statement(Nette\Caching\Cache::class, [1 => $cacheId]);
100100

101-
$connection = $builder->addDefinition($this->prefix("$name.connection"))
102-
->setFactory(Nette\Database\Connection::class, [$config->dsn, $config->user, $config->password, $config->options])
101+
$explorer = $builder->addDefinition($this->prefix($name))
102+
->setFactory(Nette\Database\Explorer::class, [$config->dsn, $config->user, $config->password, $config->options])
103+
->addSetup('setCache', [$cache])
103104
->setAutowired($config->autowired);
104105

105-
$structure = $builder->addDefinition($this->prefix("$name.structure"))
106-
->setFactory(Nette\Database\Structure::class)
107-
->setArguments([new Statement([$connection, 'getDatabaseEngine']), $cache])
108-
->setAutowired($config->autowired);
109-
110-
if (!$config->conventions) {
111-
$conventions = null;
106+
if (!$config->conventions || $config->conventions === 'discovered') {
112107

113108
} elseif (is_string($config->conventions)) {
114109
$conventions = $builder->addDefinition($this->prefix("$name.conventions"))
115110
->setFactory(preg_match('#^[a-z]+$#Di', $config->conventions)
116111
? 'Nette\Database\Conventions\\' . ucfirst($config->conventions) . 'Conventions'
117112
: $config->conventions)
118-
->setArguments(strtolower($config->conventions) === 'discovered' ? [$structure] : [])
119113
->setAutowired($config->autowired);
114+
$explorer->addSetup('setConventions', [$conventions]);
120115

121116
} else {
122-
$conventions = Nette\DI\Helpers::filterArguments([$config->conventions])[0];
117+
$explorer->addSetup('setConventions', [Nette\DI\Helpers::filterArguments([$config->conventions])[0]]);
123118
}
124119

125-
$builder->addDefinition($this->prefix("$name.explorer"))
126-
->setFactory(Nette\Database\Explorer::class, [$connection, $structure, $conventions, $cache])
127-
->setAutowired($config->autowired);
128-
129-
$builder->addAlias($this->prefix("$name.context"), $this->prefix("$name.explorer"));
120+
$builder->addAlias($this->prefix("$name.connection"), $this->prefix($name));
121+
$builder->addAlias($this->prefix("$name.context"), $this->prefix($name));
122+
$builder->addAlias($this->prefix("$name.explorer"), $this->prefix($name));
130123

131124
if ($this->name === 'database') {
132-
$builder->addAlias($this->prefix($name), $this->prefix("$name.connection"));
133125
$builder->addAlias("nette.database.$name", $this->prefix($name));
134126
$builder->addAlias("nette.database.$name.context", $this->prefix("$name.explorer"));
135127
}

src/Bridges/DatabaseTracy/ConnectionPanel.php

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

1212
use Nette;
13-
use Nette\Database\Connection;
1413
use Nette\Database\DriverException;
14+
use Nette\Database\Explorer;
1515
use Nette\Database\Helpers;
1616
use Nette\Database\Result;
1717
use Tracy;
@@ -34,7 +34,7 @@ class ConnectionPanel implements Tracy\IBarPanel
3434

3535

3636
public static function initialize(
37-
Connection $connection,
37+
Explorer $explorer,
3838
bool $addBarPanel = true,
3939
string $name = '',
4040
bool $explain = true,
@@ -46,7 +46,7 @@ public static function initialize(
4646
$blueScreen->addPanel(self::renderException(...));
4747

4848
if ($addBarPanel) {
49-
$panel = new self($connection, $blueScreen);
49+
$panel = new self($explorer, $blueScreen);
5050
$panel->explain = $explain;
5151
$panel->name = $name;
5252
$bar ??= Tracy\Debugger::getBar();
@@ -57,14 +57,14 @@ public static function initialize(
5757
}
5858

5959

60-
public function __construct(Connection $connection, Tracy\BlueScreen $blueScreen)
60+
public function __construct(Explorer $explorer, Tracy\BlueScreen $blueScreen)
6161
{
62-
$connection->onQuery[] = $this->logQuery(...);
62+
$explorer->onQuery[] = $this->logQuery(...);
6363
$this->blueScreen = $blueScreen;
6464
}
6565

6666

67-
private function logQuery(Connection $connection, $result): void
67+
private function logQuery(Explorer $connection, $result): void
6868
{
6969
if ($this->disabled) {
7070
return;

src/Database/Connection.php renamed to src/Database/Explorer.php

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111

1212
use JetBrains\PhpStorm\Language;
1313
use Nette;
14+
use Nette\Caching\Cache;
1415
use Nette\Utils\Arrays;
1516

1617

1718
/**
1819
* Manages database connection and executes SQL queries.
1920
*/
20-
class Connection
21+
class Explorer
2122
{
2223
private const Drivers = [
2324
'pdo-mssql' => Drivers\PDO\MSSQL\Driver::class,
@@ -42,6 +43,9 @@ class Connection
4243
private TypeConverter $typeConverter;
4344
private ?SqlLiteral $lastQuery = null;
4445
private int $transactionDepth = 0;
46+
private ?Cache $cache = null;
47+
private ?Conventions $conventions = null;
48+
private ?IStructure $structure = null;
4549

4650

4751
public function __construct(
@@ -415,4 +419,75 @@ public static function literal(string $value, ...$params): SqlLiteral
415419
{
416420
return new SqlLiteral($value, $params);
417421
}
422+
423+
424+
/********************* active row ****************d*g**/
425+
426+
427+
public function table(string $table): Table\Selection
428+
{
429+
return new Table\Selection($this, $table);
430+
}
431+
432+
433+
public function setCache(Cache $cache): static
434+
{
435+
if (isset($this->structure)) {
436+
throw new \LogicException('Cannot set cache after structure is created.');
437+
}
438+
$this->cache = $cache;
439+
return $this;
440+
}
441+
442+
443+
/** @internal */
444+
public function getCache(): ?Cache
445+
{
446+
return $this->cache;
447+
}
448+
449+
450+
public function setConventions(Conventions $conventions): static
451+
{
452+
if (isset($this->conventions)) {
453+
throw new \LogicException('Conventions are already set.');
454+
}
455+
$this->conventions = $conventions;
456+
return $this;
457+
}
458+
459+
460+
/** @internal */
461+
public function getConventions(): Conventions
462+
{
463+
return $this->conventions ??= new Conventions\DiscoveredConventions($this->getStructure());
464+
}
465+
466+
467+
/** @internal */
468+
public function getStructure(): IStructure
469+
{
470+
return $this->structure ??= new Structure($this->getDatabaseEngine(), $this->getCache());
471+
}
472+
473+
474+
/** @internal */
475+
public function createActiveRow(Table\Selection $selection, array $row): Table\ActiveRow
476+
{
477+
return new Table\ActiveRow($row, $selection);
478+
}
479+
480+
481+
/** @internal */
482+
public function createGroupedSelectionInstance(
483+
Table\Selection $selection,
484+
string $table,
485+
string $column,
486+
): Table\GroupedSelection
487+
{
488+
return new Table\GroupedSelection($this, $table, $column, $selection);
489+
}
418490
}
491+
492+
493+
class_exists(Connection::class);

src/Database/Helpers.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static function dumpResult(Result $result): void
7575
/**
7676
* Returns syntax highlighted SQL command.
7777
*/
78-
public static function dumpSql(SqlLiteral $query, ?Connection $connection = null): string
78+
public static function dumpSql(SqlLiteral $query, ?Explorer $explorer = null): string
7979
{
8080
$keywords1 = 'SELECT|(?:ON\s+DUPLICATE\s+KEY)?UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|DELETE|CALL|UNION|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE';
8181
$keywords2 = 'ALL|DISTINCT|DISTINCTROW|IGNORE|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|[RI]?LIKE|REGEXP|TRUE|FALSE';
@@ -110,7 +110,7 @@ public static function dumpSql(SqlLiteral $query, ?Connection $connection = null
110110

111111
// parameters
112112
$params = $query->getParameters();
113-
$sql = preg_replace_callback('#\?#', function () use ($params, $connection): string {
113+
$sql = preg_replace_callback('#\?#', function () use ($params, $explorer): string {
114114
static $i = 0;
115115
$param = $params[$i++] ?? null;
116116
if ($param === null) {
@@ -128,7 +128,7 @@ public static function dumpSql(SqlLiteral $query, ?Connection $connection = null
128128
} elseif (is_string($param)) {
129129
$length = Nette\Utils\Strings::length($param);
130130
$truncated = Nette\Utils\Strings::truncate($param, self::$maxLength);
131-
$text = htmlspecialchars($connection ? $connection->quote($truncated) : '\'' . $truncated . '\'', ENT_NOQUOTES, 'UTF-8');
131+
$text = htmlspecialchars($explorer ? $explorer->quote($truncated) : '\'' . $truncated . '\'', ENT_NOQUOTES, 'UTF-8');
132132
return '<span title="Length ' . $length . ' characters">' . $text . '</span>';
133133

134134
} elseif (is_resource($param)) {
@@ -158,7 +158,7 @@ public static function dumpSql(SqlLiteral $query, ?Connection $connection = null
158158
* @return int Number of executed commands
159159
* @throws Nette\FileNotFoundException
160160
*/
161-
public static function loadFromFile(Connection $connection, string $file, ?callable $onProgress = null): int
161+
public static function loadFromFile(Explorer $explorer, string $file, ?callable $onProgress = null): int
162162
{
163163
@set_time_limit(0); // @ function may be disabled
164164

@@ -171,7 +171,7 @@ public static function loadFromFile(Connection $connection, string $file, ?calla
171171
$count = $size = 0;
172172
$delimiter = ';';
173173
$sql = '';
174-
$connection = $connection->getConnection(); // native query without logging
174+
$connection = $explorer->getConnection(); // native query without logging
175175
while (($s = fgets($handle)) !== false) {
176176
$size += strlen($s);
177177
if (!strncasecmp($s, 'DELIMITER ', 10)) {
@@ -205,7 +205,7 @@ public static function loadFromFile(Connection $connection, string $file, ?calla
205205

206206
/** @deprecated use Nette\Bridges\DatabaseTracy\ConnectionPanel::initialize() */
207207
public static function createDebugPanel(
208-
Connection $connection,
208+
Explorer $connection,
209209
bool $explain,
210210
string $name,
211211
Tracy\Bar $bar,
@@ -219,7 +219,7 @@ public static function createDebugPanel(
219219

220220
/** @deprecated use Nette\Bridges\DatabaseTracy\ConnectionPanel::initialize() */
221221
public static function initializeTracy(
222-
Connection $connection,
222+
Explorer $connection,
223223
bool $addBarPanel = false,
224224
string $name = '',
225225
bool $explain = true,

src/Database/Result.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Result implements \Iterator
2727

2828

2929
public function __construct(
30-
private readonly Connection $connection,
30+
private readonly Explorer $explorer,
3131
private readonly SqlLiteral $query,
3232
private readonly ?Drivers\Result $result,
3333
private float $time,
@@ -36,10 +36,10 @@ public function __construct(
3636

3737

3838
/** @deprecated */
39-
public function getConnection(): Connection
39+
public function getConnection(): Explorer
4040
{
4141
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
42-
return $this->connection;
42+
return $this->explorer;
4343
}
4444

4545

@@ -225,8 +225,8 @@ public function fetchAll(): array
225225

226226
private function normalizeRow(array $row): array
227227
{
228-
$engine = $this->connection->getDatabaseEngine();
229-
$converter = $this->connection->getTypeConverter();
228+
$engine = $this->explorer->getDatabaseEngine();
229+
$converter = $this->explorer->getTypeConverter();
230230
$this->meta ??= $this->getColumnsMeta();
231231
foreach ($row as $key => $value) {
232232
$row[$key] = isset($value, $this->meta[$key])

src/Database/SqlPreprocessor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ class SqlPreprocessor
5858
private ?string $arrayMode;
5959

6060

61-
public function __construct(Connection $connection)
61+
public function __construct(Explorer $explorer)
6262
{
63-
$this->connection = $connection->getConnection();
64-
$this->engine = $connection->getDatabaseEngine();
63+
$this->connection = $explorer->getConnection();
64+
$this->engine = $explorer->getDatabaseEngine();
6565
}
6666

6767

src/compatibility.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ class ResultSet extends Result
2626
} elseif (!class_exists(ResultSet::class)) {
2727
class_alias(Result::class, ResultSet::class);
2828
}
29+
30+
if (false) {
31+
class Connection extends Explorer
32+
{
33+
}
34+
} elseif (!class_exists(Connection::class)) {
35+
class_alias(Explorer::class, Connection::class);
36+
}

tests/Database.DI/DatabaseExtension.basic.phpt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,12 @@ test('', function () {
3636
$container = new Container1;
3737
$container->initialize();
3838

39-
$connection = $container->getService('database.default');
40-
Assert::type(Nette\Database\Connection::class, $connection);
41-
42-
$explorer = $container->getService('database.default.explorer');
39+
$explorer = $container->getService('database.default');
4340
Assert::type(Nette\Database\Explorer::class, $explorer);
44-
Assert::same($connection, $explorer->getConnection());
45-
Assert::same($container->getService('database.default.context'), $explorer);
46-
47-
Assert::type(Nette\Database\Structure::class, $explorer->getStructure());
48-
Assert::type(Nette\Database\Conventions\DiscoveredConventions::class, $explorer->getConventions());
41+
Assert::type(Nette\Caching\Cache::class, $explorer->getCache());
4942

5043
// aliases
51-
Assert::same($connection, $container->getService('nette.database.default'));
44+
Assert::same($explorer, $container->getService('database.default.explorer'));
45+
Assert::same($explorer, $container->getService('nette.database.default'));
5246
Assert::same($explorer, $container->getService('nette.database.default.context'));
5347
});

tests/Database.DI/DatabaseExtension.multiple.phpt

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,13 @@ test('', function () {
4141
$container = new Container1;
4242
$container->initialize();
4343

44-
$connection = $container->getService('database.first');
45-
Assert::type(Nette\Database\Connection::class, $connection);
46-
Assert::same($connection, $container->getByType(Nette\Database\Connection::class));
47-
48-
$explorer = $container->getService('database.first.explorer');
44+
$explorer = $container->getService('database.first');
4945
Assert::type(Nette\Database\Explorer::class, $explorer);
5046
Assert::same($explorer, $container->getByType(Nette\Database\Explorer::class));
51-
Assert::same($connection, $explorer->getConnection());
52-
Assert::same($container->getService('database.first.context'), $explorer);
53-
54-
Assert::type(Nette\Database\Structure::class, $explorer->getStructure());
55-
Assert::same($explorer->getStructure(), $container->getByType(Nette\Database\IStructure::class));
56-
Assert::type(Nette\Database\Conventions\DiscoveredConventions::class, $explorer->getConventions());
57-
Assert::same($explorer->getConventions(), $container->getByType(Nette\Database\Conventions::class));
47+
Assert::type(Nette\Caching\Cache::class, $explorer->getCache());
5848

5949
// aliases
60-
Assert::same($connection, $container->getService('nette.database.first'));
50+
Assert::same($explorer, $container->getService('database.first.explorer'));
51+
Assert::same($explorer, $container->getService('nette.database.first'));
6152
Assert::same($explorer, $container->getService('nette.database.first.context'));
6253
});

0 commit comments

Comments
 (0)