@@ -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 ;
0 commit comments