Showing 1 changed files with 305 additions and 14 deletions
+305 -14
t/common.t
... ...
@@ -298,33 +298,337 @@ is($result->fetch->[0], '2010-03-03');
298 298
 
299 299
 
300 300
 
301
+test 'type_rule and filter order';
302
+$dbi = DBIx::Custom->connect;
303
+eval { $dbi->execute('drop table table1') };
304
+$dbi->execute($create_table1_type);
305
+$dbi->type_rule(
306
+    into1 => {
307
+        $date_typename => sub { my $v = shift; $v =~ s/4/5/; return $v }
308
+    },
309
+    into2 => {
310
+        $date_typename => sub { my $v = shift; $v =~ s/5/6/; return $v }
311
+    },
312
+    from1 => {
313
+        $date_datatype => sub { my $v = shift; $v =~ s/6/7/; return $v }
314
+    },
315
+    from2 => {
316
+        $date_datatype => sub { my $v = shift; $v =~ s/7/8/; return $v }
317
+    }
318
+);
319
+$dbi->insert({key1 => '2010-01-03'}, 
320
+  table => 'table1', filter => {key1 => sub { my $v = shift; $v =~ s/3/4/; return $v }});
321
+$result = $dbi->select(table => 'table1');
322
+$result->filter(key1 => sub { my $v = shift; $v =~ s/8/9/; return $v });
323
+is($result->fetch_first->[0], '2010-01-09');
301 324
 
302 325
 
326
+$dbi = DBIx::Custom->connect;
327
+eval { $dbi->execute('drop table table1') };
328
+$dbi->execute($create_table1_type);
329
+$dbi->type_rule(
330
+    from1 => {
331
+        $date_datatype => sub { my $v = shift; $v =~ s/3/4/; return $v }
332
+    },
333
+    from2 => {
334
+        $date_datatype => sub { my $v = shift; $v =~ s/4/5/; return $v }
335
+    },
336
+);
337
+$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
338
+$result = $dbi->select(table => 'table1');
339
+$result->type_rule(
340
+    from1 => {
341
+        $date_datatype => sub { my $v = shift; $v =~ s/3/6/; return $v }
342
+    },
343
+    from2 => {
344
+        $date_datatype => sub { my $v = shift; $v =~ s/6/8/; return $v }
345
+    }
346
+);
347
+$result->filter(key1 => sub { my $v = shift; $v =~ s/8/9/; return $v });
348
+is($result->fetch_first->[0], '2010-01-09');
303 349
 
350
+test 'type_rule_off';
351
+$dbi = DBIx::Custom->connect;
352
+eval { $dbi->execute('drop table table1') };
353
+$dbi->execute($create_table1_type);
354
+$dbi->type_rule(
355
+    from1 => {
356
+        $date_datatype => sub { my $v = shift; $v =~ s/3/5/; return $v }
357
+    },
358
+    into1 => {
359
+        $date_typename => sub { my $v = shift; $v =~ s/3/4/; return $v }
360
+    }
361
+);
362
+$dbi->insert({key1 => '2010-01-03'}, table => 'table1', type_rule_off => 1);
363
+$result = $dbi->select(table => 'table1', type_rule_off => 1);
364
+is($result->type_rule_off->fetch->[0], '2010-01-03');
304 365
 
366
+$dbi = DBIx::Custom->connect;
367
+eval { $dbi->execute('drop table table1') };
368
+$dbi->execute($create_table1_type);
369
+$dbi->type_rule(
370
+    from1 => {
371
+        $date_datatype => sub { my $v = shift; $v =~ s/3/4/; return $v }
372
+    },
373
+    into1 => {
374
+        $date_typename => sub { my $v = shift; $v =~ s/3/5/; return $v }
375
+    }
376
+);
377
+$dbi->insert({key1 => '2010-01-03'}, table => 'table1', type_rule_off => 1);
378
+$result = $dbi->select(table => 'table1', type_rule_off => 1);
379
+is($result->one->{key1}, '2010-01-04');
305 380
 
381
+$dbi = DBIx::Custom->connect;
382
+eval { $dbi->execute('drop table table1') };
383
+$dbi->execute($create_table1_type);
384
+$dbi->type_rule(
385
+    from1 => {
386
+        $date_datatype => sub { my $v = shift; $v =~ s/4/5/; return $v }
387
+    },
388
+    into1 => {
389
+        $date_typename => sub { my $v = shift; $v =~ s/3/4/; return $v }
390
+    }
391
+);
392
+$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
393
+$result = $dbi->select(table => 'table1');
394
+is($result->one->{key1}, '2010-01-05');
306 395
 
396
+$dbi = DBIx::Custom->connect;
397
+eval { $dbi->execute('drop table table1') };
398
+$dbi->execute($create_table1_type);
399
+$dbi->type_rule(
400
+    from1 => {
401
+        $date_datatype => sub { my $v = shift; $v =~ s/4/5/; return $v }
402
+    },
403
+    into1 => {
404
+        $date_typename => sub { my $v = shift; $v =~ s/3/4/; return $v }
405
+    }
406
+);
407
+$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
408
+$result = $dbi->select(table => 'table1');
409
+is($result->fetch->[0], '2010-01-05');
307 410
 
411
+$dbi = DBIx::Custom->connect;
412
+eval { $dbi->execute('drop table table1') };
413
+$dbi->execute($create_table1_type);
414
+$dbi->register_filter(ppp => sub { my $v = shift; $v =~ s/3/4/; return $v });
415
+$dbi->type_rule(
416
+    into1 => {
417
+        $date_typename => 'ppp'
418
+    }
419
+);
420
+$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
421
+$result = $dbi->select(table => 'table1');
422
+is($result->one->{key1}, '2010-01-04');
308 423
 
424
+eval{$dbi->type_rule(
425
+    into1 => {
426
+        $date_typename => 'pp'
427
+    }
428
+)};
429
+like($@, qr/not registered/);
309 430
 
431
+$dbi = DBIx::Custom->connect;
432
+eval { $dbi->execute('drop table table1') };
433
+$dbi->execute($create_table1_type);
434
+eval {
435
+    $dbi->type_rule(
436
+        from1 => {
437
+            Date => sub { $_[0] * 2 },
438
+        }
439
+    );
440
+};
441
+like($@, qr/lower/);
310 442
 
443
+eval {
444
+    $dbi->type_rule(
445
+        into1 => {
446
+            Date => sub { $_[0] * 2 },
447
+        }
448
+    );
449
+};
450
+like($@, qr/lower/);
311 451
 
452
+$dbi = DBIx::Custom->connect;
453
+eval { $dbi->execute('drop table table1') };
454
+$dbi->execute($create_table1_type);
455
+$dbi->type_rule(
456
+    from1 => {
457
+        $date_datatype => sub { my $v = shift; $v =~ s/4/5/; return $v }
458
+    },
459
+    into1 => {
460
+        $date_typename => sub { my $v = shift; $v =~ s/3/4/; return $v }
461
+    }
462
+);
463
+$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
464
+$result = $dbi->select(table => 'table1');
465
+$result->type_rule_off;
466
+is($result->one->{key1}, '2010-01-04');
312 467
 
468
+$dbi = DBIx::Custom->connect;
469
+eval { $dbi->execute('drop table table1') };
470
+$dbi->execute($create_table1_type);
471
+$dbi->type_rule(
472
+    from1 => {
473
+        $date_datatype => sub { my $v = shift; $v =~ s/3/4/; return $v },
474
+        $datetime_datatype => sub { my $v = shift; $v =~ s/3/4/; return $v }
475
+    },
476
+);
477
+$dbi->insert({key1 => '2010-01-03', key2 => '2010-01-01 01:01:03'}, table => 'table1');
478
+$result = $dbi->select(table => 'table1');
479
+$result->type_rule(
480
+    from1 => {
481
+        $date_datatype => sub { my $v = shift; $v =~ s/3/5/; return $v }
482
+    }
483
+);
484
+$row = $result->one;
485
+is($row->{key1}, '2010-01-05');
486
+is($row->{key2}, '2010-01-01 01:01:03');
313 487
 
488
+$result = $dbi->select(table => 'table1');
489
+$result->type_rule(
490
+    from1 => {
491
+        $date_datatype => sub { my $v = shift; $v =~ s/3/5/; return $v }
492
+    }
493
+);
494
+$row = $result->one;
495
+is($row->{key1}, '2010-01-05');
496
+is($row->{key2}, '2010-01-01 01:01:03');
314 497
 
498
+$result = $dbi->select(table => 'table1');
499
+$result->type_rule(
500
+    from1 => {
501
+        $date_datatype => sub { my $v = shift; $v =~ s/3/5/; return $v }
502
+    }
503
+);
504
+$row = $result->one;
505
+is($row->{key1}, '2010-01-05');
506
+is($row->{key2}, '2010-01-01 01:01:03');
315 507
 
508
+$result = $dbi->select(table => 'table1');
509
+$result->type_rule(
510
+    from1 => [$date_datatype => sub { my $v = shift; $v =~ s/3/5/; return $v }]
511
+);
512
+$row = $result->one;
513
+is($row->{key1}, '2010-01-05');
514
+is($row->{key2}, '2010-01-01 01:01:03');
316 515
 
516
+$dbi->register_filter(five => sub { my $v = shift; $v =~ s/3/5/; return $v });
517
+$result = $dbi->select(table => 'table1');
518
+$result->type_rule(
519
+    from1 => [$date_datatype => 'five']
520
+);
521
+$row = $result->one;
522
+is($row->{key1}, '2010-01-05');
523
+is($row->{key2}, '2010-01-01 01:01:03');
317 524
 
525
+$result = $dbi->select(table => 'table1');
526
+$result->type_rule(
527
+    from1 => [$date_datatype => undef]
528
+);
529
+$row = $result->one;
530
+is($row->{key1}, '2010-01-03');
531
+is($row->{key2}, '2010-01-01 01:01:03');
318 532
 
533
+$dbi = DBIx::Custom->connect;
534
+eval { $dbi->execute('drop table table1') };
535
+$dbi->execute($create_table1_type);
536
+$dbi->type_rule(
537
+    from1 => {
538
+        $date_datatype => sub { my $v = shift; $v =~ s/3/4/; return $v },
539
+    },
540
+);
541
+$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
542
+$result = $dbi->select(table => 'table1');
543
+$result->filter(key1 => sub { my $v = shift; $v =~ s/4/5/; return $v });
544
+is($result->one->{key1}, '2010-01-05');
319 545
 
546
+$dbi = DBIx::Custom->connect;
547
+eval { $dbi->execute('drop table table1') };
548
+$dbi->execute($create_table1_type);
549
+$dbi->type_rule(
550
+    from1 => {
551
+        $date_datatype => sub { my $v = shift; $v =~ s/3/4/; return $v }
552
+    },
553
+);
554
+$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
555
+$result = $dbi->select(table => 'table1');
556
+$result->filter(key1 => sub { my $v = shift; $v =~ s/4/5/; return $v });
557
+is($result->fetch->[0], '2010-01-05');
320 558
 
559
+$dbi = DBIx::Custom->connect;
560
+eval { $dbi->execute('drop table table1') };
561
+$dbi->execute($create_table1_type);
562
+$dbi->type_rule(
563
+    into1 => {
564
+        $date_typename => sub { my $v = shift; $v =~ s/3/4/; return $v }
565
+    },
566
+    into2 => {
567
+        $date_typename => sub { my $v = shift; $v =~ s/3/5/; return $v }
568
+    },
569
+    from1 => {
570
+        $date_datatype => sub { my $v = shift; $v =~ s/3/6/; return $v }
571
+    },
572
+    from2 => {
573
+        $date_datatype => sub { my $v = shift; $v =~ s/(3|6)/7/; return $v }
574
+    }
575
+);
576
+$dbi->insert({key1 => '2010-01-03'}, table => 'table1', type_rule_off => 1);
577
+$result = $dbi->select(table => 'table1');
578
+is($result->type_rule_off->fetch_first->[0], '2010-01-03');
579
+$result = $dbi->select(table => 'table1');
580
+is($result->type_rule_on->fetch_first->[0], '2010-01-07');
321 581
 
582
+$dbi = DBIx::Custom->connect;
583
+eval { $dbi->execute('drop table table1') };
584
+$dbi->execute($create_table1_type);
585
+$dbi->type_rule(
586
+    into1 => {
587
+        $date_typename => sub { my $v = shift; $v =~ s/3/4/; return $v }
588
+    },
589
+    into2 => {
590
+        $date_typename => sub { my $v = shift; $v =~ s/3/5/; return $v }
591
+    },
592
+    from1 => {
593
+        $date_datatype => sub { my $v = shift; $v =~ s/(3|5)/6/; return $v }
594
+    },
595
+    from2 => {
596
+        $date_datatype => sub { my $v = shift; $v =~ s/6/7/; return $v }
597
+    }
598
+);
599
+$dbi->insert({key1 => '2010-01-03'}, table => 'table1', type_rule1_off => 1);
600
+$result = $dbi->select(table => 'table1');
601
+is($result->type_rule1_off->fetch_first->[0], '2010-01-05');
602
+$result = $dbi->select(table => 'table1');
603
+is($result->type_rule1_on->fetch_first->[0], '2010-01-07');
322 604
 
323
-
605
+$dbi = DBIx::Custom->connect;
606
+eval { $dbi->execute('drop table table1') };
607
+$dbi->execute($create_table1_type);
608
+$dbi->type_rule(
609
+    into1 => {
610
+        $date_typename => sub { my $v = shift; $v =~ s/3/5/; return $v }
611
+    },
612
+    into2 => {
613
+        $date_typename => sub { my $v = shift; $v =~ s/3/4/; return $v }
614
+    },
615
+    from1 => {
616
+        $date_datatype => sub { my $v = shift; $v =~ s/5/6/; return $v }
617
+    },
618
+    from2 => {
619
+        $date_datatype => sub { my $v = shift; $v =~ s/(3|6)/7/; return $v }
620
+    }
621
+);
622
+$dbi->insert({key1 => '2010-01-03'}, table => 'table1', type_rule2_off => 1);
623
+$result = $dbi->select(table => 'table1');
624
+is($result->type_rule2_off->fetch_first->[0], '2010-01-06');
625
+$result = $dbi->select(table => 'table1');
626
+is($result->type_rule2_on->fetch_first->[0], '2010-01-07');
324 627
 
325 628
 
326 629
 
327 630
 # Create table
631
+$dbi = DBIx::Custom->connect;
328 632
 eval { $dbi->execute('drop table table1') };
329 633
 $dbi->execute($create_table1);
330 634
 $model = $dbi->create_model(table => 'table1');
... ...
@@ -3484,18 +3788,5 @@ $result = $dbi->select(
3484 3788
 is_deeply($result->all, [{'table2.key3' => 4}]);
3485 3789
 
3486 3790
 
3487
-test 'table_alias';
3488
-$dbi = DBIx::Custom->connect;
3489
-eval { $dbi->execute('drop table table1') };
3490
-$dbi->execute($create_table1_type);
3491
-$dbi->type_rule(
3492
-    into1 => {
3493
-        $date_typename => sub { '2010-' . $_[0] }
3494
-    }
3495
-);
3496
-$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => '01-01'},
3497
-  table_alias => {table2 => 'table1'});
3498
-$result = $dbi->select(table => 'table1');
3499
-is($result->one->{key1}, '2010-01-01');
3500 3791
 
3501 3792
 1;