Skip to content

Commit dce7544

Browse files
committed
Merge pull request #309 from avbdr/master
fixes
2 parents acaf0d1 + e4b060d commit dce7544

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

MysqliDb.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@ class MysqliDb
139139
*/
140140
protected $_nestJoin = false;
141141
private $_tableName = '';
142+
143+
/**
144+
* FOR UPDATE flag
145+
* @var boolean
146+
*/
147+
protected $_forUpdate = false;
148+
149+
/**
150+
* LOCK IN SHARE MODE flag
151+
* @var boolean
152+
*/
153+
protected $_lockInShareMode = false;
154+
142155
/**
143156
* Variables for query execution tracing
144157
*
@@ -247,6 +260,8 @@ protected function reset()
247260
$this->_queryOptions = array();
248261
$this->returnType = 'Array';
249262
$this->_nestJoin = false;
263+
$this->_forUpdate = false;
264+
$this->_lockInShareMode = false;
250265
$this->_tableName = '';
251266
$this->_lastInsertId = null;
252267
$this->_updateColumns = null;
@@ -401,7 +416,7 @@ public function query($query, $numRows = null)
401416
public function setQueryOption ($options) {
402417
$allowedOptions = Array ('ALL','DISTINCT','DISTINCTROW','HIGH_PRIORITY','STRAIGHT_JOIN','SQL_SMALL_RESULT',
403418
'SQL_BIG_RESULT','SQL_BUFFER_RESULT','SQL_CACHE','SQL_NO_CACHE', 'SQL_CALC_FOUND_ROWS',
404-
'LOW_PRIORITY','IGNORE','QUICK', 'MYSQLI_NESTJOIN');
419+
'LOW_PRIORITY','IGNORE','QUICK', 'MYSQLI_NESTJOIN', 'FOR UPDATE', 'LOCK IN SHARE MODE');
405420
if (!is_array ($options))
406421
$options = Array ($options);
407422

@@ -412,6 +427,10 @@ public function setQueryOption ($options) {
412427

413428
if ($option == 'MYSQLI_NESTJOIN')
414429
$this->_nestJoin = true;
430+
else if ($option == 'MYSQLI_FOR_UPDATE')
431+
$this->_forUpdate = true;
432+
else if ($option == 'MYSQLI_LOCK_IN_SHARE_MODE')
433+
$this->_lockInShareMode = true;
415434
else
416435
$this->_queryOptions[] = $option;
417436
}
@@ -632,6 +651,7 @@ public function onDuplicate($_updateColumns, $_lastInsertId = null)
632651
{
633652
$this->_lastInsertId = $_lastInsertId;
634653
$this->_updateColumns = $_updateColumns;
654+
return $this;
635655
}
636656

637657
/**
@@ -883,6 +903,10 @@ protected function _buildQuery($numRows = null, $tableData = null)
883903
$this->_buildOrderBy();
884904
$this->_buildLimit ($numRows);
885905
$this->_buildOnDuplicate($tableData);
906+
if ($this->_forUpdate)
907+
$this->_query .= ' FOR UPDATE';
908+
if ($this->_lockInShareMode)
909+
$this->_query .= ' LOCK IN SHARE MODE';
886910

887911
$this->_lastQuery = $this->replacePlaceHolders ($this->_query, $this->_bindParams);
888912

readme.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,23 +351,25 @@ echo "Showing {$count} from {$db->totalCount}";
351351
```
352352

353353
### Query Keywords
354-
To add LOW PRIORITY | DELAYED | HIGH PRIORITY | IGNORE and the rest of the mysql keywords to INSERT (), REPLACE (), GET (), UPDATE (), DELETE() method:
354+
To add LOW PRIORITY | DELAYED | HIGH PRIORITY | IGNORE and the rest of the mysql keywords to INSERT (), REPLACE (), GET (), UPDATE (), DELETE() method or FOR UPDATE | LOCK IN SHARE MODE into SELECT ():
355355
```php
356-
$db->setQueryOption('LOW_PRIORITY');
357-
$db->insert ($table, $param);
356+
$db->setQueryOption ('LOW_PRIORITY')->insert ($table, $param);
358357
// GIVES: INSERT LOW_PRIORITY INTO table ...
359358
```
359+
```php
360+
$db->setQueryOption ('FOR UPDATE')->get ('users');
361+
// GIVES: SELECT * FROM USERS FOR UPDATE;
362+
```
360363

361364
Also you can use an array of keywords:
362365
```php
363-
$db->setQueryOption(Array('LOW_PRIORITY', 'IGNORE'));
364-
$db->insert ($table,$param);
366+
$db->setQueryOption (Array('LOW_PRIORITY', 'IGNORE'))->insert ($table,$param);
365367
// GIVES: INSERT LOW_PRIORITY IGNORE INTO table ...
366368
```
367369

368370
Same way keywords could be used in SELECT queries as well:
369371
```php
370-
$db->setQueryOption('SQL_NO_CACHE');
372+
$db->setQueryOption ('SQL_NO_CACHE');
371373
$db->get("users");
372374
// GIVES: SELECT SQL_NO_CACHE * FROM USERS;
373375
```

0 commit comments

Comments
 (0)