Showing 4 changed files with 223 additions and 36 deletions
+4
Changes
... ...
@@ -1,4 +1,8 @@
1
+0.2111
2
+  - "created_at" option is renamed to "ctime" option
3
+  - "updated_at" option is renamed to "mtime" option
1 4
 0.2110
5
+  - Perl 5.008007 is required.
2 6
   - added EXPERIMETNAL aysnc_conf attribute
3 7
 0.2109
4 8
   - select method can be called even if table name is not specified
+37 -23
lib/DBIx/Custom.pm
... ...
@@ -1,7 +1,8 @@
1
+use 5.008007;
1 2
 package DBIx::Custom;
2 3
 use Object::Simple -base;
3 4
 
4
-our $VERSION = '0.2110';
5
+our $VERSION = '0.2111';
5 6
 use 5.008001;
6 7
 
7 8
 use Carp 'croak';
... ...
@@ -684,7 +685,7 @@ sub insert {
684 685
   
685 686
   # Timestamp(DEPRECATED!)
686 687
   if (!$multi && $opt{timestamp} && (my $insert_timestamp = $self->insert_timestamp)) {
687
-    warn "insert timestamp option is DEPRECATED! use created_at with now attribute";
688
+    warn "insert timestamp option is DEPRECATED! use ctime option";
688 689
     my $columns = $insert_timestamp->[0];
689 690
     $columns = [$columns] unless ref $columns eq 'ARRAY';
690 691
     my $value = $insert_timestamp->[1];
... ...
@@ -694,16 +695,24 @@ sub insert {
694 695
 
695 696
   # Created time and updated time
696 697
   my @timestamp_cleanup;
697
-  if (defined $opt{created_at} || defined $opt{updated_at}) {
698
+  warn "insert method created_at option is DEPRECATED! "
699
+      . "use ctime option instead. " . _subname
700
+    if $opt{created_at};
701
+  warn "insert method updated_at option is DEPRECATED! "
702
+      . "use mtime option instead. " . _subname
703
+    if $opt{updated_at};
704
+  $opt{ctime} ||= $opt{created_at};
705
+  $opt{mtime} ||= $opt{updated_at};
706
+  if (defined $opt{ctime} || defined $opt{mtime}) {
698 707
     my $now = $self->now;
699 708
     $now = $now->() if ref $now eq 'CODE';
700
-    if (defined $opt{created_at}) {
701
-      $_->{$opt{created_at}} = $now for @$params;
702
-      push @timestamp_cleanup, $opt{created_at};
709
+    if (defined $opt{ctime}) {
710
+      $_->{$opt{ctime}} = $now for @$params;
711
+      push @timestamp_cleanup, $opt{ctime};
703 712
     }
704
-    if (defined $opt{updated_at}) {
705
-      $_->{$opt{updated_at}} = $now for @$params;
706
-      push @timestamp_cleanup, $opt{updated_at};
713
+    if (defined $opt{mtime}) {
714
+      $_->{$opt{mtime}} = $now for @$params;
715
+      push @timestamp_cleanup, $opt{mtime};
707 716
     }
708 717
   }
709 718
   
... ...
@@ -775,8 +784,7 @@ sub include_model {
775 784
     croak qq{"$name_space" is invalid class name } . _subname
776 785
       if $name_space =~ /[^\w:]/;
777 786
     eval "use $name_space";
778
-    croak qq{Name space module "$name_space.pm" is needed. $@ }
779
-        . _subname
787
+    croak qq{Name space module "$name_space.pm" is needed. $@ } . _subname
780 788
       if $@;
781 789
     
782 790
     # Search model modules
... ...
@@ -1193,7 +1201,7 @@ sub update {
1193 1201
   
1194 1202
   # Timestamp(DEPRECATED!)
1195 1203
   if ($opt{timestamp} && (my $update_timestamp = $self->update_timestamp)) {
1196
-    warn "update timestamp option is DEPRECATED! use updated_at and now method";
1204
+    warn "update timestamp option is DEPRECATED! use mtime";
1197 1205
     my $columns = $update_timestamp->[0];
1198 1206
     $columns = [$columns] unless ref $columns eq 'ARRAY';
1199 1207
     my $value = $update_timestamp->[1];
... ...
@@ -1203,11 +1211,15 @@ sub update {
1203 1211
 
1204 1212
   # Created time and updated time
1205 1213
   my @timestamp_cleanup;
1206
-  if (defined $opt{updated_at}) {
1214
+  warn "update method update_at option is DEPRECATED! "
1215
+      . "use mtime option instead " . _subname
1216
+    if $opt{updated_at};
1217
+  $opt{mtime} ||= $opt{updated_at};
1218
+  if (defined $opt{mtime}) {
1207 1219
     my $now = $self->now;
1208 1220
     $now = $now->() if ref $now eq 'CODE';
1209
-    $param->{$opt{updated_at}} = $self->now->();
1210
-    push @timestamp_cleanup, $opt{updated_at};
1221
+    $param->{$opt{mtime}} = $self->now->();
1222
+    push @timestamp_cleanup, $opt{mtime};
1211 1223
   }
1212 1224
 
1213 1225
   # Assign clause
... ...
@@ -2946,11 +2958,11 @@ The SQL like the following one is executed.
2946 2958
 
2947 2959
   insert into book (id, title) values (?, ?), (?, ?);
2948 2960
 
2949
-=item C<created_at>
2961
+=item C<ctime>
2950 2962
 
2951
-  created_at => 'created_datetime'
2963
+  ctime => 'created_time'
2952 2964
 
2953
-Created timestamp column name. time when row is created is set to the column.
2965
+Created time column name. time when row is created is set to the column.
2954 2966
 default time format is "YYYY-mm-dd HH:MM:SS", which can be changed by
2955 2967
 C<now> attribute.
2956 2968
 
... ...
@@ -2990,9 +3002,9 @@ prefix before table name section
2990 3002
 
2991 3003
 Table name.
2992 3004
 
2993
-=item C<updated_at>
3005
+=item C<mtime>
2994 3006
 
2995
-This option is same as C<update> method C<updated_at> option.
3007
+This option is same as C<update> method C<mtime> option.
2996 3008
 
2997 3009
 =item C<wrap>
2998 3010
 
... ...
@@ -3471,11 +3483,11 @@ is executed, the following SQL is executed.
3471 3483
 
3472 3484
   update book set price =  ? + 5;
3473 3485
 
3474
-=item C<updated_at>
3486
+=item C<mtime>
3475 3487
 
3476
-  updated_at => 'updated_datetime'
3488
+  mtime => 'modified_time'
3477 3489
 
3478
-Updated timestamp column name. time when row is updated is set to the column.
3490
+Modified time column name. time row is updated is set to the column.
3479 3491
 default time format is C<YYYY-mm-dd HH:MM:SS>, which can be changed by
3480 3492
 C<now> attribute.
3481 3493
 
... ...
@@ -3649,6 +3661,8 @@ L<DBIx::Custom>
3649 3661
   update_param_tag # will be removed at 2017/1/1
3650 3662
   
3651 3663
   # Options
3664
+  insert method created_at option # will be removed 2017/3/1
3665
+  update method updated_at option # will be removed 2017/3/1
3652 3666
   select column option [COLUMN => ALIAS] syntax # will be removed 2017/1/1
3653 3667
   execute method id option # will be removed 2017/1/1
3654 3668
   update timestamp option # will be removed 2017/1/1
+12 -10
lib/DBIx/Custom/Model.pm
... ...
@@ -7,7 +7,7 @@ use DBIx::Custom::Util '_subname';
7 7
 # Carp trust relationship
8 8
 push @DBIx::Custom::CARP_NOT, __PACKAGE__;
9 9
 
10
-has [qw/dbi table created_at updated_at bind_type join primary_key/],
10
+has [qw/dbi table ctime mtime bind_type join primary_key/],
11 11
   columns => sub { [] };
12 12
 
13 13
 our $AUTOLOAD;
... ...
@@ -47,8 +47,8 @@ for my $method (@methods) {
47 47
 
48 48
   
49 49
   my @attrs = qw/table type primary_key bind_type/;
50
-  my @insert_attrs = qw/created_at updated_at/;
51
-  my @update_attrs = qw/updated_at/;
50
+  my @insert_attrs = qw/created_at updated_at ctime mtime/;
51
+  my @update_attrs = qw/updated_at mtime/;
52 52
   my @select_attrs = qw/join/;
53 53
   if ($method eq 'insert') { push @attrs, @insert_attrs }
54 54
   elsif ($method eq 'update') { push @attrs, @update_attrs }
... ...
@@ -144,7 +144,7 @@ sub new {
144 144
   }
145 145
   
146 146
   # Cache
147
-  for my $attr (qw/dbi table created_at updated_at bind_type join primary_key/) {
147
+  for my $attr (qw/dbi table created_at updated_at ctime mtime bind_type join primary_key/) {
148 148
     $self->$attr;
149 149
     $self->{$attr} = undef unless exists $self->{$attr};
150 150
   }
... ...
@@ -157,6 +157,8 @@ sub new {
157 157
 has 'filter';
158 158
 has 'name';
159 159
 has 'type';
160
+has 'created_at';
161
+has 'updated_at'; 
160 162
 
161 163
 # DEPRECATED!
162 164
 sub method {
... ...
@@ -185,10 +187,10 @@ my $model = DBIx::Custom::Model->new(table => 'books');
185 187
 
186 188
 L<DBIx::Custom> object.
187 189
 
188
-=head2 C<created_at>
190
+=head2 C<ctime>
189 191
 
190
-  my $created_at = $model->created_at;
191
-  $model = $model->created_at('created_datatime');
192
+  my $ctime = $model->ctime;
193
+  $model = $model->ctime('created_time');
192 194
 
193 195
 Create timestamp column, this is passed to C<insert> or C<update> method.
194 196
 
... ...
@@ -225,10 +227,10 @@ Database data type, this is used as type optioon of C<insert>,
225 227
 C<update>, C<update_all>, C<delete>, C<delete_all>,
226 228
 and C<select> method
227 229
 
228
-=head2 C<updated_at>
230
+=head2 C<mtime>
229 231
 
230
-  my $updated_at = $model->updated_at;
231
-  $model = $model->updated_at('updated_datatime');
232
+  my $mtime = $model->mtime;
233
+  $model = $model->mtime('modified_time');
232 234
 
233 235
 Updated timestamp column, this is passed to C<update> method.
234 236
 
+170 -3
t/common.t
... ...
@@ -590,6 +590,16 @@ $row   = $result->one;
590 590
 is($row->{$key1}, 1);
591 591
 like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
592 592
 
593
+eval { $dbi->execute("drop table $table1") };
594
+$dbi->execute($create_table1_2);
595
+$param = {$key1 => 1};
596
+$dbi->insert($param, table => $table1, ctime => $key2);
597
+$result = $dbi->select(table => $table1);
598
+is_deeply($param, {$key1 => 1});
599
+$row   = $result->one;
600
+is($row->{$key1}, 1);
601
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
602
+
593 603
 eval { $dbi->execute("drop table $table1") };
594 604
 $dbi->execute($create_table1_2);
595 605
 $param = {$key1 => 1};
... ...
@@ -600,6 +610,16 @@ $row   = $result->one;
600 610
 is($row->{$key1}, 1);
601 611
 like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
602 612
 
613
+eval { $dbi->execute("drop table $table1") };
614
+$dbi->execute($create_table1_2);
615
+$param = {$key1 => 1};
616
+$dbi->insert($param, table => $table1, mtime => $key3);
617
+$result = $dbi->select(table => $table1);
618
+is_deeply($param, {$key1 => 1});
619
+$row   = $result->one;
620
+is($row->{$key1}, 1);
621
+like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
622
+
603 623
 eval { $dbi->execute("drop table $table1") };
604 624
 $dbi->execute($create_table1_2);
605 625
 $param = {$key1 => 1};
... ...
@@ -612,6 +632,42 @@ like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
612 632
 like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
613 633
 is($row->{$key2}, $row->{$key3});
614 634
 
635
+eval { $dbi->execute("drop table $table1") };
636
+$dbi->execute($create_table1_2);
637
+$param = {$key1 => 1};
638
+$dbi->insert($param, table => $table1, created_at => $key2, mtime => $key3);
639
+$result = $dbi->select(table => $table1);
640
+is_deeply($param, {$key1 => 1});
641
+$row   = $result->one;
642
+is($row->{$key1}, 1);
643
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
644
+like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
645
+is($row->{$key2}, $row->{$key3});
646
+
647
+eval { $dbi->execute("drop table $table1") };
648
+$dbi->execute($create_table1_2);
649
+$param = {$key1 => 1};
650
+$dbi->insert($param, table => $table1, ctime => $key2, updated_at => $key3);
651
+$result = $dbi->select(table => $table1);
652
+is_deeply($param, {$key1 => 1});
653
+$row   = $result->one;
654
+is($row->{$key1}, 1);
655
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
656
+like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
657
+is($row->{$key2}, $row->{$key3});
658
+
659
+eval { $dbi->execute("drop table $table1") };
660
+$dbi->execute($create_table1_2);
661
+$param = {$key1 => 1};
662
+$dbi->insert($param, table => $table1, ctime => $key2, mtime => $key3);
663
+$result = $dbi->select(table => $table1);
664
+is_deeply($param, {$key1 => 1});
665
+$row   = $result->one;
666
+is($row->{$key1}, 1);
667
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
668
+like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
669
+is($row->{$key2}, $row->{$key3});
670
+
615 671
 eval { $dbi->execute("drop table $table1") };
616 672
 $dbi->execute($create_table1_2);
617 673
 $model = $dbi->create_model(table => $table1, created_at => $key2);
... ...
@@ -623,6 +679,17 @@ $row   = $result->one;
623 679
 is($row->{$key1}, 1);
624 680
 like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
625 681
 
682
+eval { $dbi->execute("drop table $table1") };
683
+$dbi->execute($create_table1_2);
684
+$model = $dbi->create_model(table => $table1, ctime => $key2);
685
+$param = {$key1 => 1};
686
+$model->insert($param);
687
+$result = $dbi->select(table => $table1);
688
+is_deeply($param, {$key1 => 1});
689
+$row   = $result->one;
690
+is($row->{$key1}, 1);
691
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
692
+
626 693
 eval { $dbi->execute("drop table $table1") };
627 694
 $dbi->execute($create_table1_2);
628 695
 $param = {$key1 => 1};
... ...
@@ -634,6 +701,17 @@ $row   = $result->one;
634 701
 is($row->{$key1}, 1);
635 702
 like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
636 703
 
704
+eval { $dbi->execute("drop table $table1") };
705
+$dbi->execute($create_table1_2);
706
+$param = {$key1 => 1};
707
+$model = $dbi->create_model(table => $table1, mtime => $key3);
708
+$model->insert($param);
709
+$result = $dbi->select(table => $table1);
710
+is_deeply($param, {$key1 => 1});
711
+$row   = $result->one;
712
+is($row->{$key1}, 1);
713
+like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
714
+
637 715
 eval { $dbi->execute("drop table $table1") };
638 716
 $dbi->execute($create_table1_2);
639 717
 $param = {$key1 => 1};
... ...
@@ -647,6 +725,45 @@ like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
647 725
 like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
648 726
 is($row->{$key2}, $row->{$key3});
649 727
 
728
+eval { $dbi->execute("drop table $table1") };
729
+$dbi->execute($create_table1_2);
730
+$param = {$key1 => 1};
731
+$model = $dbi->create_model(table => $table1, created_at => $key2, mtime => $key3);
732
+$model->insert($param);
733
+$result = $dbi->select(table => $table1);
734
+is_deeply($param, {$key1 => 1});
735
+$row   = $result->one;
736
+is($row->{$key1}, 1);
737
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
738
+like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
739
+is($row->{$key2}, $row->{$key3});
740
+
741
+eval { $dbi->execute("drop table $table1") };
742
+$dbi->execute($create_table1_2);
743
+$param = {$key1 => 1};
744
+$model = $dbi->create_model(table => $table1, ctime=> $key2, updated_at => $key3);
745
+$model->insert($param);
746
+$result = $dbi->select(table => $table1);
747
+is_deeply($param, {$key1 => 1});
748
+$row   = $result->one;
749
+is($row->{$key1}, 1);
750
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
751
+like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
752
+is($row->{$key2}, $row->{$key3});
753
+
754
+eval { $dbi->execute("drop table $table1") };
755
+$dbi->execute($create_table1_2);
756
+$param = {$key1 => 1};
757
+$model = $dbi->create_model(table => $table1, ctime=> $key2, mtime => $key3);
758
+$model->insert($param);
759
+$result = $dbi->select(table => $table1);
760
+is_deeply($param, {$key1 => 1});
761
+$row   = $result->one;
762
+is($row->{$key1}, 1);
763
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
764
+like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
765
+is($row->{$key2}, $row->{$key3});
766
+
650 767
 eval { $dbi->execute("drop table $table1") };
651 768
 $dbi->execute($create_table1);
652 769
 $dbi->insert([{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}] , table => $table1);
... ...
@@ -657,9 +774,25 @@ is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "basic");
657 774
 eval { $dbi->execute("drop table $table1") };
658 775
 $dbi->execute($create_table1_2);
659 776
 $dbi->insert([{$key1 => 1}, {$key1 => 3}] ,
660
-table => $table1,
661
-updated_at => $key2,
662
-created_at => $key3
777
+  table => $table1,
778
+  updated_at => $key2,
779
+  created_at => $key3
780
+);
781
+$result = $dbi->execute("select * from $table1");
782
+$rows   = $result->all;
783
+is($rows->[0]->{$key1}, 1);
784
+is($rows->[1]->{$key1}, 3);
785
+like($rows->[0]->{$key2}, qr/\d{2}:/);
786
+like($rows->[1]->{$key2}, qr/\d{2}:/);
787
+like($rows->[0]->{$key3}, qr/\d{2}:/);
788
+like($rows->[1]->{$key3}, qr/\d{2}:/);
789
+
790
+eval { $dbi->execute("drop table $table1") };
791
+$dbi->execute($create_table1_2);
792
+$dbi->insert([{$key1 => 1}, {$key1 => 3}] ,
793
+  table => $table1,
794
+  mtime => $key2,
795
+  ctime => $key3
663 796
 );
664 797
 $result = $dbi->execute("select * from $table1");
665 798
 $rows   = $result->all;
... ...
@@ -998,6 +1131,17 @@ $row   = $result->one;
998 1131
 is($row->{$key3}, 4);
999 1132
 like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
1000 1133
 
1134
+eval { $dbi->execute("drop table $table1") };
1135
+$dbi->execute($create_table1_2);
1136
+$param = {$key3 => 4};
1137
+$dbi->insert({$key1 => 1, $key2 => 2, $key3 => 3}, table => $table1);
1138
+$dbi->update($param, table => $table1, mtime => $key2, where => {$key1 => 1});
1139
+$result = $dbi->select(table => $table1);
1140
+is_deeply($param, {$key3 => 4});
1141
+$row   = $result->one;
1142
+is($row->{$key3}, 4);
1143
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
1144
+
1001 1145
 eval { $dbi->execute("drop table $table1") };
1002 1146
 $dbi->execute($create_table1_2);
1003 1147
 $param = {$key3 => 4};
... ...
@@ -1009,6 +1153,17 @@ $row   = $result->one;
1009 1153
 is($row->{$key3}, 4);
1010 1154
 like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
1011 1155
 
1156
+eval { $dbi->execute("drop table $table1") };
1157
+$dbi->execute($create_table1_2);
1158
+$param = {$key3 => 4};
1159
+$dbi->insert({$key1 => 1, $key2 => 2, $key3 => 3}, table => $table1);
1160
+$dbi->update($param, table => $table1, mtime => $key2, where => {$key3 => 3});
1161
+$result = $dbi->select(table => $table1);
1162
+is_deeply($param, {$key3 => 4});
1163
+$row   = $result->one;
1164
+is($row->{$key3}, 4);
1165
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
1166
+
1012 1167
 eval { $dbi->execute("drop table $table1") };
1013 1168
 $dbi->execute($create_table1_2);
1014 1169
 $model = $dbi->create_model(table => $table1, updated_at => $key2);
... ...
@@ -1021,6 +1176,18 @@ $row   = $result->one;
1021 1176
 is($row->{$key3}, 4);
1022 1177
 like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
1023 1178
 
1179
+eval { $dbi->execute("drop table $table1") };
1180
+$dbi->execute($create_table1_2);
1181
+$model = $dbi->create_model(table => $table1, mtime => $key2);
1182
+$param = {$key3 => 4};
1183
+$dbi->insert({$key1 => 1, $key2 => 2, $key3 => 3}, table => $table1);
1184
+$model->update($param, where => {$key1 => 1});
1185
+$result = $dbi->select(table => $table1);
1186
+is_deeply($param, {$key3 => 4});
1187
+$row   = $result->one;
1188
+is($row->{$key3}, 4);
1189
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
1190
+
1024 1191
 test 'update_all';
1025 1192
 eval { $dbi->execute("drop table $table1") };
1026 1193
 $dbi->execute($create_table1_2);