Skip to content

Commit

Permalink
Fix #17 Incremental values on UPDATE
Browse files Browse the repository at this point in the history
  • Loading branch information
Yada Khov committed Apr 27, 2017
1 parent 4a5f934 commit 13962f7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
16 changes: 10 additions & 6 deletions src/InsertOnDuplicateKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,20 @@ protected static function getColumnList(array $first)
/**
* Build a value list.
*
* @param array $first
* @param array $updatedColumns
*
* @return string
*/
protected static function buildValuesList(array $first)
protected static function buildValuesList(array $updatedColumns)
{
$out = [];

foreach (array_keys($first) as $key) {
$out[] = sprintf('`%s` = VALUES(`%s`)', $key, $key);
foreach ($updatedColumns as $key => $value) {
if (is_numeric($key)) {
$out[] = sprintf('`%s` = VALUES(`%s`)', $value, $value);
} else {
$out[] = sprintf('%s = %s', $key, $value);
}
}

return implode(', ', $out);
Expand Down Expand Up @@ -240,9 +244,9 @@ protected static function buildInsertOnDuplicateSql(array $data, array $updateCo
$sql .= 'ON DUPLICATE KEY UPDATE ';

if (empty($updateColumns)) {
$sql .= static::buildValuesList($first);
$sql .= static::buildValuesList(array_keys($first));
} else {
$sql .= static::buildValuesList(array_combine($updateColumns, $updateColumns));
$sql .= static::buildValuesList($updateColumns);
}

return $sql;
Expand Down
35 changes: 33 additions & 2 deletions tests/MainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,29 @@ public function testGetColumnList()

public function testBuildValuesList()
{
$data = $this->getDataForInsert();
$value = [
'id',
'email',
'name'
];

$expected = '`id` = VALUES(`id`), `email` = VALUES(`email`), `name` = VALUES(`name`)';

$result = $this->invokeMethod($this->user, 'buildValuesList', [$data[0]]);
$result = $this->invokeMethod($this->user, 'buildValuesList', [$value]);

$this->assertEquals($expected, $result);
}

public function testBuildValuesListAssociativeArray()
{
$value = [
'id' => 'id + 1',
'counter' => 'counter + 2',
];

$expected = 'id = id + 1, counter = counter + 2';

$result = $this->invokeMethod($this->user, 'buildValuesList', [$value]);

$this->assertEquals($expected, $result);
}
Expand Down Expand Up @@ -152,6 +170,19 @@ public function testBuildInsertOnDuplicateSqlMultipleWithUpdateColumn()
$this->assertEquals($expected, $result);
}

public function testUpdatedColumnIsAssociativeArray()
{
$data = $this->getDataForInsert();

$expected = 'INSERT INTO `prefix_test_user_table`(`id`,`email`,`name`) VALUES
(?,?,?), (?,?,?), (?,?,?)
ON DUPLICATE KEY UPDATE counter = counter + 1';

$result = $this->invokeMethod($this->user, 'buildInsertOnDuplicateSql', [$data, ['counter' => 'counter + 1']]);

$this->assertEquals($expected, $result);
}

public function testBuildInsertIgnoreSqlSimple()
{
$data = [
Expand Down

0 comments on commit 13962f7

Please sign in to comment.