@@ -111,29 +111,159 @@ public function testUpdateWithWhereAndLimit(): void
111
111
}
112
112
}
113
113
114
- public function testUpdateBatch (): void
114
+ /**
115
+ * @dataProvider provideUpdateBatch
116
+ */
117
+ public function testUpdateBatch (string $ constraints , array $ data , array $ expected ): void
115
118
{
116
- $ data = [
117
- [
118
- 'name ' => 'Derek Jones ' ,
119
- 'country ' => 'Greece ' ,
119
+ $ table = 'type_test ' ;
120
+
121
+ // Prepares test data.
122
+ $ builder = $ this ->db ->table ($ table );
123
+ $ builder ->truncate ();
124
+
125
+ for ($ i = 1 ; $ i < 4 ; $ i ++) {
126
+ $ builder ->insert ([
127
+ 'type_varchar ' => 'test ' . $ i ,
128
+ 'type_char ' => 'char ' ,
129
+ 'type_text ' => 'text ' ,
130
+ 'type_smallint ' => 32767 ,
131
+ 'type_integer ' => 2_147_483_647 ,
132
+ 'type_bigint ' => 9_223_372_036_854_775_807 ,
133
+ 'type_float ' => 10.1 ,
134
+ 'type_numeric ' => 123.23 ,
135
+ 'type_date ' => '2023-12-0 ' . $ i ,
136
+ 'type_datetime ' => '2023-12-21 12:00:00 ' ,
137
+ ]);
138
+ }
139
+
140
+ $ this ->db ->table ($ table )->updateBatch ($ data , $ constraints );
141
+
142
+ if ($ this ->db ->DBDriver === 'SQLSRV ' ) {
143
+ // We cannot compare `text` and `varchar` with `=`. It causes the error:
144
+ // [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The data types text and varchar are incompatible in the equal to operator.
145
+ // And data type `text`, `ntext`, `image` are deprecated in SQL Server 2016
146
+ // See https://github.com/codeigniter4/CodeIgniter4/pull/8439#issuecomment-1902535909
147
+ unset($ expected [0 ]['type_text ' ], $ expected [1 ]['type_text ' ]);
148
+ }
149
+
150
+ $ this ->seeInDatabase ($ table , $ expected [0 ]);
151
+ $ this ->seeInDatabase ($ table , $ expected [1 ]);
152
+ }
153
+
154
+ public static function provideUpdateBatch (): iterable
155
+ {
156
+ yield from [
157
+ 'constraints varchar ' => [
158
+ 'type_varchar ' ,
159
+ [
160
+ [
161
+ 'type_varchar ' => 'test1 ' , // Key
162
+ 'type_text ' => 'updated ' ,
163
+ 'type_smallint ' => 9999 ,
164
+ 'type_integer ' => 9_999_999 ,
165
+ 'type_bigint ' => 9_999_999 ,
166
+ 'type_float ' => 99.9 ,
167
+ 'type_numeric ' => 999999.99 ,
168
+ 'type_date ' => '2024-01-01 ' ,
169
+ 'type_datetime ' => '2024-01-01 09:00:00 ' ,
170
+ ],
171
+ [
172
+ 'type_varchar ' => 'test2 ' , // Key
173
+ 'type_text ' => 'updated ' ,
174
+ 'type_smallint ' => 9999 ,
175
+ 'type_integer ' => 9_999_999 ,
176
+ 'type_bigint ' => 9_999_999 ,
177
+ 'type_float ' => 99.9 ,
178
+ 'type_numeric ' => 999999.99 ,
179
+ 'type_date ' => '2024-01-01 ' ,
180
+ 'type_datetime ' => '2024-01-01 09:00:00 ' ,
181
+ ],
182
+ ],
183
+ [
184
+ [
185
+ 'type_varchar ' => 'test1 ' ,
186
+ 'type_text ' => 'updated ' ,
187
+ 'type_smallint ' => 9999 ,
188
+ 'type_integer ' => 9_999_999 ,
189
+ 'type_bigint ' => 9_999_999 ,
190
+ 'type_numeric ' => 999999.99 ,
191
+ 'type_date ' => '2024-01-01 ' ,
192
+ 'type_datetime ' => '2024-01-01 09:00:00 ' ,
193
+ ],
194
+ [
195
+ 'type_varchar ' => 'test2 ' ,
196
+ 'type_text ' => 'updated ' ,
197
+ 'type_smallint ' => 9999 ,
198
+ 'type_integer ' => 9_999_999 ,
199
+ 'type_bigint ' => 9_999_999 ,
200
+ 'type_numeric ' => 999999.99 ,
201
+ 'type_date ' => '2024-01-01 ' ,
202
+ 'type_datetime ' => '2024-01-01 09:00:00 ' ,
203
+ ],
204
+ ],
120
205
],
121
- [
122
- 'name ' => 'Ahmadinejad ' ,
123
- 'country ' => 'Greece ' ,
206
+ 'constraints date ' => [
207
+ 'type_date ' ,
208
+ [
209
+ [
210
+ 'type_text ' => 'updated ' ,
211
+ 'type_bigint ' => 9_999_999 ,
212
+ 'type_date ' => '2023-12-01 ' , // Key
213
+ 'type_datetime ' => '2024-01-01 09:00:00 ' ,
214
+ ],
215
+ [
216
+ 'type_text ' => 'updated ' ,
217
+ 'type_bigint ' => 9_999_999 ,
218
+ 'type_date ' => '2023-12-02 ' , // Key
219
+ 'type_datetime ' => '2024-01-01 09:00:00 ' ,
220
+ ],
221
+ ],
222
+ [
223
+ [
224
+ 'type_varchar ' => 'test1 ' ,
225
+ 'type_text ' => 'updated ' ,
226
+ 'type_bigint ' => 9_999_999 ,
227
+ 'type_date ' => '2023-12-01 ' ,
228
+ 'type_datetime ' => '2024-01-01 09:00:00 ' ,
229
+ ],
230
+ [
231
+ 'type_varchar ' => 'test2 ' ,
232
+ 'type_text ' => 'updated ' ,
233
+ 'type_bigint ' => 9_999_999 ,
234
+ 'type_date ' => '2023-12-02 ' ,
235
+ 'type_datetime ' => '2024-01-01 09:00:00 ' ,
236
+ ],
237
+ ],
238
+ ],
239
+ 'int as string ' => [
240
+ 'type_varchar ' ,
241
+ [
242
+ [
243
+ 'type_varchar ' => 'test1 ' , // Key
244
+ 'type_integer ' => '9999999 ' , // PHP string
245
+ 'type_bigint ' => '2448114396435166946 ' , // PHP string
246
+ ],
247
+ [
248
+ 'type_varchar ' => 'test2 ' , // Key
249
+ 'type_integer ' => '9999999 ' , // PHP string
250
+ 'type_bigint ' => '2448114396435166946 ' , // PHP string
251
+ ],
252
+ ],
253
+ [
254
+ [
255
+ 'type_varchar ' => 'test1 ' ,
256
+ 'type_integer ' => 9_999_999 ,
257
+ 'type_bigint ' => 2_448_114_396_435_166_946 ,
258
+ ],
259
+ [
260
+ 'type_varchar ' => 'test2 ' ,
261
+ 'type_integer ' => 9_999_999 ,
262
+ 'type_bigint ' => 2_448_114_396_435_166_946 ,
263
+ ],
264
+ ],
124
265
],
125
266
];
126
-
127
- $ this ->db ->table ('user ' )->updateBatch ($ data , 'name ' );
128
-
129
- $ this ->seeInDatabase ('user ' , [
130
- 'name ' => 'Derek Jones ' ,
131
- 'country ' => 'Greece ' ,
132
- ]);
133
- $ this ->seeInDatabase ('user ' , [
134
- 'name ' => 'Ahmadinejad ' ,
135
- 'country ' => 'Greece ' ,
136
- ]);
137
267
}
138
268
139
269
public function testUpdateWithWhereSameColumn (): void
0 commit comments