@@ -43,6 +43,12 @@ class MysqliDb
43
43
* @var string
44
44
*/
45
45
protected $ _lastQuery ;
46
+ /**
47
+ * The SQL query options required after SELECT, INSERT, UPDATE or DELETE
48
+ *
49
+ * @var string
50
+ */
51
+ protected $ _queryOptions = array ();
46
52
/**
47
53
* An array that holds where joins
48
54
*
@@ -81,7 +87,6 @@ class MysqliDb
81
87
* @var string
82
88
*/
83
89
public $ totalCount = 0 ;
84
- protected $ fetchTotalCount = false ;
85
90
/**
86
91
* Variable which holds last statement error
87
92
*
@@ -107,6 +112,15 @@ class MysqliDb
107
112
*/
108
113
protected $ isSubQuery = false ;
109
114
115
+ /**
116
+ * Variables for query execution tracing
117
+ *
118
+ */
119
+ protected $ traceStartQ ;
120
+ protected $ traceEnabled ;
121
+ protected $ traceStripPrefix ;
122
+ public $ trace = array ();
123
+
110
124
/**
111
125
* @param string $host
112
126
* @param string $username
@@ -187,13 +201,16 @@ public static function getInstance()
187
201
*/
188
202
protected function reset ()
189
203
{
204
+ if ($ this ->traceEnabled )
205
+ $ this ->trace [] = array ($ this ->_lastQuery , (microtime (true ) - $ this ->traceStartQ ) , $ this ->_traceGetCaller ());
206
+
190
207
$ this ->_where = array ();
191
208
$ this ->_join = array ();
192
209
$ this ->_orderBy = array ();
193
210
$ this ->_groupBy = array ();
194
211
$ this ->_bindParams = array ('' ); // Create the empty 0 index
195
212
$ this ->_query = null ;
196
- $ this ->count = 0 ;
213
+ $ this ->_queryOptions = array () ;
197
214
}
198
215
199
216
/**
@@ -238,9 +255,10 @@ public function rawQuery ($query, $bindParams = null, $sanitize = true)
238
255
$ stmt ->execute ();
239
256
$ this ->_stmtError = $ stmt ->error ;
240
257
$ this ->_lastQuery = $ this ->replacePlaceHolders ($ this ->_query , $ params );
258
+ $ res = $ this ->_dynamicBindResults ($ stmt );
241
259
$ this ->reset ();
242
260
243
- return $ this -> _dynamicBindResults ( $ stmt ) ;
261
+ return $ res ;
244
262
}
245
263
246
264
/**
@@ -256,9 +274,37 @@ public function query($query, $numRows = null)
256
274
$ stmt = $ this ->_buildQuery ($ numRows );
257
275
$ stmt ->execute ();
258
276
$ this ->_stmtError = $ stmt ->error ;
277
+ $ res = $ this ->_dynamicBindResults ($ stmt );
259
278
$ this ->reset ();
260
279
261
- return $ this ->_dynamicBindResults ($ stmt );
280
+ return $ res ;
281
+ }
282
+
283
+ /**
284
+ * This method allows you to specify multiple (method chaining optional) options for SQL queries.
285
+ *
286
+ * @uses $MySqliDb->setQueryOption('name');
287
+ *
288
+ * @param string/array $options The optons name of the query.
289
+ *
290
+ * @return MysqliDb
291
+ */
292
+ public function setQueryOption ($ options ) {
293
+ $ allowedOptions = Array ('ALL ' ,'DISTINCT ' ,'DISTINCTROW ' ,'HIGH_PRIORITY ' ,'STRAIGHT_JOIN ' ,'SQL_SMALL_RESULT ' ,
294
+ 'SQL_BIG_RESULT ' ,'SQL_BUFFER_RESULT ' ,'SQL_CACHE ' ,'SQL_NO_CACHE ' , 'SQL_CALC_FOUND_ROWS ' ,
295
+ 'LOW_PRIORITY ' ,'IGNORE ' ,'QUICK ' );
296
+ if (!is_array ($ options ))
297
+ $ options = Array ($ options );
298
+
299
+ foreach ($ options as $ option ) {
300
+ $ option = strtoupper ($ option );
301
+ if (!in_array ($ option , $ allowedOptions ))
302
+ die ('Wrong query option: ' .$ option );
303
+
304
+ $ this ->_queryOptions [] = $ option ;
305
+ }
306
+
307
+ return $ this ;
262
308
}
263
309
264
310
/**
@@ -267,7 +313,7 @@ public function query($query, $numRows = null)
267
313
* @return MysqliDb
268
314
*/
269
315
public function withTotalCount () {
270
- $ this ->fetchTotalCount = true ;
316
+ $ this ->setQueryOption ( ' SQL_CALC_FOUND_ROWS ' ) ;
271
317
return $ this ;
272
318
}
273
319
@@ -284,19 +330,20 @@ public function get($tableName, $numRows = null, $columns = '*')
284
330
if (empty ($ columns ))
285
331
$ columns = '* ' ;
286
332
287
- $ this ->_query = $ this ->fetchTotalCount == true ? 'SELECT SQL_CALC_FOUND_ROWS ' : 'SELECT ' ;
288
333
$ column = is_array ($ columns ) ? implode (', ' , $ columns ) : $ columns ;
289
- $ this ->_query .= "$ column FROM " .self ::$ _prefix . $ tableName ;
334
+ $ this ->_query = 'SELECT ' . implode (' ' , $ this ->_queryOptions ) . ' ' .
335
+ $ column . " FROM " .self ::$ _prefix . $ tableName ;
290
336
$ stmt = $ this ->_buildQuery ($ numRows );
291
337
292
338
if ($ this ->isSubQuery )
293
339
return $ this ;
294
340
295
341
$ stmt ->execute ();
296
342
$ this ->_stmtError = $ stmt ->error ;
343
+ $ res = $ this ->_dynamicBindResults ($ stmt );
297
344
$ this ->reset ();
298
345
299
- return $ this -> _dynamicBindResults ( $ stmt ) ;
346
+ return $ res ;
300
347
}
301
348
302
349
/**
@@ -724,9 +771,8 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
724
771
if ($ this ->_mysqli ->more_results ())
725
772
$ this ->_mysqli ->next_result ();
726
773
727
- if ($ this ->fetchTotalCount === true ) {
728
- $ this ->fetchTotalCount = false ;
729
- $ stmt = $ this ->_mysqli ->query ('SELECT FOUND_ROWS(); ' );
774
+ if (in_array ('SQL_CALC_FOUND_ROWS ' , $ this ->_queryOptions )) {
775
+ $ stmt = $ this ->_mysqli ->query ('SELECT FOUND_ROWS() ' );
730
776
$ totalCount = $ stmt ->fetch_row ();
731
777
$ this ->totalCount = $ totalCount [0 ];
732
778
}
@@ -934,6 +980,9 @@ protected function _prepareQuery()
934
980
if (!$ stmt = $ this ->_mysqli ->prepare ($ this ->_query )) {
935
981
trigger_error ("Problem preparing query ( $ this ->_query ) " . $ this ->_mysqli ->error , E_USER_ERROR );
936
982
}
983
+ if ($ this ->traceEnabled )
984
+ $ this ->traceStartQ = microtime (true );
985
+
937
986
return $ stmt ;
938
987
}
939
988
@@ -1170,5 +1219,31 @@ public function _transaction_status_check () {
1170
1219
return ;
1171
1220
$ this ->rollback ();
1172
1221
}
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
+ }
1173
1248
} // END class
1174
1249
?>
0 commit comments