Skip to content

Commit 44218c2

Browse files
committed
merging Connection & Explorer classes into one (BC break)
1 parent 7fc4ae8 commit 44218c2

File tree

106 files changed

+279
-295
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

+279
-295
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: 7 additions & 7 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;
@@ -82,7 +82,7 @@ private function logQuery(Connection $connection, $result): void
8282
&& preg_match('~\.(php.?|phtml)$~', $row['file'])
8383
&& !$this->blueScreen->isCollapsed($row['file']))
8484
&& ($row['class'] ?? '') !== self::class
85-
&& !is_a($row['class'] ?? '', Connection::class, allow_string: true)
85+
&& !is_a($row['class'] ?? '', Explorer::class, allow_string: true)
8686
) {
8787
$source = [$row['file'], (int) $row['line']];
8888
break;

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

Lines changed: 59 additions & 2 deletions
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
/**
18-
* Represents a connection between PHP and a database server.
19+
* The central access point to Nette Database functionality.
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(
@@ -385,4 +389,57 @@ public static function literal(string $value, ...$params): SqlLiteral
385389
{
386390
return new SqlLiteral($value, $params);
387391
}
392+
393+
394+
/********************* active row ****************d*g**/
395+
396+
397+
public function table(string $table): Table\Selection
398+
{
399+
return new Table\Selection($this, $table);
400+
}
401+
402+
403+
public function setCache(Cache $cache): static
404+
{
405+
if (isset($this->structure)) {
406+
throw new \LogicException('Cannot set cache after structure is created.');
407+
}
408+
$this->cache = $cache;
409+
return $this;
410+
}
411+
412+
413+
/** @internal */
414+
public function getCache(): ?Cache
415+
{
416+
return $this->cache;
417+
}
418+
419+
420+
public function setConventions(Conventions $conventions): static
421+
{
422+
if (isset($this->conventions)) {
423+
throw new \LogicException('Conventions are already set.');
424+
}
425+
$this->conventions = $conventions;
426+
return $this;
427+
}
428+
429+
430+
/** @internal */
431+
public function getConventions(): Conventions
432+
{
433+
return $this->conventions ??= new Conventions\DiscoveredConventions($this->getStructure());
434+
}
435+
436+
437+
/** @internal */
438+
public function getStructure(): IStructure
439+
{
440+
return $this->structure ??= new Structure($this->getDatabaseEngine(), $this->getCache());
441+
}
388442
}
443+
444+
445+
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)) {
@@ -157,7 +157,7 @@ public static function dumpSql(SqlLiteral $query, ?Connection $connection = null
157157
* @param ?array<callable(int, ?float): void> $onProgress
158158
* @return int count of commands
159159
*/
160-
public static function loadFromFile(Connection $connection, string $file, ?callable $onProgress = null): int
160+
public static function loadFromFile(Explorer $explorer, string $file, ?callable $onProgress = null): int
161161
{
162162
@set_time_limit(0); // @ function may be disabled
163163

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

205205
/** @deprecated use Nette\Bridges\DatabaseTracy\ConnectionPanel::initialize() */
206206
public static function createDebugPanel(
207-
Connection $connection,
207+
Explorer $connection,
208208
bool $explain,
209209
string $name,
210210
Tracy\Bar $bar,
@@ -218,7 +218,7 @@ public static function createDebugPanel(
218218

219219
/** @deprecated use Nette\Bridges\DatabaseTracy\ConnectionPanel::initialize() */
220220
public static function initializeTracy(
221-
Connection $connection,
221+
Explorer $connection,
222222
bool $addBarPanel = false,
223223
string $name = '',
224224
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

@@ -237,8 +237,8 @@ private function normalizeRow(array $row): array
237237
private function chooseColumnConverters(): array
238238
{
239239
$res = [];
240-
$engine = $this->connection->getDatabaseEngine();
241-
$converter = $this->connection->getTypeConverter();
240+
$engine = $this->explorer->getDatabaseEngine();
241+
$converter = $this->explorer->getTypeConverter();
242242
foreach ($this->result->getColumnsInfo() as $meta) {
243243
$res[$meta['name']] = isset($meta['nativeType'])
244244
? $engine->chooseColumnConverter($meta, $converter)

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)