Skip to content

Commit a33203f

Browse files
committed
Connection: calling query() moved here from ResultSet (BC break)
WIP: begin/commit/rollback is not sent to onQuery
1 parent 37e3593 commit a33203f

3 files changed

Lines changed: 21 additions & 27 deletions

File tree

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->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
@@ -146,7 +146,7 @@ public function beginTransaction(): void
146146
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
147147
}
148148

149-
$this->query('::beginTransaction');
149+
$this->getConnectionDriver()->beginTransaction();
150150
}
151151

152152

@@ -156,7 +156,7 @@ public function commit(): void
156156
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
157157
}
158158

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

162162

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

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

172172

@@ -205,14 +205,17 @@ public function query(#[Language('SQL')] string $sql, #[Language('GenericSQL')]
205205
{
206206
[$this->sql, $params] = $this->preprocess($sql, ...$params);
207207
try {
208-
$result = new ResultSet($this, $this->sql, $params, $this->rowNormalizer);
208+
$time = microtime(true);
209+
$result = $this->connection->query($this->sql, $params);
210+
$time = microtime(true) - $time;
211+
$resultSet = new ResultSet($result, new SqlLiteral($this->sql, $params), $this->rowNormalizer, $time);
209212
} catch (DriverException $e) {
210213
Arrays::invoke($this->onQuery, $this, $e);
211214
throw $e;
212215
}
213216

214-
Arrays::invoke($this->onQuery, $this, $result);
215-
return $result;
217+
Arrays::invoke($this->onQuery, $this, $resultSet);
218+
return $resultSet;
216219
}
217220

218221

src/Database/ResultSet.php

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,23 @@
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 $types;
3128

3229

3330
public function __construct(
34-
private readonly Connection $connection,
35-
private readonly string $queryString,
36-
private readonly array $params,
31+
private readonly Drivers\Result $result,
32+
private readonly ?SqlLiteral $query = null,
3733
?callable $normalizer = null,
34+
private ?float $time = null,
3835
) {
39-
$time = microtime(true);
4036
$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;
4737
}
4838

4939

@@ -54,27 +44,27 @@ public function getConnection(): Connection
5444
}
5545

5646

57-
public function getQueryString(): string
47+
public function getQueryString(): ?string
5848
{
59-
return $this->queryString;
49+
return $this->query?->getSql();
6050
}
6151

6252

6353
public function getParameters(): array
6454
{
65-
return $this->params;
55+
return $this->query?->getParameters();
6656
}
6757

6858

6959
public function getColumnCount(): ?int
7060
{
71-
return $this->result?->getColumnCount();
61+
return $this->result->getColumnCount();
7262
}
7363

7464

7565
public function getRowCount(): ?int
7666
{
77-
return $this->result?->getRowCount();
67+
return $this->result->getRowCount();
7868
}
7969

8070

@@ -149,7 +139,7 @@ public function valid(): bool
149139
*/
150140
public function fetch(): ?Row
151141
{
152-
$data = $this->result?->fetch();
142+
$data = $this->result->fetch();
153143
if ($data === null) {
154144
return null;
155145

@@ -173,7 +163,7 @@ public function fetch(): ?Row
173163
/** @internal */
174164
public function fetchAssociative(): ?array
175165
{
176-
return $this->result?->fetch();
166+
return $this->result->fetch();
177167
}
178168

179169

0 commit comments

Comments
 (0)