@@ -112,6 +112,15 @@ class MysqliDb
112112 */
113113 protected $ isSubQuery = false ;
114114
115+ /**
116+ * Variables for query execution tracing
117+ *
118+ */
119+ protected $ traceStartQ ;
120+ protected $ traceEnabled ;
121+ protected $ traceStripPrefix ;
122+ public $ trace = array ();
123+
115124 /**
116125 * @param string $host
117126 * @param string $username
@@ -192,6 +201,9 @@ public static function getInstance()
192201 */
193202 protected function reset ()
194203 {
204+ if ($ this ->traceEnabled )
205+ $ this ->trace [] = array ($ this ->_lastQuery , (microtime (true ) - $ this ->traceStartQ ) , $ this ->_traceGetCaller ());
206+
195207 $ this ->_where = array ();
196208 $ this ->_join = array ();
197209 $ this ->_orderBy = array ();
@@ -968,6 +980,9 @@ protected function _prepareQuery()
968980 if (!$ stmt = $ this ->_mysqli ->prepare ($ this ->_query )) {
969981 trigger_error ("Problem preparing query ( $ this ->_query ) " . $ this ->_mysqli ->error , E_USER_ERROR );
970982 }
983+ if ($ this ->traceEnabled )
984+ $ this ->traceStartQ = microtime (true );
985+
971986 return $ stmt ;
972987 }
973988
@@ -1204,5 +1219,31 @@ public function _transaction_status_check () {
12041219 return ;
12051220 $ this ->rollback ();
12061221 }
1222+
1223+ /**
1224+ * Query exection time tracking switch
1225+ *
1226+ * @param bool $enabled Enable execution time tracking
1227+ * @param string $stripPrefix Prefix to strip from the path in exec log
1228+ **/
1229+ public function setTrace ($ enabled , $ stripPrefix = null ) {
1230+ $ this ->traceEnabled = $ enabled ;
1231+ $ this ->traceStripPrefix = $ stripPrefix ;
1232+ return $ this ;
1233+ }
1234+ /**
1235+ * Get where and what function was called for query stored in MysqliDB->trace
1236+ *
1237+ * @return string with information
1238+ */
1239+ private function _traceGetCaller () {
1240+ $ dd = debug_backtrace ();
1241+ $ caller = next ($ dd );
1242+ while (isset ($ caller ) && $ caller ["file " ] == __FILE__ )
1243+ $ caller = next ($ dd );
1244+
1245+ return __CLASS__ . "-> " . $ caller ["function " ] . "() >> file \"" .
1246+ str_replace ($ this ->traceStripPrefix , '' , $ caller ["file " ] ) . "\" line # " . $ caller ["line " ] . " " ;
1247+ }
12071248} // END class
12081249?>
0 commit comments