Skip to content

Commit 0c81232

Browse files
committed
Connection: calling query() moved here from ResultSet (BC break)
WIP: begin/commit/rollback is not sent to onQuery
1 parent 46682c3 commit 0c81232

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

src/Bridges/DatabaseTracy/ConnectionPanel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Nette;
1313
use Nette\Database\Connection;
1414
use Nette\Database\Helpers;
15+
use Nette\Database\ResultSet;
1516
use Tracy;
1617

1718

@@ -155,7 +156,7 @@ public function getPanel(): ?string
155156
$cmd = is_string($this->explain)
156157
? $this->explain
157158
: 'EXPLAIN';
158-
$explain = (new Nette\Database\ResultSet($connection, "$cmd $sql", $params))->fetchAll();
159+
$explain = (new ResultSet($connection, $connection->getConnectionDriver()->query("$cmd $sql", $params)))->fetchAll();
159160
} catch (\PDOException) {
160161
}
161162
}

src/Database/Connection.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function beginTransaction(): void
156156
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
157157
}
158158

159-
$this->query('::beginTransaction');
159+
$this->getConnectionDriver()->beginTransaction();
160160
}
161161

162162

@@ -166,7 +166,7 @@ public function commit(): void
166166
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
167167
}
168168

169-
$this->query('::commit');
169+
$this->getConnectionDriver()->commit();
170170
}
171171

172172

@@ -176,7 +176,7 @@ public function rollBack(): void
176176
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
177177
}
178178

179-
$this->query('::rollBack');
179+
$this->getConnectionDriver()->rollBack();
180180
}
181181

182182

@@ -215,14 +215,17 @@ public function query(#[Language('SQL')] string $sql, #[Language('GenericSQL')]
215215
{
216216
[$this->sql, $params] = $this->preprocess($sql, ...$params);
217217
try {
218-
$result = new ResultSet($this, $this->sql, $params, $this->rowNormalizer);
218+
$time = microtime(true);
219+
$result = $this->connection->query($this->sql, $params);
220+
$time = microtime(true) - $time;
221+
$resultSet = new ResultSet($this, $result, new SqlLiteral($this->sql, $params), $this->rowNormalizer, $time);
219222
} catch (DriverException $e) {
220223
Arrays::invoke($this->onQuery, $this, $e);
221224
throw $e;
222225
}
223226

224-
Arrays::invoke($this->onQuery, $this, $result);
225-
return $result;
227+
Arrays::invoke($this->onQuery, $this, $resultSet);
228+
return $resultSet;
226229
}
227230

228231

src/Database/ResultSet.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,24 @@
1717
*/
1818
class ResultSet implements \Iterator
1919
{
20-
private readonly ?Drivers\Result $result;
21-
2220
/** @var callable(array, ResultSet): array */
2321
private readonly mixed $normalizer;
2422
private Row|false|null $lastRow = null;
2523
private int $lastRowKey = -1;
2624

2725
/** @var Row[] */
2826
private array $rows;
29-
private float $time;
3027
private array $converters;
3128

3229

3330
public function __construct(
3431
private readonly Connection $connection,
35-
private readonly string $queryString,
36-
private readonly array $params,
32+
private readonly Drivers\Result $result,
33+
private readonly ?SqlLiteral $query = null,
3734
?callable $normalizer = null,
35+
private ?float $time = null,
3836
) {
39-
$time = microtime(true);
4037
$this->normalizer = $normalizer;
41-
if (str_starts_with($queryString, '::')) {
42-
$connection->getConnectionDriver()->{substr($queryString, 2)}();
43-
} else {
44-
$this->result = $connection->getConnectionDriver()->query($queryString, $params);
45-
}
46-
$this->time = microtime(true) - $time;
4738
}
4839

4940

@@ -55,27 +46,27 @@ public function getConnection(): Connection
5546
}
5647

5748

58-
public function getQueryString(): string
49+
public function getQueryString(): ?string
5950
{
60-
return $this->queryString;
51+
return $this->query?->getSql();
6152
}
6253

6354

6455
public function getParameters(): array
6556
{
66-
return $this->params;
57+
return $this->query?->getParameters();
6758
}
6859

6960

7061
public function getColumnCount(): ?int
7162
{
72-
return $this->result?->getColumnCount();
63+
return $this->result->getColumnCount();
7364
}
7465

7566

7667
public function getRowCount(): ?int
7768
{
78-
return $this->result?->getRowCount();
69+
return $this->result->getRowCount();
7970
}
8071

8172

@@ -150,7 +141,7 @@ public function valid(): bool
150141
*/
151142
public function fetch(): ?Row
152143
{
153-
$data = $this->result?->fetch();
144+
$data = $this->result->fetch();
154145
if ($data === null) {
155146
return null;
156147

@@ -174,7 +165,7 @@ public function fetch(): ?Row
174165
/** @internal */
175166
public function fetchAssociative(): ?array
176167
{
177-
return $this->result?->fetch();
168+
return $this->result->fetch();
178169
}
179170

180171

0 commit comments

Comments
 (0)