Commit 16d88ae2 authored by Alexander Butenko's avatar Alexander Butenko

Merge pull request #284 from avbdr/master

fixes
parents 20db85d5 bd30f30d
......@@ -805,57 +805,6 @@ class MysqliDb
return true;
}
/**
* Helper function to add variables into the query statement
*
* @param Array Variable with values
*/
protected function _buildDuplicate($tableData)
{
if (is_array($this->_updateColumns) && !empty($this->_updateColumns)) {
$this->_query .= " on duplicate key update ";
if ($this->_lastInsertId) {
$this->_lastQuery .= $this->_lastInsertId."=LAST_INSERT_ID(".$this->_lastInsertId."),";
$this->_lastInsertId = null;
}
foreach ($this->_updateColumns as $column) {
$this->_query .= "`" . $column . "` = ";
// Simple value
if (!is_array ($tableData[$column])) {
$this->_bindParam($tableData[$column]);
$this->_query .= '?, ';
continue;
}
// Function value
$arr = $tableData[$column];
$key = key($arr);
$val = $arr[$key];
switch ($key) {
case '[I]':
$this->_query .= $column . $val . ", ";
break;
case '[F]':
$this->_query .= $val[0] . ", ";
if (!empty ($val[1]))
$this->_bindParams ($val[1]);
break;
case '[N]':
if ($val == null)
$this->_query .= "!" . $column . ", ";
else
$this->_query .= "!" . $val . ", ";
break;
default:
die ("Wrong operation");
}
}
$this->_query = rtrim($this->_query, ', ');
}
}
/**
* Abstraction method that will compile the WHERE statement,
* any passed update data, and the desired rows.
......@@ -870,12 +819,12 @@ class MysqliDb
protected function _buildQuery($numRows = null, $tableData = null)
{
$this->_buildJoin();
$this->_buildTableData ($tableData);
$this->_buildInsertQuery ($tableData);
$this->_buildWhere();
$this->_buildGroupBy();
$this->_buildOrderBy();
$this->_buildLimit ($numRows);
$this->_buildDuplicate($tableData);
$this->_buildOnDuplicate($tableData);
$this->_lastQuery = $this->replacePlaceHolders ($this->_query, $this->_bindParams);
......@@ -996,20 +945,9 @@ class MysqliDb
}
}
/**
* Abstraction method that will build an INSERT or UPDATE part of the query
*/
protected function _buildTableData ($tableData) {
if (!is_array ($tableData))
return;
$isInsert = preg_match ('/^[INSERT|REPLACE]/', $this->_query);
if ($isInsert)
$this->_query .= ' (`' . implode(array_keys($tableData), '`, `') . '`) VALUES (';
else
$this->_query .= " SET ";
foreach ($tableData as $column => $value) {
public function _buildDataPairs ($tableData, $tableColumns, $isInsert) {
foreach ($tableColumns as $column) {
$value = $tableData[$column];
if (!$isInsert)
$this->_query .= "`" . $column . "` = ";
......@@ -1021,7 +959,7 @@ class MysqliDb
// Simple value
if (!is_array ($value)) {
$this->_bindParam ($value);
$this->_bindParam($value);
$this->_query .= '?, ';
continue;
}
......@@ -1048,7 +986,41 @@ class MysqliDb
die ("Wrong operation");
}
}
$this->_query = rtrim ($this->_query, ', ');
$this->_query = rtrim($this->_query, ', ');
}
/**
* Helper function to add variables into the query statement
*
* @param Array Variable with values
*/
protected function _buildOnDuplicate($tableData)
{
if (is_array($this->_updateColumns) && !empty($this->_updateColumns)) {
$this->_query .= " on duplicate key update ";
if ($this->_lastInsertId)
$this->_query .= $this->_lastInsertId . "=LAST_INSERT_ID (".$this->_lastInsertId."), ";
$this->_buildDataPairs ($tableData, $this->_updateColumns, false);
}
}
/**
* Abstraction method that will build an INSERT or UPDATE part of the query
*/
protected function _buildInsertQuery ($tableData) {
if (!is_array ($tableData))
return;
$isInsert = preg_match ('/^[INSERT|REPLACE]/', $this->_query);
$dataColumns = array_keys ($tableData);
if ($isInsert)
$this->_query .= ' (`' . implode ($dataColumns, '`, `') . '`) VALUES (';
else
$this->_query .= " SET ";
$this->_buildDataPairs ($tableData, $dataColumns, $isInsert);
if ($isInsert)
$this->_query .= ')';
}
......
......@@ -102,6 +102,7 @@ $data = Array (
function createTable ($name, $data) {
global $db;
//$q = "CREATE TABLE $name (id INT(9) UNSIGNED PRIMARY KEY NOT NULL";
$db->rawQuery("DROP TABLE IF EXISTS $name");
$q = "CREATE TABLE $name (id INT(9) UNSIGNED PRIMARY KEY AUTO_INCREMENT";
foreach ($data as $k => $v) {
$q .= ", $k $v";
......@@ -192,6 +193,15 @@ $updateColumns = Array ("updatedAt");
$insertLastId = "id";
$db->onDuplicate($updateColumns, "id");
$db->insert("users", $user);
$nUser = $db->where('login','user3')->get('users');
if ($db->count != 1) {
echo "onDuplicate update failed. ";
exit;
}
if ($nUser[0]['createdAt'] == $nUser[0]['updatedAt']) {
echo "onDuplicate2 update failed. ";
exit;
}
// order by field
$db->orderBy("login","asc", Array ("user3","user2","user1"));
......
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