Showing 3 changed files with 109 additions and 37 deletions
+3 -3
Changes
... ...
@@ -8,10 +8,10 @@
8 8
     - update_timestamp method is DEPRECATED!
9 9
       use use now method
10 10
     - added EXPEREIMTNAL now method
11
-    - added EXPERIMENTAL execute created_at method
12
-    - added EXPERIMETNAL execute updated_at method
11
+    - added EXPERIMENTAL insert created_at option
12
+    - added EXPERIMETNAL insert updated_at option
13
+    - added EXPERIMETNAL update updated_at option
13 14
     - added new timestamp system using the above methods and options
14
-     
15 15
 0.1738
16 16
     - micro optimization
17 17
     - removed example that query pass execute method in documentation
+52 -34
lib/DBIx/Custom.pm
... ...
@@ -57,7 +57,9 @@ has [qw/connector dsn password quote user exclude_table user_table_info
57 57
             my ($sec, $min, $hour, $mday, $mon, $year) = localtime;
58 58
             $mon++;
59 59
             $year += 1900;
60
-            return sprintf("%04d-%02d-%02d %02d:%02d:%02d");
60
+            my $now = sprintf("%04d-%02d-%02d %02d:%02d:%02d",
61
+              $year, $mon, $mday, $hour, $min, $sec);
62
+            return $now;
61 63
         }
62 64
     },
63 65
     query_builder => sub {
... ...
@@ -371,6 +373,7 @@ sub execute {
371 373
     
372 374
     # Merge second parameter
373 375
     my @cleanup;
376
+    my $saved_param;
374 377
     if (ref $param eq 'ARRAY') {
375 378
         my $param2 = $param->[1];
376 379
         $param = $param->[0];
... ...
@@ -382,7 +385,9 @@ sub execute {
382 385
             else {
383 386
                 delete $param->{$_} for @cleanup;
384 387
                 @cleanup = ();
388
+                $saved_param  = $param;
385 389
                 $param = $self->merge_param($param, $param2);
390
+                delete $saved_param->{$_} for (@{$opt{cleanup} || []});
386 391
                 last;
387 392
             }
388 393
         }
... ...
@@ -436,19 +441,6 @@ sub execute {
436 441
         }
437 442
     }
438 443
     
439
-    # Created time and updated time
440
-    if (defined $opt{created_at} || defined $opt{updated_at}) {
441
-        my $now = $self->now->();
442
-        if (defined $opt{create_at}) {
443
-            $param->{$opt{created_at}} = $now;
444
-            push @cleanup, $opt{created_at};
445
-        }
446
-        if (defined $opt{updated_at}) {
447
-            $param->{$opt{updated_at}} = $now;
448
-            push @cleanup, $opt{updated_at};
449
-        }
450
-    }
451
-    
452 444
     # Cleanup tables(DEPRECATED!)
453 445
     $tables = $self->_remove_duplicate_table($tables, $main_table)
454 446
       if @$tables > 1;
... ...
@@ -527,8 +519,7 @@ sub execute {
527 519
       . qq{$query->{sql}\n} . _subname) if $@;
528 520
 
529 521
     # Remove id from parameter
530
-    delete $param->{$_} for @cleanup;
531
-
522
+    delete $param->{$_} for (@cleanup, @{$opt{cleanup} || []});
532 523
     
533 524
     # DEBUG message
534 525
     if ($ENV{DBIX_CUSTOM_DEBUG}) {
... ...
@@ -626,7 +617,7 @@ sub insert {
626 617
     warn "insert method param option is DEPRECATED!" if $opt{param};
627 618
     $param ||= delete $opt{param} || {};
628 619
     
629
-    # Timestamp
620
+    # Timestamp(DEPRECATED!)
630 621
     if ($opt{timestamp} && (my $insert_timestamp = $self->insert_timestamp)) {
631 622
         warn "insert timestamp option is DEPRECATED! use created_at with now attribute";
632 623
         my $columns = $insert_timestamp->[0];
... ...
@@ -635,6 +626,20 @@ sub insert {
635 626
         $value = $value->() if ref $value eq 'CODE';
636 627
         $param->{$_} = $value for @$columns;
637 628
     }
629
+
630
+    # Created time and updated time
631
+    my @timestamp_cleanup;
632
+    if (defined $opt{created_at} || defined $opt{updated_at}) {
633
+        my $now = $self->now->();
634
+        if (defined $opt{created_at}) {
635
+            $param->{$opt{created_at}} = $now;
636
+            push @timestamp_cleanup, $opt{created_at};
637
+        }
638
+        if (defined $opt{updated_at}) {
639
+            $param->{$opt{updated_at}} = $now;
640
+            push @timestamp_cleanup, $opt{updated_at};
641
+        }
642
+    }
638 643
     
639 644
     # Merge id to parameter
640 645
     my @cleanup;
... ...
@@ -663,6 +668,7 @@ sub insert {
663 668
     
664 669
     # Execute query
665 670
     $opt{statement} = 'insert';
671
+    $opt{cleanup} = \@timestamp_cleanup;
666 672
     $self->execute($sql, $param, %opt);
667 673
 }
668 674
 
... ...
@@ -1085,7 +1091,7 @@ sub update {
1085 1091
     croak qq{update method where option must be specified } . _subname
1086 1092
       if !$opt{where} && !defined $opt{id} && !$opt{allow_update_all};
1087 1093
     
1088
-    # Timestamp
1094
+    # Timestamp(DEPRECATED!)
1089 1095
     if ($opt{timestamp} && (my $update_timestamp = $self->update_timestamp)) {
1090 1096
         warn "update timestamp option is DEPRECATED! use updated_at and now method";
1091 1097
         my $columns = $update_timestamp->[0];
... ...
@@ -1095,6 +1101,13 @@ sub update {
1095 1101
         $param->{$_} = $value for @$columns;
1096 1102
     }
1097 1103
 
1104
+    # Created time and updated time
1105
+    my @timestamp_cleanup;
1106
+    if (defined $opt{updated_at}) {
1107
+        $param->{$opt{updated_at}} = $self->now->();
1108
+        push @timestamp_cleanup, $opt{updated_at};
1109
+    }
1110
+
1098 1111
     # Assign clause
1099 1112
     my $assign_clause = $self->assign_clause($param, {wrap => $opt{wrap}});
1100 1113
     
... ...
@@ -1109,6 +1122,7 @@ sub update {
1109 1122
     
1110 1123
     # Execute query
1111 1124
     $opt{statement} = 'update';
1125
+    $opt{cleanup} = \@timestamp_cleanup;
1112 1126
     $self->execute($sql, [$param, $w->{param}], %opt);
1113 1127
 }
1114 1128
 
... ...
@@ -2596,14 +2610,6 @@ This is used to bind parameter by C<bind_param> of statment handle.
2596 2610
 
2597 2611
     $sth->bind_param($pos, $value, DBI::SQL_BLOB);
2598 2612
 
2599
-=item C<created_at EXPERIMETNAL>
2600
-
2601
-    created_at => 'created_datetime'
2602
-
2603
-Created timestamp column name. time when row is created is set to the column.
2604
-default time format is "YYYY-mm-dd HH:MM:SS", which can be changed by
2605
-C<now> attribute.
2606
-
2607 2613
 =item C<filter>
2608 2614
     
2609 2615
     filter => {
... ...
@@ -2721,14 +2727,6 @@ Turn C<into1> type rule off.
2721 2727
 
2722 2728
 Turn C<into2> type rule off.
2723 2729
 
2724
-=item C<updated_at EXPERIMETNAL>
2725
-
2726
-    updated_at => 'updated_datetime'
2727
-
2728
-Updated timestamp column name. time when row is updated is set to the column.
2729
-default time format is C<YYYY-mm-dd HH:MM:SS>, which can be changed by
2730
-C<now> attribute.
2731
-
2732 2730
 =back
2733 2731
 
2734 2732
 =head2 C<get_column_info>
... ...
@@ -2794,6 +2792,14 @@ and use the following new ones.
2794 2792
 
2795 2793
 =over 4
2796 2794
 
2795
+=item C<created_at EXPERIMETNAL>
2796
+
2797
+    created_at => 'created_datetime'
2798
+
2799
+Created timestamp column name. time when row is created is set to the column.
2800
+default time format is "YYYY-mm-dd HH:MM:SS", which can be changed by
2801
+C<now> attribute.
2802
+
2797 2803
 =item C<id>
2798 2804
 
2799 2805
     id => 4
... ...
@@ -2830,6 +2836,10 @@ prefix before table name section
2830 2836
 
2831 2837
 Table name.
2832 2838
 
2839
+=item C<updated_at EXPERIMENTAL>
2840
+
2841
+This option is same as C<update> method C<updated_at> option.
2842
+
2833 2843
 =item C<wrap>
2834 2844
 
2835 2845
     wrap => {price => sub { "max($_[0])" }}
... ...
@@ -3307,6 +3317,14 @@ is executed, the following SQL is executed.
3307 3317
 
3308 3318
     update book set price =  ? + 5;
3309 3319
 
3320
+=item C<updated_at EXPERIMETNAL>
3321
+
3322
+    updated_at => 'updated_datetime'
3323
+
3324
+Updated timestamp column name. time when row is updated is set to the column.
3325
+default time format is C<YYYY-mm-dd HH:MM:SS>, which can be changed by
3326
+C<now> attribute.
3327
+
3310 3328
 =back
3311 3329
 
3312 3330
 =head2 C<update_all>
+54
t/common.t
... ...
@@ -546,6 +546,38 @@ $result = $dbi->execute("select * from $table1");
546 546
 $rows   = $result->all;
547 547
 is($rows->[0]->{$key1}, $rows->[0]->{$key2});
548 548
 
549
+eval { $dbi->execute("drop table $table1") };
550
+$dbi->execute($create_table1_2);
551
+$param = {$key1 => 1};
552
+$dbi->insert(table => $table1, param => $param, created_at => $key2);
553
+$result = $dbi->select(table => $table1);
554
+is_deeply($param, {$key1 => 1});
555
+$row   = $result->one;
556
+is($row->{$key1}, 1);
557
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
558
+
559
+eval { $dbi->execute("drop table $table1") };
560
+$dbi->execute($create_table1_2);
561
+$param = {$key1 => 1};
562
+$dbi->insert(table => $table1, param => $param, updated_at => $key3);
563
+$result = $dbi->select(table => $table1);
564
+is_deeply($param, {$key1 => 1});
565
+$row   = $result->one;
566
+is($row->{$key1}, 1);
567
+like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
568
+
569
+eval { $dbi->execute("drop table $table1") };
570
+$dbi->execute($create_table1_2);
571
+$param = {$key1 => 1};
572
+$dbi->insert(table => $table1, param => $param, created_at => $key2, updated_at => $key3);
573
+$result = $dbi->select(table => $table1);
574
+is_deeply($param, {$key1 => 1});
575
+$row   = $result->one;
576
+is($row->{$key1}, 1);
577
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
578
+like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
579
+is($row->{$key2}, $row->{$key3});
580
+
549 581
 test 'update_or_insert';
550 582
 eval { $dbi->execute("drop table $table1") };
551 583
 $dbi->execute($create_table1);
... ...
@@ -793,6 +825,28 @@ is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
793 825
                   {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
794 826
                   "basic");
795 827
 
828
+eval { $dbi->execute("drop table $table1") };
829
+$dbi->execute($create_table1_2);
830
+$param = {$key3 => 4};
831
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
832
+$dbi->update(table => $table1, param => $param, updated_at => $key2, where => {$key1 => 1});
833
+$result = $dbi->select(table => $table1);
834
+is_deeply($param, {$key3 => 4});
835
+$row   = $result->one;
836
+is($row->{$key3}, 4);
837
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
838
+
839
+eval { $dbi->execute("drop table $table1") };
840
+$dbi->execute($create_table1_2);
841
+$param = {$key3 => 4};
842
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
843
+$dbi->update(table => $table1, param => $param, updated_at => $key2, where => {$key3 => 3});
844
+$result = $dbi->select(table => $table1);
845
+is_deeply($param, {$key3 => 4});
846
+$row   = $result->one;
847
+is($row->{$key3}, 4);
848
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
849
+
796 850
 test 'update_all';
797 851
 eval { $dbi->execute("drop table $table1") };
798 852
 $dbi->execute($create_table1_2);