@@ -200,7 +200,7 @@ public function beginTransaction(): void
200200 throw new \LogicException (__METHOD__ . '() call is forbidden inside a transaction() callback ' );
201201 }
202202
203- $ this ->query ( ' :: beginTransaction' );
203+ $ this ->logOperation ( $ this -> getConnection ()-> beginTransaction (...), new SqlLiteral ( ' BEGIN TRANSACTION ' ) );
204204 }
205205
206206
@@ -214,7 +214,7 @@ public function commit(): void
214214 throw new \LogicException (__METHOD__ . '() call is forbidden inside a transaction() callback ' );
215215 }
216216
217- $ this ->query ( ' :: commit' );
217+ $ this ->logOperation ( $ this -> getConnection ()-> commit (...), new SqlLiteral ( ' COMMIT ' ) );
218218 }
219219
220220
@@ -228,7 +228,7 @@ public function rollBack(): void
228228 throw new \LogicException (__METHOD__ . '() call is forbidden inside a transaction() callback ' );
229229 }
230230
231- $ this ->query ( ' :: rollBack' );
231+ $ this ->logOperation ( $ this -> getConnection ()-> rollBack (...), new SqlLiteral ( ' ROLLBACK ' ) );
232232 }
233233
234234
@@ -269,15 +269,10 @@ public function transaction(callable $callback): mixed
269269 public function query (#[Language('SQL ' )] string $ sql , #[Language('GenericSQL ' )] ...$ params ): Result
270270 {
271271 [$ this ->sql , $ params ] = $ this ->preprocess ($ sql , ...$ params );
272- try {
273- $ result = new Result ($ this , $ this ->sql , $ params );
274- } catch (DriverException $ e ) {
275- Arrays::invoke ($ this ->onQuery , $ this , $ e );
276- throw $ e ;
277- }
278-
279- Arrays::invoke ($ this ->onQuery , $ this , $ result );
280- return $ result ;
272+ return $ this ->logOperation (
273+ fn () => $ this ->connection ->query ($ this ->sql , $ params ),
274+ new SqlLiteral ($ this ->sql , $ params ),
275+ );
281276 }
282277
283278
@@ -303,13 +298,31 @@ public function preprocess(string $sql, ...$params): array
303298 }
304299
305300
301+ private function logOperation (\Closure $ callback , SqlLiteral $ query ): Result
302+ {
303+ try {
304+ $ time = microtime (true );
305+ $ result = $ callback ();
306+ $ time = microtime (true ) - $ time ;
307+ } catch (DriverException $ e ) {
308+ $ e = $ this ->convertException ($ e );
309+ Arrays::invoke ($ this ->onQuery , $ this , $ e );
310+ throw $ e ;
311+ }
312+
313+ $ result = new Result ($ this , $ query ->getSql (), $ query ->getParameters (), $ result , $ time );
314+ Arrays::invoke ($ this ->onQuery , $ this , $ result );
315+ return $ result ;
316+ }
317+
318+
306319 public function getLastQueryString (): ?string
307320 {
308321 return $ this ->sql ;
309322 }
310323
311324
312- public function convertException (DriverException $ e ): DriverException
325+ private function convertException (DriverException $ e ): DriverException
313326 {
314327 $ class = $ this ->getDatabaseEngine ()->classifyException ($ e );
315328 return $ class ? $ class ::from ($ e ) : $ e ;
0 commit comments