Showing 2 changed files with 135 additions and 164 deletions
+121 -14
t/common.t
... ...
@@ -2535,33 +2535,140 @@ $result = $model->select(
2535 2535
 is_deeply($result->one,
2536 2536
           {'table2.key1' => 1, 'table2.key3' => 3});
2537 2537
 
2538
+test 'separator';
2539
+$dbi = DBIx::Custom->connect;
2540
+eval { $dbi->execute('drop table table1') };
2541
+eval { $dbi->execute('drop table table2') };
2542
+$dbi->execute($create_table1);
2543
+$dbi->execute($create_table2);
2538 2544
 
2545
+$dbi->create_model(
2546
+    table => 'table1',
2547
+    join => [
2548
+       'left outer join table2 on table1.key1 = table2.key1'
2549
+    ],
2550
+    primary_key => ['key1'],
2551
+);
2552
+$model2 = $dbi->create_model(
2553
+    table => 'table2',
2554
+);
2555
+$dbi->setup_model;
2556
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2557
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2558
+$model = $dbi->model('table1');
2559
+$result = $model->select(
2560
+    column => [
2561
+        $model->mycolumn,
2562
+        {table2 => [qw/key1 key3/]}
2563
+    ],
2564
+    where => {'table1.key1' => 1}
2565
+);
2566
+is_deeply($result->one,
2567
+          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2568
+is_deeply($model2->select->one, {key1 => 1, key3 => 3});
2539 2569
 
2570
+$dbi->separator('__');
2571
+$model = $dbi->model('table1');
2572
+$result = $model->select(
2573
+    column => [
2574
+        $model->mycolumn,
2575
+        {table2 => [qw/key1 key3/]}
2576
+    ],
2577
+    where => {'table1.key1' => 1}
2578
+);
2579
+is_deeply($result->one,
2580
+          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
2581
+is_deeply($model2->select->one, {key1 => 1, key3 => 3});
2540 2582
 
2583
+$dbi->separator('-');
2584
+$model = $dbi->model('table1');
2585
+$result = $model->select(
2586
+    column => [
2587
+        $model->mycolumn,
2588
+        {table2 => [qw/key1 key3/]}
2589
+    ],
2590
+    where => {'table1.key1' => 1}
2591
+);
2592
+is_deeply($result->one,
2593
+          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
2594
+is_deeply($model2->select->one, {key1 => 1, key3 => 3});
2541 2595
 
2542 2596
 
2597
+test 'filter_off';
2598
+$dbi = DBIx::Custom->connect;
2599
+eval { $dbi->execute('drop table table1') };
2600
+eval { $dbi->execute('drop table table2') };
2601
+$dbi->execute($create_table1);
2602
+$dbi->execute($create_table2);
2543 2603
 
2604
+$dbi->create_model(
2605
+    table => 'table1',
2606
+    join => [
2607
+       'left outer join table2 on table1.key1 = table2.key1'
2608
+    ],
2609
+    primary_key => ['key1'],
2610
+);
2611
+$dbi->setup_model;
2612
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2613
+$model = $dbi->model('table1');
2614
+$result = $model->select(column => 'key1');
2615
+$result->filter(key1 => sub { $_[0] * 2 });
2616
+is_deeply($result->one, {key1 => 2});
2544 2617
 
2618
+test 'available_datetype';
2619
+$dbi = DBIx::Custom->connect;
2620
+ok($dbi->can('available_datatype'));
2545 2621
 
2546 2622
 
2623
+test 'select prefix option';
2624
+$dbi = DBIx::Custom->connect;
2625
+eval { $dbi->execute('drop table table1') };
2626
+$dbi->execute($create_table1);
2627
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2628
+$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
2629
+is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
2547 2630
 
2631
+test 'map_param';
2632
+$dbi = DBIx::Custom->connect;
2633
+$param = $dbi->map_param(
2634
+    {id => 1, author => 'Ken', price => 1900},
2635
+    id => 'book.id',
2636
+    author => ['book.author', sub { '%' . $_[0] . '%' }],
2637
+    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
2638
+);
2639
+is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
2640
+  'book.price' => 1900});
2548 2641
 
2642
+$param = $dbi->map_param(
2643
+    {id => 0, author => 0, price => 0},
2644
+    id => 'book.id',
2645
+    author => ['book.author', sub { '%' . $_[0] . '%' }],
2646
+    price => ['book.price', sub { '%' . $_[0] . '%' },
2647
+      {if => sub { $_[0] eq 0 }}]
2648
+);
2649
+is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
2549 2650
 
2651
+$param = $dbi->map_param(
2652
+    {id => '', author => '', price => ''},
2653
+    id => 'book.id',
2654
+    author => ['book.author', sub { '%' . $_[0] . '%' }],
2655
+    price => ['book.price', sub { '%' . $_[0] . '%' },
2656
+      {if => sub { $_[0] eq 1 }}]
2657
+);
2658
+is_deeply($param, {});
2550 2659
 
2660
+$param = $dbi->map_param(
2661
+    {id => undef, author => undef, price => undef},
2662
+    id => 'book.id',
2663
+    price => ['book.price', {if => 'exists'}]
2664
+);
2665
+is_deeply($param, {'book.price' => undef});
2551 2666
 
2552
-
2553
-
2554
-
2555
-
2556
-
2557
-
2558
-
2559
-
2560
-
2561
-
2562
-
2563
-
2564
-
2565
-
2667
+$param = $dbi->map_param(
2668
+    {price => 'a'},
2669
+    id => ['book.id', {if => 'exists'}],
2670
+    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
2671
+);
2672
+is_deeply($param, {'book.price' => '%a'});
2566 2673
 
2567 2674
 1;
+14 -150
t/sqlite.t
... ...
@@ -201,156 +201,6 @@ $dbi = DBIx::Custom->connect;
201 201
 
202 202
 
203 203
 
204
-test 'separator';
205
-$dbi = DBIx::Custom->connect;
206
-eval { $dbi->execute('drop table table1') };
207
-eval { $dbi->execute('drop table table2') };
208
-$dbi->execute($create_table1);
209
-$dbi->execute($create_table2);
210
-
211
-$dbi->create_model(
212
-    table => 'table1',
213
-    join => [
214
-       'left outer join table2 on table1.key1 = table2.key1'
215
-    ],
216
-    primary_key => ['key1'],
217
-);
218
-$model2 = $dbi->create_model(
219
-    table => 'table2',
220
-);
221
-$dbi->setup_model;
222
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
223
-$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
224
-$model = $dbi->model('table1');
225
-$result = $model->select(
226
-    column => [
227
-        $model->mycolumn,
228
-        {table2 => [qw/key1 key3/]}
229
-    ],
230
-    where => {'table1.key1' => 1}
231
-);
232
-is_deeply($result->one,
233
-          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
234
-is_deeply($model2->select->one, {key1 => 1, key3 => 3});
235
-
236
-$dbi->separator('__');
237
-$model = $dbi->model('table1');
238
-$result = $model->select(
239
-    column => [
240
-        $model->mycolumn,
241
-        {table2 => [qw/key1 key3/]}
242
-    ],
243
-    where => {'table1.key1' => 1}
244
-);
245
-is_deeply($result->one,
246
-          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
247
-is_deeply($model2->select->one, {key1 => 1, key3 => 3});
248
-
249
-$dbi->separator('-');
250
-$model = $dbi->model('table1');
251
-$result = $model->select(
252
-    column => [
253
-        $model->mycolumn,
254
-        {table2 => [qw/key1 key3/]}
255
-    ],
256
-    where => {'table1.key1' => 1}
257
-);
258
-is_deeply($result->one,
259
-          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
260
-is_deeply($model2->select->one, {key1 => 1, key3 => 3});
261
-
262
-
263
-test 'filter_off';
264
-$dbi = DBIx::Custom->connect;
265
-eval { $dbi->execute('drop table table1') };
266
-eval { $dbi->execute('drop table table2') };
267
-$dbi->execute($create_table1);
268
-$dbi->execute($create_table2);
269
-
270
-$dbi->create_model(
271
-    table => 'table1',
272
-    join => [
273
-       'left outer join table2 on table1.key1 = table2.key1'
274
-    ],
275
-    primary_key => ['key1'],
276
-);
277
-$dbi->setup_model;
278
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
279
-$model = $dbi->model('table1');
280
-$result = $model->select(column => 'key1');
281
-$result->filter(key1 => sub { $_[0] * 2 });
282
-is_deeply($result->one, {key1 => 2});
283
-
284
-test 'available_datetype';
285
-$dbi = DBIx::Custom->connect;
286
-ok($dbi->can('available_datatype'));
287
-
288
-
289
-test 'select prefix option';
290
-$dbi = DBIx::Custom->connect;
291
-eval { $dbi->execute('drop table table1') };
292
-$dbi->execute($create_table1);
293
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
294
-$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
295
-is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
296
-
297
-test 'map_param';
298
-$dbi = DBIx::Custom->connect;
299
-$param = $dbi->map_param(
300
-    {id => 1, author => 'Ken', price => 1900},
301
-    id => 'book.id',
302
-    author => ['book.author', sub { '%' . $_[0] . '%' }],
303
-    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
304
-);
305
-is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
306
-  'book.price' => 1900});
307
-
308
-$param = $dbi->map_param(
309
-    {id => 0, author => 0, price => 0},
310
-    id => 'book.id',
311
-    author => ['book.author', sub { '%' . $_[0] . '%' }],
312
-    price => ['book.price', sub { '%' . $_[0] . '%' },
313
-      {if => sub { $_[0] eq 0 }}]
314
-);
315
-is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
316
-
317
-$param = $dbi->map_param(
318
-    {id => '', author => '', price => ''},
319
-    id => 'book.id',
320
-    author => ['book.author', sub { '%' . $_[0] . '%' }],
321
-    price => ['book.price', sub { '%' . $_[0] . '%' },
322
-      {if => sub { $_[0] eq 1 }}]
323
-);
324
-is_deeply($param, {});
325
-
326
-$param = $dbi->map_param(
327
-    {id => undef, author => undef, price => undef},
328
-    id => 'book.id',
329
-    price => ['book.price', {if => 'exists'}]
330
-);
331
-is_deeply($param, {'book.price' => undef});
332
-
333
-$param = $dbi->map_param(
334
-    {price => 'a'},
335
-    id => ['book.id', {if => 'exists'}],
336
-    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
337
-);
338
-is_deeply($param, {'book.price' => '%a'});
339
-
340
-
341
-test 'table_alias';
342
-$dbi = DBIx::Custom->connect;
343
-eval { $dbi->execute('drop table table1') };
344
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
345
-$dbi->type_rule(
346
-    into1 => {
347
-        date => sub { uc $_[0] }
348
-    }
349
-);
350
-$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
351
-  table_alias => {table2 => 'table1'});
352
-$result = $dbi->select(table => 'table1');
353
-is($result->one->{key1}, 'A');
354 204
 
355 205
 
356 206
 test 'order';
... ...
@@ -758,6 +608,20 @@ like($@, qr/unexpected "{"/, "error : 2");
758 608
 
759 609
 
760 610
 ### a little complex test
611
+test 'table_alias';
612
+$dbi = DBIx::Custom->connect;
613
+eval { $dbi->execute('drop table table1') };
614
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
615
+$dbi->type_rule(
616
+    into1 => {
617
+        date => sub { uc $_[0] }
618
+    }
619
+);
620
+$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
621
+  table_alias => {table2 => 'table1'});
622
+$result = $dbi->select(table => 'table1');
623
+is($result->one->{key1}, 'A');
624
+
761 625
 test 'select() wrap option';
762 626
 $dbi = DBIx::Custom->connect;
763 627
 eval { $dbi->execute('drop table table1') };