3737 */
3838
3939use CodeIgniter \DatabaseException ;
40+ use CodeIgniter \Hooks \Hooks ;
4041
4142/**
4243 * Class BaseConnection
@@ -188,23 +189,15 @@ abstract class BaseConnection implements ConnectionInterface
188189 */
189190 public $ failover = [];
190191
191- /**
192- * Whether to keep an in-memory history of queries
193- * for debugging and timeline purposes.
194- *
195- * @var bool
196- */
197- public $ saveQueries = false ;
198-
199192 //--------------------------------------------------------------------
200193
201194 /**
202- * Array of query objects that have executed
195+ * The last query object that was executed
203196 * on this connection.
204197 *
205198 * @var array
206199 */
207- protected $ queries = [] ;
200+ protected $ lastQuery ;
208201
209202 /**
210203 * Connection ID
@@ -495,40 +488,6 @@ abstract public function getVersion();
495488
496489 //--------------------------------------------------------------------
497490
498- /**
499- * Specifies whether this connection should keep queries objects or not.
500- *
501- * @param bool $save
502- */
503- public function saveQueries ($ save = false )
504- {
505- $ this ->saveQueries = $ save ;
506-
507- return $ this ;
508- }
509-
510- //--------------------------------------------------------------------
511-
512- /**
513- * Stores a new query with the object. This is primarily used by
514- * the prepared queries.
515- *
516- * @param \CodeIgniter\Database\Query $query
517- *
518- * @return $this
519- */
520- public function addQuery (Query $ query )
521- {
522- if ($ this ->saveQueries )
523- {
524- $ this ->queries [] = $ query ;
525- }
526-
527- return $ this ;
528- }
529-
530- //--------------------------------------------------------------------
531-
532491 /**
533492 * Executes the query against the database.
534493 *
@@ -574,7 +533,9 @@ public function query(string $sql, $binds = null, $queryClass = 'CodeIgniter\\Da
574533
575534 $ startTime = microtime (true );
576535
577-
536+ // Always save the last query so we can use
537+ // the getLastQuery() method.
538+ $ this ->lastQuery = $ query ;
578539
579540 // Run the query for real
580541 if (! $ this ->pretend && false === ($ this ->resultID = $ this ->simpleQuery ($ query ->getQuery ())))
@@ -583,19 +544,21 @@ public function query(string $sql, $binds = null, $queryClass = 'CodeIgniter\\Da
583544
584545 // @todo deal with errors
585546
586- if ($ this -> saveQueries && ! $ this ->pretend )
547+ if (! $ this ->pretend )
587548 {
588- $ this ->queries [] = $ query ;
549+ // Let others do something with this query.
550+ Hooks::trigger ('DBQuery ' , $ query );
589551 }
590552
591553 return new $ resultClass ($ this ->connID , $ this ->resultID );
592554 }
593555
594556 $ query ->setDuration ($ startTime );
595557
596- if ($ this -> saveQueries && ! $ this ->pretend )
558+ if (! $ this ->pretend )
597559 {
598- $ this ->queries [] = $ query ;
560+ // Let others do somethign with this query
561+ Hooks::trigger ('DBQuery ' , $ query );
599562 }
600563
601564 // If $pretend is true, then we just want to return
@@ -691,39 +654,14 @@ public function prepare(\Closure $func, array $options = [])
691654
692655 //--------------------------------------------------------------------
693656
694- /**
695- * Returns an array containing all of the
696- *
697- * @return array
698- */
699- public function getQueries (): array
700- {
701- return $ this ->queries ;
702- }
703-
704- //--------------------------------------------------------------------
705-
706- /**
707- * Returns the total number of queries that have been performed
708- * on this connection.
709- *
710- * @return mixed
711- */
712- public function getQueryCount ()
713- {
714- return count ($ this ->queries );
715- }
716-
717- //--------------------------------------------------------------------
718-
719657 /**
720658 * Returns the last query's statement object.
721659 *
722660 * @return mixed
723661 */
724662 public function getLastQuery ()
725663 {
726- return end ( $ this ->queries ) ;
664+ return $ this ->lastQuery ;
727665 }
728666
729667 //--------------------------------------------------------------------
@@ -735,7 +673,7 @@ public function getLastQuery()
735673 */
736674 public function showLastQuery ()
737675 {
738- return (string )end ( $ this ->queries ) ;
676+ return (string )$ this ->lastQuery ;
739677 }
740678
741679 //--------------------------------------------------------------------
0 commit comments