Skip to content

Commit b5fd2b5

Browse files
committed
merging Connection & Explorer classes into one (BC break)
1 parent 7b9836a commit b5fd2b5

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
@@ -99,38 +99,30 @@ private function setupDatabase(\stdClass $config, string $name): void
9999
$cacheId = 'Nette.Database.' . hash('xxh128', $name . $config->dsn);
100100
$cache = new Statement(Nette\Caching\Cache::class, [1 => $cacheId]);
101101

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

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

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

122117
} else {
123-
$conventions = Nette\DI\Helpers::filterArguments([$config->conventions])[0];
118+
$explorer->addSetup('setConventions', [Nette\DI\Helpers::filterArguments([$config->conventions])[0]]);
124119
}
125120

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

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

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;
@@ -35,7 +35,7 @@ class ConnectionPanel implements Tracy\IBarPanel
3535

3636

3737
public static function initialize(
38-
Connection $connection,
38+
Explorer $explorer,
3939
bool $addBarPanel = true,
4040
string $name = '',
4141
bool $explain = true,
@@ -47,7 +47,7 @@ public static function initialize(
4747
$blueScreen->addPanel(self::renderException(...));
4848

4949
if ($addBarPanel) {
50-
$panel = new self($connection, $blueScreen);
50+
$panel = new self($explorer, $blueScreen);
5151
$panel->explain = $explain;
5252
$panel->name = $name;
5353
$bar ??= Tracy\Debugger::getBar();
@@ -58,14 +58,14 @@ public static function initialize(
5858
}
5959

6060

61-
public function __construct(Connection $connection, Tracy\BlueScreen $blueScreen)
61+
public function __construct(Explorer $explorer, Tracy\BlueScreen $blueScreen)
6262
{
63-
$connection->onQuery[] = $this->logQuery(...);
63+
$explorer->onQuery[] = $this->logQuery(...);
6464
$this->blueScreen = $blueScreen;
6565
}
6666

6767

68-
private function logQuery(Connection $connection, $result): void
68+
private function logQuery(Explorer $connection, $result): void
6969
{
7070
if ($this->disabled) {
7171
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,14 +11,15 @@
1111

1212
use JetBrains\PhpStorm\Language;
1313
use Nette;
14+
use Nette\Caching\Cache;
1415
use Nette\Utils\Arrays;
1516
use function func_get_args, str_replace, ucfirst;
1617

1718

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

4751

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

src/Database/Helpers.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static function dumpResult(Result $result): void
7777
/**
7878
* Returns syntax highlighted SQL command.
7979
*/
80-
public static function dumpSql(SqlLiteral $query, ?Connection $connection = null): string
80+
public static function dumpSql(SqlLiteral $query, ?Explorer $explorer = null): string
8181
{
8282
$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';
8383
$keywords2 = 'ALL|DISTINCT|DISTINCTROW|IGNORE|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|[RI]?LIKE|REGEXP|TRUE|FALSE';
@@ -112,7 +112,7 @@ public static function dumpSql(SqlLiteral $query, ?Connection $connection = null
112112

113113
// parameters
114114
$params = $query->getParameters();
115-
$sql = preg_replace_callback('#\?#', function () use ($params, $connection): string {
115+
$sql = preg_replace_callback('#\?#', function () use ($params, $explorer): string {
116116
static $i = 0;
117117
$param = $params[$i++] ?? null;
118118
if ($param === null) {
@@ -130,7 +130,7 @@ public static function dumpSql(SqlLiteral $query, ?Connection $connection = null
130130
} elseif (is_string($param)) {
131131
$length = Nette\Utils\Strings::length($param);
132132
$truncated = Nette\Utils\Strings::truncate($param, self::$maxLength);
133-
$text = htmlspecialchars($connection ? $connection->quote($truncated) : '\'' . $truncated . '\'', ENT_NOQUOTES, 'UTF-8');
133+
$text = htmlspecialchars($explorer ? $explorer->quote($truncated) : '\'' . $truncated . '\'', ENT_NOQUOTES, 'UTF-8');
134134
return '<span title="Length ' . $length . ' characters">' . $text . '</span>';
135135

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

@@ -173,7 +173,7 @@ public static function loadFromFile(Connection $connection, string $file, ?calla
173173
$count = $size = 0;
174174
$delimiter = ';';
175175
$sql = '';
176-
$connection = $connection->getConnection(); // native query without logging
176+
$connection = $explorer->getConnection(); // native query without logging
177177
while (($s = fgets($handle)) !== false) {
178178
$size += strlen($s);
179179
if (!strncasecmp($s, 'DELIMITER ', 10)) {
@@ -207,7 +207,7 @@ public static function loadFromFile(Connection $connection, string $file, ?calla
207207

208208
/** @deprecated use Nette\Bridges\DatabaseTracy\ConnectionPanel::initialize() */
209209
public static function createDebugPanel(
210-
Connection $connection,
210+
Explorer $connection,
211211
bool $explain,
212212
string $name,
213213
Tracy\Bar $bar,
@@ -221,7 +221,7 @@ public static function createDebugPanel(
221221

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

src/Database/Result.php

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

2929

3030
public function __construct(
31-
private readonly Connection $connection,
31+
private readonly Explorer $explorer,
3232
private readonly SqlLiteral $query,
3333
private readonly ?Drivers\Result $result,
3434
private float $time,
@@ -37,10 +37,10 @@ public function __construct(
3737

3838

3939
/** @deprecated */
40-
public function getConnection(): Connection
40+
public function getConnection(): Explorer
4141
{
4242
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
43-
return $this->connection;
43+
return $this->explorer;
4444
}
4545

4646

@@ -226,8 +226,8 @@ public function fetchAll(): array
226226

227227
private function normalizeRow(array $row): array
228228
{
229-
$engine = $this->connection->getDatabaseEngine();
230-
$converter = $this->connection->getTypeConverter();
229+
$engine = $this->explorer->getDatabaseEngine();
230+
$converter = $this->explorer->getTypeConverter();
231231
$this->meta ??= $this->getColumnsMeta();
232232
foreach ($row as $key => $value) {
233233
$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
@@ -59,10 +59,10 @@ class SqlPreprocessor
5959
private ?string $arrayMode;
6060

6161

62-
public function __construct(Connection $connection)
62+
public function __construct(Explorer $explorer)
6363
{
64-
$this->connection = $connection->getConnection();
65-
$this->engine = $connection->getDatabaseEngine();
64+
$this->connection = $explorer->getConnection();
65+
$this->engine = $explorer->getDatabaseEngine();
6666
}
6767

6868

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)