Commit e4b060d6 authored by Alexander Butenko's avatar Alexander Butenko

Support for FOR UPDATE and LOCK IN SHARE MODE in selects

parent ab1f535b
...@@ -139,6 +139,19 @@ class MysqliDb ...@@ -139,6 +139,19 @@ class MysqliDb
*/ */
protected $_nestJoin = false; protected $_nestJoin = false;
private $_tableName = ''; private $_tableName = '';
/**
* FOR UPDATE flag
* @var boolean
*/
protected $_forUpdate = false;
/**
* LOCK IN SHARE MODE flag
* @var boolean
*/
protected $_lockInShareMode = false;
/** /**
* Variables for query execution tracing * Variables for query execution tracing
* *
...@@ -247,6 +260,8 @@ class MysqliDb ...@@ -247,6 +260,8 @@ class MysqliDb
$this->_queryOptions = array(); $this->_queryOptions = array();
$this->returnType = 'Array'; $this->returnType = 'Array';
$this->_nestJoin = false; $this->_nestJoin = false;
$this->_forUpdate = false;
$this->_lockInShareMode = false;
$this->_tableName = ''; $this->_tableName = '';
$this->_lastInsertId = null; $this->_lastInsertId = null;
$this->_updateColumns = null; $this->_updateColumns = null;
...@@ -401,7 +416,7 @@ class MysqliDb ...@@ -401,7 +416,7 @@ class MysqliDb
public function setQueryOption ($options) { public function setQueryOption ($options) {
$allowedOptions = Array ('ALL','DISTINCT','DISTINCTROW','HIGH_PRIORITY','STRAIGHT_JOIN','SQL_SMALL_RESULT', $allowedOptions = Array ('ALL','DISTINCT','DISTINCTROW','HIGH_PRIORITY','STRAIGHT_JOIN','SQL_SMALL_RESULT',
'SQL_BIG_RESULT','SQL_BUFFER_RESULT','SQL_CACHE','SQL_NO_CACHE', 'SQL_CALC_FOUND_ROWS', 'SQL_BIG_RESULT','SQL_BUFFER_RESULT','SQL_CACHE','SQL_NO_CACHE', 'SQL_CALC_FOUND_ROWS',
'LOW_PRIORITY','IGNORE','QUICK', 'MYSQLI_NESTJOIN'); 'LOW_PRIORITY','IGNORE','QUICK', 'MYSQLI_NESTJOIN', 'FOR UPDATE', 'LOCK IN SHARE MODE');
if (!is_array ($options)) if (!is_array ($options))
$options = Array ($options); $options = Array ($options);
...@@ -412,6 +427,10 @@ class MysqliDb ...@@ -412,6 +427,10 @@ class MysqliDb
if ($option == 'MYSQLI_NESTJOIN') if ($option == 'MYSQLI_NESTJOIN')
$this->_nestJoin = true; $this->_nestJoin = true;
else if ($option == 'MYSQLI_FOR_UPDATE')
$this->_forUpdate = true;
else if ($option == 'MYSQLI_LOCK_IN_SHARE_MODE')
$this->_lockInShareMode = true;
else else
$this->_queryOptions[] = $option; $this->_queryOptions[] = $option;
} }
...@@ -884,6 +903,10 @@ class MysqliDb ...@@ -884,6 +903,10 @@ class MysqliDb
$this->_buildOrderBy(); $this->_buildOrderBy();
$this->_buildLimit ($numRows); $this->_buildLimit ($numRows);
$this->_buildOnDuplicate($tableData); $this->_buildOnDuplicate($tableData);
if ($this->_forUpdate)
$this->_query .= ' FOR UPDATE';
if ($this->_lockInShareMode)
$this->_query .= ' LOCK IN SHARE MODE';
$this->_lastQuery = $this->replacePlaceHolders ($this->_query, $this->_bindParams); $this->_lastQuery = $this->replacePlaceHolders ($this->_query, $this->_bindParams);
......
...@@ -351,23 +351,25 @@ echo "Showing {$count} from {$db->totalCount}"; ...@@ -351,23 +351,25 @@ echo "Showing {$count} from {$db->totalCount}";
``` ```
### Query Keywords ### Query Keywords
To add LOW PRIORITY | DELAYED | HIGH PRIORITY | IGNORE and the rest of the mysql keywords to INSERT (), REPLACE (), GET (), UPDATE (), DELETE() method: 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 ():
```php ```php
$db->setQueryOption('LOW_PRIORITY'); $db->setQueryOption ('LOW_PRIORITY')->insert ($table, $param);
$db->insert ($table, $param);
// GIVES: INSERT LOW_PRIORITY INTO table ... // GIVES: INSERT LOW_PRIORITY INTO table ...
``` ```
```php
$db->setQueryOption ('FOR UPDATE')->get ('users');
// GIVES: SELECT * FROM USERS FOR UPDATE;
```
Also you can use an array of keywords: Also you can use an array of keywords:
```php ```php
$db->setQueryOption(Array('LOW_PRIORITY', 'IGNORE')); $db->setQueryOption (Array('LOW_PRIORITY', 'IGNORE'))->insert ($table,$param);
$db->insert ($table,$param);
// GIVES: INSERT LOW_PRIORITY IGNORE INTO table ... // GIVES: INSERT LOW_PRIORITY IGNORE INTO table ...
``` ```
Same way keywords could be used in SELECT queries as well: Same way keywords could be used in SELECT queries as well:
```php ```php
$db->setQueryOption('SQL_NO_CACHE'); $db->setQueryOption ('SQL_NO_CACHE');
$db->get("users"); $db->get("users");
// GIVES: SELECT SQL_NO_CACHE * FROM USERS; // GIVES: SELECT SQL_NO_CACHE * FROM USERS;
``` ```
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment