Skip to content

Commit 75030a1

Browse files
committed
fixed _buildDuplicate() method unable to handle array arguments
1 parent 19abf86 commit 75030a1

File tree

3 files changed

+92
-27
lines changed

3 files changed

+92
-27
lines changed

MysqliDb.php

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,17 @@ class MysqliDb
112112
*/
113113
protected $isSubQuery = false;
114114

115-
/**
116-
* Name of the auto increment column
117-
*
118-
*/
119-
protected $lastInsertId = null;
115+
/**
116+
* Name of the auto increment column
117+
*
118+
*/
119+
protected $_lastInsertId = null;
120120

121-
/**
122-
* Column names for update when using onDuplicate method
123-
*
124-
*/
125-
protected $updateColumns = null;
121+
/**
122+
* Column names for update when using onDuplicate method
123+
*
124+
*/
125+
protected $_updateColumns = null;
126126

127127
/**
128128
* Return type: 'Array' to return results as array, 'Object' as object
@@ -247,6 +247,8 @@ protected function reset()
247247
$this->returnType = 'Array';
248248
$this->_nestJoin = false;
249249
$this->_tableName = '';
250+
$this->_lastInsertId = null;
251+
$this->_updateColumns = null;
250252
}
251253

252254
/**
@@ -562,17 +564,17 @@ public function where($whereProp, $whereValue = 'DBNULL', $operator = '=', $cond
562564
return $this;
563565
}
564566

565-
/**
567+
/**
566568
* This function store update column's name and column name of the
567569
* autoincrement column
568570
*
569571
* @param Array Variable with values
570572
* @param String Variable value
571573
*/
572-
public function onDuplicate($updateColumns, $lastInsertId = null)
574+
public function onDuplicate($_updateColumns, $_lastInsertId = null)
573575
{
574-
$this->lastInsertId = $lastInsertId;
575-
$this->updateColumns = $updateColumns;
576+
$this->_lastInsertId = $_lastInsertId;
577+
$this->_updateColumns = $_updateColumns;
576578
}
577579

578580
/**
@@ -804,28 +806,54 @@ private function _buildInsert ($tableName, $insertData, $operation)
804806
return true;
805807
}
806808

807-
/**
809+
/**
808810
* Helper function to add variables into the query statement
809811
*
810812
* @param Array Variable with values
811813
*/
812814
protected function _buildDuplicate($tableData)
813815
{
814-
if (is_array($this->updateColumns) && !empty($this->updateColumns)) {
816+
if (is_array($this->_updateColumns) && !empty($this->_updateColumns)) {
815817
$this->_query .= " on duplicate key update ";
816-
if ($this->lastInsertId) {
817-
$this->_lastQuery .= $this->lastInsertId."=LAST_INSERT_ID(".$this->lastInsertId."),";
818-
$this->lastInsertId = null;
818+
if ($this->_lastInsertId) {
819+
$this->_lastQuery .= $this->_lastInsertId."=LAST_INSERT_ID(".$this->_lastInsertId."),";
820+
$this->_lastInsertId = null;
819821
}
820-
821-
foreach ($this->updateColumns as $value) {
822-
$this->_bindParam($tableData[$value]);
823-
$this->_query .= "`" . $value . "` = ?, ";
822+
823+
foreach ($this->_updateColumns as $column) {
824+
$this->_query .= "`" . $column . "` = ";
825+
826+
// Simple value
827+
if (!is_array ($tableData[$column])) {
828+
$this->_bindParam($tableData[$column]);
829+
$this->_query .= '?, ';
830+
continue;
831+
}
832+
833+
// Function value
834+
$arr = $tableData[$column];
835+
$key = key($arr);
836+
$val = $arr[$key];
837+
switch ($key) {
838+
case '[I]':
839+
$this->_query .= $column . $val . ", ";
840+
break;
841+
case '[F]':
842+
$this->_query .= $val[0] . ", ";
843+
if (!empty ($val[1]))
844+
$this->_bindParams ($val[1]);
845+
break;
846+
case '[N]':
847+
if ($val == null)
848+
$this->_query .= "!" . $column . ", ";
849+
else
850+
$this->_query .= "!" . $val . ", ";
851+
break;
852+
default:
853+
die ("Wrong operation");
854+
}
824855
}
825-
826856
$this->_query = rtrim($this->_query, ', ');
827-
$this->lastInsertId = null;
828-
$this->updateColumns = null;
829857
}
830858
}
831859

readme.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,20 @@ else
123123
echo 'insert failed: ' . $db->getLastError();
124124
```
125125

126+
Insert with on duplicate key update
127+
```php
128+
$data = Array ("login" => "admin",
129+
"firstName" => "John",
130+
"lastName" => 'Doe',
131+
"createdAt" => $db->now(),
132+
"updatedAt" => $db->now(),
133+
);
134+
$updateColumns = Array ("updateAt");
135+
$lastInsertId = "id";
136+
$db->onDuplicate($updateColumns, $lastInsertId);
137+
$id = $db->insert ('users', $data);
138+
```
139+
126140
### Replace Query
127141
<a href='https://dev.mysql.com/doc/refman/5.0/en/replace.html'>Replace()</a> method implements same API as insert();
128142

tests/mysqliDbTests.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
'lastName' => 'char(10)',
3030
'password' => 'text not null',
3131
'createdAt' => 'datetime',
32+
'updatedAt' => 'datetime',
3233
'expires' => 'datetime',
33-
'loginCount' => 'int(10) default 0'
34+
'loginCount' => 'int(10) default 0',
35+
'unique key' => 'login (login)'
3436
),
3537
'products' => Array (
3638
'customerId' => 'int(10) not null',
@@ -46,6 +48,7 @@
4648
'lastName' => 'Doe',
4749
'password' => $db->func('SHA1(?)',Array ("secretpassword+salt")),
4850
'createdAt' => $db->now(),
51+
'updatedAt' => $db->now(),
4952
'expires' => $db->now('+1Y'),
5053
'loginCount' => $db->inc()
5154
),
@@ -55,6 +58,7 @@
5558
'lastName' => NULL,
5659
'password' => $db->func('SHA1(?)',Array ("secretpassword2+salt")),
5760
'createdAt' => $db->now(),
61+
'updatedAt' => $db->now(),
5862
'expires' => $db->now('+1Y'),
5963
'loginCount' => $db->inc(2)
6064
),
@@ -65,6 +69,7 @@
6569
'lastName' => 'D',
6670
'password' => $db->func('SHA1(?)',Array ("secretpassword2+salt")),
6771
'createdAt' => $db->now(),
72+
'updatedAt' => $db->now(),
6873
'expires' => $db->now('+1Y'),
6974
'loginCount' => $db->inc(3)
7075
)
@@ -136,6 +141,7 @@ function createTable ($name, $data) {
136141
'lastName' => 'Doe',
137142
'password' => 'test',
138143
'createdAt' => $db->now(),
144+
'updatedAt' => $db->now(),
139145
'expires' => $db->now('+1Y'),
140146
'loginCount' => $db->inc()
141147
);
@@ -170,6 +176,23 @@ function createTable ($name, $data) {
170176
exit;
171177
}
172178

179+
// insert with on duplicate key update
180+
$user = Array ('login' => 'user3',
181+
'active' => true,
182+
'customerId' => 11,
183+
'firstName' => 'Pete',
184+
'lastName' => 'D',
185+
'password' => $db->func('SHA1(?)',Array ("secretpassword2+salt")),
186+
'createdAt' => $db->now(),
187+
'updatedAt' => $db->now(),
188+
'expires' => $db->now('+1Y'),
189+
'loginCount' => $db->inc(3)
190+
);
191+
$updateColumns = Array ("updatedAt");
192+
$insertLastId = "id";
193+
$db->onDuplicate($updateColumns, "id");
194+
$db->insert("users", $user);
195+
173196
// order by field
174197
$db->orderBy("login","asc", Array ("user3","user2","user1"));
175198
$login = $db->getValue ("users", "login");

0 commit comments

Comments
 (0)