Commit 799a4ac 1 parent 6de7195 commit 799a4ac Copy full SHA for 799a4ac
File tree 3 files changed +97
-3
lines changed
src/System/Database/MyQuery
3 files changed +97
-3
lines changed Original file line number Diff line number Diff line change @@ -103,8 +103,15 @@ protected function builder(): string
103
103
104
104
private function getDuplicateKeyUpdate (): string
105
105
{
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 );
109
116
}
110
117
}
Original file line number Diff line number Diff line change @@ -101,4 +101,24 @@ public function itCorrectInsertQueryMultyRaws(): void
101
101
$ insert ->queryBind ()
102
102
);
103
103
}
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
+ }
104
124
}
Original file line number Diff line number Diff line change @@ -52,4 +52,71 @@ public function itCanInsertMultyRaw()
52
52
$ this ->assertUserExist ('sony ' );
53
53
$ this ->assertUserExist ('pradana ' );
54
54
}
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
+ }
55
122
}
You can’t perform that action at this time.
0 commit comments