Showing 2 changed files with 183 additions and 184 deletions
+161
t/common.t
... ...
@@ -2228,6 +2228,167 @@ $result = $model->select_at(
2228 2228
 is_deeply($result->one,
2229 2229
           {key1 => 1, 'table2.key1' => 1});
2230 2230
 
2231
+test 'merge_param';
2232
+$dbi = DBIx::Custom->new;
2233
+$params = [
2234
+    {key1 => 1, key2 => 2, key3 => 3},
2235
+    {key1 => 1, key2 => 2},
2236
+    {key1 => 1}
2237
+];
2238
+$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
2239
+is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2240
+
2241
+$params = [
2242
+    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2243
+    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2244
+];
2245
+$param = $dbi->merge_param($params->[0], $params->[1]);
2246
+is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]});
2247
+
2248
+test 'select() param option';
2249
+$dbi = DBIx::Custom->connect;
2250
+eval { $dbi->execute('drop table table1') };
2251
+$dbi->execute($create_table1);
2252
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2253
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2254
+eval { $dbi->execute('drop table table2') };
2255
+$dbi->execute($create_table2);
2256
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2257
+$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
2258
+$rows = $dbi->select(
2259
+    table => 'table1',
2260
+    column => 'table1.key1 as table1_key1, key2, key3',
2261
+    where   => {'table1.key2' => 3},
2262
+    join  => ['inner join (select * from table2 where {= table2.key3})' . 
2263
+              ' as table2 on table1.key1 = table2.key1'],
2264
+    param => {'table2.key3' => 5}
2265
+)->all;
2266
+is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
2267
+
2268
+test 'select() string where';
2269
+$dbi = DBIx::Custom->connect;
2270
+eval { $dbi->execute('drop table table1') };
2271
+$dbi->execute($create_table1);
2272
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2273
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2274
+$rows = $dbi->select(
2275
+    table => 'table1',
2276
+    where => 'key1 = :key1 and key2 = :key2',
2277
+    where_param => {key1 => 1, key2 => 2}
2278
+)->all;
2279
+is_deeply($rows, [{key1 => 1, key2 => 2}]);
2280
+
2281
+$dbi = DBIx::Custom->connect;
2282
+eval { $dbi->execute('drop table table1') };
2283
+$dbi->execute($create_table1);
2284
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2285
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2286
+$rows = $dbi->select(
2287
+    table => 'table1',
2288
+    where => [
2289
+        'key1 = :key1 and key2 = :key2',
2290
+        {key1 => 1, key2 => 2}
2291
+    ]
2292
+)->all;
2293
+is_deeply($rows, [{key1 => 1, key2 => 2}]);
2294
+
2295
+test 'delete() string where';
2296
+$dbi = DBIx::Custom->connect;
2297
+eval { $dbi->execute('drop table table1') };
2298
+$dbi->execute($create_table1);
2299
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2300
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2301
+$dbi->delete(
2302
+    table => 'table1',
2303
+    where => 'key1 = :key1 and key2 = :key2',
2304
+    where_param => {key1 => 1, key2 => 2}
2305
+);
2306
+$rows = $dbi->select(table => 'table1')->all;
2307
+is_deeply($rows, [{key1 => 2, key2 => 3}]);
2308
+
2309
+$dbi = DBIx::Custom->connect;
2310
+eval { $dbi->execute('drop table table1') };
2311
+$dbi->execute($create_table1);
2312
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2313
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2314
+$dbi->delete(
2315
+    table => 'table1',
2316
+    where => [
2317
+        'key1 = :key1 and key2 = :key2',
2318
+         {key1 => 1, key2 => 2}
2319
+    ]
2320
+);
2321
+$rows = $dbi->select(table => 'table1')->all;
2322
+is_deeply($rows, [{key1 => 2, key2 => 3}]);
2323
+
2324
+
2325
+test 'update() string where';
2326
+$dbi = DBIx::Custom->connect;
2327
+eval { $dbi->execute('drop table table1') };
2328
+$dbi->execute($create_table1);
2329
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2330
+$dbi->update(
2331
+    table => 'table1',
2332
+    param => {key1 => 5},
2333
+    where => 'key1 = :key1 and key2 = :key2',
2334
+    where_param => {key1 => 1, key2 => 2}
2335
+);
2336
+$rows = $dbi->select(table => 'table1')->all;
2337
+is_deeply($rows, [{key1 => 5, key2 => 2}]);
2338
+
2339
+$dbi = DBIx::Custom->connect;
2340
+eval { $dbi->execute('drop table table1') };
2341
+$dbi->execute($create_table1);
2342
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2343
+$dbi->update(
2344
+    table => 'table1',
2345
+    param => {key1 => 5},
2346
+    where => [
2347
+        'key1 = :key1 and key2 = :key2',
2348
+        {key1 => 1, key2 => 2}
2349
+    ]
2350
+);
2351
+$rows = $dbi->select(table => 'table1')->all;
2352
+is_deeply($rows, [{key1 => 5, key2 => 2}]);
2353
+
2354
+test 'insert id and primary_key option';
2355
+$dbi = DBIx::Custom->connect;
2356
+eval { $dbi->execute('drop table table1') };
2357
+$dbi->execute($create_table1_2);
2358
+$dbi->insert(
2359
+    primary_key => ['key1', 'key2'], 
2360
+    table => 'table1',
2361
+    id => [1, 2],
2362
+    param => {key3 => 3}
2363
+);
2364
+is($dbi->select(table => 'table1')->one->{key1}, 1);
2365
+is($dbi->select(table => 'table1')->one->{key2}, 2);
2366
+is($dbi->select(table => 'table1')->one->{key3}, 3);
2367
+
2368
+$dbi->delete_all(table => 'table1');
2369
+$dbi->insert(
2370
+    primary_key => 'key1', 
2371
+    table => 'table1',
2372
+    id => 0,
2373
+    param => {key2 => 2, key3 => 3}
2374
+);
2375
+
2376
+is($dbi->select(table => 'table1')->one->{key1}, 0);
2377
+is($dbi->select(table => 'table1')->one->{key2}, 2);
2378
+is($dbi->select(table => 'table1')->one->{key3}, 3);
2379
+
2380
+$dbi = DBIx::Custom->connect;
2381
+eval { $dbi->execute('drop table table1') };
2382
+$dbi->execute($create_table1_2);
2383
+$dbi->insert(
2384
+    {key3 => 3},
2385
+    primary_key => ['key1', 'key2'], 
2386
+    table => 'table1',
2387
+    id => [1, 2],
2388
+);
2389
+is($dbi->select(table => 'table1')->one->{key1}, 1);
2390
+is($dbi->select(table => 'table1')->one->{key2}, 2);
2391
+is($dbi->select(table => 'table1')->one->{key3}, 3);
2231 2392
 
2232 2393
 
2233 2394
 
+22 -184
t/sqlite.t
... ...
@@ -196,190 +196,6 @@ my $binary;
196 196
 # Prepare table
197 197
 $dbi = DBIx::Custom->connect;
198 198
 
199
-test 'merge_param';
200
-$dbi = DBIx::Custom->new;
201
-$params = [
202
-    {key1 => 1, key2 => 2, key3 => 3},
203
-    {key1 => 1, key2 => 2},
204
-    {key1 => 1}
205
-];
206
-$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
207
-is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
208
-
209
-$params = [
210
-    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
211
-    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
212
-];
213
-$param = $dbi->merge_param($params->[0], $params->[1]);
214
-is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]});
215
-
216
-test 'select() param option';
217
-$dbi = DBIx::Custom->connect;
218
-eval { $dbi->execute('drop table table1') };
219
-$dbi->execute($create_table1);
220
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
221
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
222
-eval { $dbi->execute('drop table table2') };
223
-$dbi->execute($create_table2);
224
-$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
225
-$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
226
-$rows = $dbi->select(
227
-    table => 'table1',
228
-    column => 'table1.key1 as table1_key1, key2, key3',
229
-    where   => {'table1.key2' => 3},
230
-    join  => ['inner join (select * from table2 where {= table2.key3})' . 
231
-              ' as table2 on table1.key1 = table2.key1'],
232
-    param => {'table2.key3' => 5}
233
-)->all;
234
-is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
235
-
236
-
237
-test 'select() wrap option';
238
-$dbi = DBIx::Custom->connect;
239
-eval { $dbi->execute('drop table table1') };
240
-$dbi->execute($create_table1);
241
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
242
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
243
-$rows = $dbi->select(
244
-    table => 'table1',
245
-    column => 'key1',
246
-    wrap => ['select * from (', ') as t where key1 = 1']
247
-)->all;
248
-is_deeply($rows, [{key1 => 1}]);
249
-
250
-eval {
251
-$dbi->select(
252
-    table => 'table1',
253
-    column => 'key1',
254
-    wrap => 'select * from ('
255
-)
256
-};
257
-like($@, qr/array/);
258
-
259
-test 'select() string where';
260
-$dbi = DBIx::Custom->connect;
261
-eval { $dbi->execute('drop table table1') };
262
-$dbi->execute($create_table1);
263
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
264
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
265
-$rows = $dbi->select(
266
-    table => 'table1',
267
-    where => 'key1 = :key1 and key2 = :key2',
268
-    where_param => {key1 => 1, key2 => 2}
269
-)->all;
270
-is_deeply($rows, [{key1 => 1, key2 => 2}]);
271
-
272
-$dbi = DBIx::Custom->connect;
273
-eval { $dbi->execute('drop table table1') };
274
-$dbi->execute($create_table1);
275
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
276
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
277
-$rows = $dbi->select(
278
-    table => 'table1',
279
-    where => [
280
-        'key1 = :key1 and key2 = :key2',
281
-        {key1 => 1, key2 => 2}
282
-    ]
283
-)->all;
284
-is_deeply($rows, [{key1 => 1, key2 => 2}]);
285
-
286
-test 'delete() string where';
287
-$dbi = DBIx::Custom->connect;
288
-eval { $dbi->execute('drop table table1') };
289
-$dbi->execute($create_table1);
290
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
291
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
292
-$dbi->delete(
293
-    table => 'table1',
294
-    where => 'key1 = :key1 and key2 = :key2',
295
-    where_param => {key1 => 1, key2 => 2}
296
-);
297
-$rows = $dbi->select(table => 'table1')->all;
298
-is_deeply($rows, [{key1 => 2, key2 => 3}]);
299
-
300
-$dbi = DBIx::Custom->connect;
301
-eval { $dbi->execute('drop table table1') };
302
-$dbi->execute($create_table1);
303
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
304
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
305
-$dbi->delete(
306
-    table => 'table1',
307
-    where => [
308
-        'key1 = :key1 and key2 = :key2',
309
-         {key1 => 1, key2 => 2}
310
-    ]
311
-);
312
-$rows = $dbi->select(table => 'table1')->all;
313
-is_deeply($rows, [{key1 => 2, key2 => 3}]);
314
-
315
-
316
-test 'update() string where';
317
-$dbi = DBIx::Custom->connect;
318
-eval { $dbi->execute('drop table table1') };
319
-$dbi->execute($create_table1);
320
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
321
-$dbi->update(
322
-    table => 'table1',
323
-    param => {key1 => 5},
324
-    where => 'key1 = :key1 and key2 = :key2',
325
-    where_param => {key1 => 1, key2 => 2}
326
-);
327
-$rows = $dbi->select(table => 'table1')->all;
328
-is_deeply($rows, [{key1 => 5, key2 => 2}]);
329
-
330
-$dbi = DBIx::Custom->connect;
331
-eval { $dbi->execute('drop table table1') };
332
-$dbi->execute($create_table1);
333
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
334
-$dbi->update(
335
-    table => 'table1',
336
-    param => {key1 => 5},
337
-    where => [
338
-        'key1 = :key1 and key2 = :key2',
339
-        {key1 => 1, key2 => 2}
340
-    ]
341
-);
342
-$rows = $dbi->select(table => 'table1')->all;
343
-is_deeply($rows, [{key1 => 5, key2 => 2}]);
344
-
345
-test 'insert id and primary_key option';
346
-$dbi = DBIx::Custom->connect;
347
-eval { $dbi->execute('drop table table1') };
348
-$dbi->execute($create_table1_2);
349
-$dbi->insert(
350
-    primary_key => ['key1', 'key2'], 
351
-    table => 'table1',
352
-    id => [1, 2],
353
-    param => {key3 => 3}
354
-);
355
-is($dbi->select(table => 'table1')->one->{key1}, 1);
356
-is($dbi->select(table => 'table1')->one->{key2}, 2);
357
-is($dbi->select(table => 'table1')->one->{key3}, 3);
358
-
359
-$dbi->delete_all(table => 'table1');
360
-$dbi->insert(
361
-    primary_key => 'key1', 
362
-    table => 'table1',
363
-    id => 0,
364
-    param => {key2 => 2, key3 => 3}
365
-);
366
-
367
-is($dbi->select(table => 'table1')->one->{key1}, 0);
368
-is($dbi->select(table => 'table1')->one->{key2}, 2);
369
-is($dbi->select(table => 'table1')->one->{key3}, 3);
370
-
371
-$dbi = DBIx::Custom->connect;
372
-eval { $dbi->execute('drop table table1') };
373
-$dbi->execute($create_table1_2);
374
-$dbi->insert(
375
-    {key3 => 3},
376
-    primary_key => ['key1', 'key2'], 
377
-    table => 'table1',
378
-    id => [1, 2],
379
-);
380
-is($dbi->select(table => 'table1')->one->{key1}, 1);
381
-is($dbi->select(table => 'table1')->one->{key2}, 2);
382
-is($dbi->select(table => 'table1')->one->{key3}, 3);
383 199
 
384 200
 
385 201
 test 'model insert id and primary_key option';
... ...
@@ -1141,6 +957,28 @@ like($@, qr/unexpected "{"/, "error : 2");
1141 957
 
1142 958
 
1143 959
 ### a little complex test
960
+test 'select() wrap option';
961
+$dbi = DBIx::Custom->connect;
962
+eval { $dbi->execute('drop table table1') };
963
+$dbi->execute($create_table1);
964
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
965
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
966
+$rows = $dbi->select(
967
+    table => 'table1',
968
+    column => 'key1',
969
+    wrap => ['select * from (', ') as t where key1 = 1']
970
+)->all;
971
+is_deeply($rows, [{key1 => 1}]);
972
+
973
+eval {
974
+$dbi->select(
975
+    table => 'table1',
976
+    column => 'key1',
977
+    wrap => 'select * from ('
978
+)
979
+};
980
+like($@, qr/array/);
981
+
1144 982
 test 'dbi method from model';
1145 983
 $dbi = MyDBI9->connect;
1146 984
 eval { $dbi->execute('drop table table1') };