Skip to content

Commit 799a4ac

Browse files
committed
feat: enhance ON DUPLICATE KEY UPDATE handling in Insert class and add related tests
1 parent 6de7195 commit 799a4ac

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

src/System/Database/MyQuery/Insert.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,15 @@ protected function builder(): string
103103

104104
private function getDuplicateKeyUpdate(): string
105105
{
106-
return null === $this->duplicate_key
107-
? ''
108-
: 'ON DUPLICATE KEY UPDATE ' . implode(', ', $this->duplicate_key);
106+
if (null === $this->duplicate_key) {
107+
return '';
108+
}
109+
110+
$keys = [];
111+
foreach ($this->duplicate_key as $key => $value) {
112+
$keys[] = "{$key} = {$value}";
113+
}
114+
115+
return 'ON DUPLICATE KEY UPDATE ' . implode(', ', $keys);
109116
}
110117
}

tests/DataBase/Query/InsertTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,24 @@ public function itCorrectInsertQueryMultyRaws(): void
101101
$insert->queryBind()
102102
);
103103
}
104+
105+
/** @test */
106+
public function itCorrectInsertOnDuplicateKeyUpdate(): void
107+
{
108+
$insert = MyQuery::from('test', $this->PDO)
109+
->insert()
110+
->value('a', 1)
111+
->on('a')
112+
;
113+
114+
$this->assertEquals(
115+
'INSERT INTO test (a) VALUES (:bind_a) ON DUPLICATE KEY UPDATE a = VALUES(a)',
116+
$insert->__toString()
117+
);
118+
119+
$this->assertEquals(
120+
'INSERT INTO test (a) VALUES (1) ON DUPLICATE KEY UPDATE a = VALUES(a)',
121+
$insert->queryBind()
122+
);
123+
}
104124
}

tests/DataBase/RealDatabase/InsertTest.php

+67
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,71 @@ public function itCanInsertMultyRaw()
5252
$this->assertUserExist('sony');
5353
$this->assertUserExist('pradana');
5454
}
55+
56+
/**
57+
* @test
58+
*
59+
* @group database
60+
*/
61+
public function itCanReplaceOnExistData()
62+
{
63+
MyQuery::from('users', $this->pdo)
64+
->insert()
65+
->values([
66+
'user' => 'sony',
67+
'pwd' => 'secret',
68+
'stat' => 99,
69+
])
70+
->execute();
71+
72+
MyQuery::from('users', $this->pdo)
73+
->insert()
74+
->values([
75+
'user' => 'sony',
76+
'pwd' => 'secret',
77+
'stat' => 66,
78+
])
79+
->on('stat')
80+
->execute();
81+
82+
$this->assertUserStat('sony', 66);
83+
}
84+
85+
/**
86+
* @test
87+
*
88+
* @group database
89+
*/
90+
public function itCanUpdateInsertusingOneQuery()
91+
{
92+
MyQuery::from('users', $this->pdo)
93+
->insert()
94+
->values([
95+
'user' => 'sony',
96+
'pwd' => 'secret',
97+
'stat' => 99,
98+
])
99+
->execute();
100+
101+
MyQuery::from('users', $this->pdo)
102+
->insert()
103+
->rows([
104+
[
105+
'user' => 'sony',
106+
'pwd' => 'secret',
107+
'stat' => 66,
108+
],
109+
[
110+
'user' => 'sony2',
111+
'pwd' => 'secret',
112+
'stat' => 66,
113+
],
114+
])
115+
->on('user')
116+
->on('stat')
117+
->execute();
118+
119+
$this->assertUserStat('sony', 66);
120+
$this->assertUserExist('sony2');
121+
}
55122
}

0 commit comments

Comments
 (0)