Skip to content

Commit 7429e5a

Browse files
committed
Added setQueryOption method
1 parent a204875 commit 7429e5a

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

MysqliDb.php

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ class MysqliDb
4343
* @var string
4444
*/
4545
protected $_lastQuery;
46+
/**
47+
* The SQL query options required after SELECT, INSERT, UPDATE or DELETE
48+
*
49+
* @var string
50+
*/
51+
protected $_queryOptions = array();
4652
/**
4753
* An array that holds where joins
4854
*
@@ -81,7 +87,6 @@ class MysqliDb
8187
* @var string
8288
*/
8389
public $totalCount = 0;
84-
protected $fetchTotalCount = false;
8590
/**
8691
* Variable which holds last statement error
8792
*
@@ -193,7 +198,7 @@ protected function reset()
193198
$this->_groupBy = array();
194199
$this->_bindParams = array(''); // Create the empty 0 index
195200
$this->_query = null;
196-
$this->count = 0;
201+
$this->_queryOptions = array();
197202
}
198203

199204
/**
@@ -238,9 +243,10 @@ public function rawQuery ($query, $bindParams = null, $sanitize = true)
238243
$stmt->execute();
239244
$this->_stmtError = $stmt->error;
240245
$this->_lastQuery = $this->replacePlaceHolders ($this->_query, $params);
246+
$res = $this->_dynamicBindResults($stmt);
241247
$this->reset();
242248

243-
return $this->_dynamicBindResults($stmt);
249+
return $res;
244250
}
245251

246252
/**
@@ -256,9 +262,28 @@ public function query($query, $numRows = null)
256262
$stmt = $this->_buildQuery($numRows);
257263
$stmt->execute();
258264
$this->_stmtError = $stmt->error;
265+
$res = $this->_dynamicBindResults($stmt);
259266
$this->reset();
260267

261-
return $this->_dynamicBindResults($stmt);
268+
return $res;
269+
}
270+
271+
/**
272+
* This method allows you to specify multiple (method chaining optional) options for SQL queries.
273+
*
274+
* @uses $MySqliDb->setQueryOption('name');
275+
*
276+
* @param string/array $options The optons name of the query.
277+
*
278+
* @return MysqliDb
279+
*/
280+
public function setQueryOption ($options) {
281+
if (is_array ($options))
282+
$this->_queryOptions = array_merge ($this->_queryOptions, $options);
283+
else
284+
$this->_queryOptions[] = $options;
285+
286+
return $this;
262287
}
263288

264289
/**
@@ -267,7 +292,7 @@ public function query($query, $numRows = null)
267292
* @return MysqliDb
268293
*/
269294
public function withTotalCount () {
270-
$this->fetchTotalCount = true;
295+
$this->setQueryOption ('SQL_CALC_FOUND_ROWS');
271296
return $this;
272297
}
273298

@@ -284,19 +309,20 @@ public function get($tableName, $numRows = null, $columns = '*')
284309
if (empty ($columns))
285310
$columns = '*';
286311

287-
$this->_query = $this->fetchTotalCount == true ? 'SELECT SQL_CALC_FOUND_ROWS ' : 'SELECT ';
288312
$column = is_array($columns) ? implode(', ', $columns) : $columns;
289-
$this->_query .= "$column FROM " .self::$_prefix . $tableName;
313+
$this->_query = 'SELECT ' . implode(' ', $this->_queryOptions) . ' ' .
314+
$column . " FROM " .self::$_prefix . $tableName;
290315
$stmt = $this->_buildQuery($numRows);
291316

292317
if ($this->isSubQuery)
293318
return $this;
294319

295320
$stmt->execute();
296321
$this->_stmtError = $stmt->error;
322+
$res = $this->_dynamicBindResults($stmt);
297323
$this->reset();
298324

299-
return $this->_dynamicBindResults($stmt);
325+
return $res;
300326
}
301327

302328
/**
@@ -724,9 +750,8 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
724750
if ($this->_mysqli->more_results())
725751
$this->_mysqli->next_result();
726752

727-
if ($this->fetchTotalCount === true) {
728-
$this->fetchTotalCount = false;
729-
$stmt = $this->_mysqli->query ('SELECT FOUND_ROWS();');
753+
if (in_array ('SQL_CALC_FOUND_ROWS', $this->_queryOptions)) {
754+
$stmt = $this->_mysqli->query ('SELECT FOUND_ROWS()');
730755
$totalCount = $stmt->fetch_row();
731756
$this->totalCount = $totalCount[0];
732757
}

readme.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ MysqliDb -- Simple MySQLi wrapper with prepared statements
77
**[Select Query](#select-query)**
88
**[Delete Query](#delete-query)**
99
**[Generic Query](#generic-query-method)**
10+
**[Query Keywords](#query-keywords)**
1011
**[Raw Query](#raw-query-method)**
1112
**[Where Conditions](#where-method)**
1213
**[Order Conditions](#ordering-method)**
@@ -287,6 +288,28 @@ $users = $db->withTotalCount()->get('users', Array ($offset, $count));
287288
echo "Showing {$count} from {$db->totalCount}";
288289
```
289290

291+
### Query Keywords
292+
To add LOW PRIORITY | DELAYED | HIGH PRIORITY | IGNORE and the rest of mysql keywords to INSERT , SELECT , UPDATE, DELETE query:
293+
```php
294+
$db->setQueryOption('LOW_PRIORITY');
295+
$db->insert($table,$param);
296+
// GIVES: INSERT LOW_PRIORITY INTO table ...
297+
```
298+
299+
Also you can use an array of keywords:
300+
```php
301+
$db->setQueryOption(Array('LOW_PRIORITY', 'IGNORE'));
302+
$db->insert($table,$param);
303+
// GIVES: INSERT LOW_PRIORITY IGNORE INTO table ...
304+
```
305+
306+
Same way keywords could be used in SELECT queries as well:
307+
```php
308+
$db->setQueryOption('SQL_NO_CACHE');
309+
$db->get("users");
310+
// GIVES: SELECT SQL_NO_CACHE * FROM USERS;
311+
```
312+
290313
Optionally you can use method chaining to call where multiple times without referencing your object over an over:
291314

292315
```php
@@ -418,7 +441,6 @@ if($db->has("users")) {
418441
return "Wrong user/password";
419442
}
420443
```
421-
422444
### Helper commands
423445
Reconnect in case mysql connection died
424446
```php

0 commit comments

Comments
 (0)