Skip to content

Commit 79d8713

Browse files
committed
Connection: calling query() moved here from Result
reverts bc4bebc
1 parent 36154d1 commit 79d8713

File tree

3 files changed

+31
-31
lines changed

3 files changed

+31
-31
lines changed

src/Database/Connection.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public function beginTransaction(): void
201201
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
202202
}
203203

204-
$this->query('::beginTransaction');
204+
$this->logOperation($this->getConnection()->beginTransaction(...), new SqlLiteral('BEGIN TRANSACTION'));
205205
}
206206

207207

@@ -215,7 +215,7 @@ public function commit(): void
215215
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
216216
}
217217

218-
$this->query('::commit');
218+
$this->logOperation($this->getConnection()->commit(...), new SqlLiteral('COMMIT'));
219219
}
220220

221221

@@ -229,7 +229,7 @@ public function rollBack(): void
229229
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
230230
}
231231

232-
$this->query('::rollBack');
232+
$this->logOperation($this->getConnection()->rollBack(...), new SqlLiteral('ROLLBACK'));
233233
}
234234

235235

@@ -270,15 +270,10 @@ public function transaction(callable $callback): mixed
270270
public function query(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): Result
271271
{
272272
[$this->sql, $params] = $this->preprocess($sql, ...$params);
273-
try {
274-
$result = new Result($this, $this->sql, $params);
275-
} catch (DriverException $e) {
276-
Arrays::invoke($this->onQuery, $this, $e);
277-
throw $e;
278-
}
279-
280-
Arrays::invoke($this->onQuery, $this, $result);
281-
return $result;
273+
return $this->logOperation(
274+
fn() => $this->connection->query($this->sql, $params),
275+
new SqlLiteral($this->sql, $params),
276+
);
282277
}
283278

284279

@@ -304,13 +299,31 @@ public function preprocess(string $sql, ...$params): array
304299
}
305300

306301

302+
private function logOperation(\Closure $callback, SqlLiteral $query): Result
303+
{
304+
try {
305+
$time = microtime(true);
306+
$result = $callback();
307+
$time = microtime(true) - $time;
308+
} catch (DriverException $e) {
309+
$e = $this->convertException($e);
310+
Arrays::invoke($this->onQuery, $this, $e);
311+
throw $e;
312+
}
313+
314+
$result = new Result($this, $query->getSql(), $query->getParameters(), $result, $time);
315+
Arrays::invoke($this->onQuery, $this, $result);
316+
return $result;
317+
}
318+
319+
307320
public function getLastQueryString(): ?string
308321
{
309322
return $this->sql;
310323
}
311324

312325

313-
public function convertException(DriverException $e): DriverException
326+
private function convertException(DriverException $e): DriverException
314327
{
315328
$class = $this->getDatabaseEngine()->classifyException($e);
316329
return $class ? $class::from($e) : $e;

src/Database/Result.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,21 @@
1919
*/
2020
class Result implements \Iterator
2121
{
22-
private ?Drivers\Result $result = null;
2322
private Row|false|null $lastRow = null;
2423
private int $lastRowKey = -1;
2524

2625
/** @var Row[] */
2726
private array $rows;
28-
private float $time;
2927
private array $meta;
3028

3129

3230
public function __construct(
3331
private readonly Connection $connection,
3432
private readonly string $queryString,
3533
private readonly array $params,
34+
private readonly ?Drivers\Result $result,
35+
private float $time,
3636
) {
37-
$time = microtime(true);
38-
39-
try {
40-
if (str_starts_with($queryString, '::')) {
41-
$connection->getConnection()->{substr($queryString, 2)}();
42-
} else {
43-
$this->result = $connection->getConnection()->query($queryString, $params);
44-
}
45-
} catch (DriverException $e) {
46-
throw $connection->convertException($e);
47-
}
48-
49-
$this->time = microtime(true) - $time;
5037
}
5138

5239

tests/Database.Tracy/panel.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ <h1 title="sqlite::memory:">Queries: 4, time: %a% ms, foo</h1>
77
<td %a%>%A%</td>
88

99
<td class="nette-DbConnectionPanel-sql nette-DbConnectionPanel-sql-">
10-
<pre class="dump">::beginTransaction</pre>
10+
<pre class="dump">BEGIN TRANSACTION</pre>
1111

1212

1313
<a class="nette-DbConnectionPanel-source" href="%a%" title="%a%Connection.php:%d%" class="tracy-editor">%a%<b>Connection.php</b>:%d%</a>
@@ -40,7 +40,7 @@ <h1 title="sqlite::memory:">Queries: 4, time: %a% ms, foo</h1>
4040
<td %a%>%A%</td>
4141

4242
<td class="nette-DbConnectionPanel-sql nette-DbConnectionPanel-sql-">
43-
<pre class="dump">::commit</pre>
43+
<pre class="dump">COMMIT</pre>
4444

4545

4646
<a class="nette-DbConnectionPanel-source" href="%a%" title="%a%Connection.php:%d%" class="tracy-editor">%a%<b>Connection.php</b>:%d%</a>
@@ -59,7 +59,7 @@ <h1 title="sqlite::memory:">Queries: 4, time: %a% ms, foo</h1>
5959
<pre class="dump"><strong style="color:blue">SELECT</strong></pre>
6060

6161

62-
<a class="nette-DbConnectionPanel-source" href="%a%" title="%a%Result.php:%d%" class="tracy-editor">%a%<b>Result.php</b>:%d%</a>
62+
<a class="nette-DbConnectionPanel-source" href="%a%" title="%a%Connection.php:%d%" class="tracy-editor">%a%<b>Connection.php</b>:%d%</a>
6363
<table class="tracy-collapsed nette-DbConnectionPanel-trace">%A%</table>
6464
</td>
6565

0 commit comments

Comments
 (0)