Showing 2 changed files with 78 additions and 50 deletions
+15
Changes
... ...
@@ -1,4 +1,19 @@
1
+0.1739
2
+    - insert timestamp option is DEPRECATED!
3
+      use created_at option with now attribute
4
+    - update timestamp option is DEPRECATED!
5
+      use updated_at option wieh now attribute
6
+    - insert_timestamp method is DEPRECATED!
7
+      use now method
8
+    - update_timestamp method is DEPRECATED!
9
+      use use now method
10
+    - added EXPEREIMTNAL now method
11
+    - added EXPERIMENTAL execute created_at method
12
+    - added EXPERIMETNAL execute updated_at method
13
+    - added new timestamp system using the above methods and options
14
+     
1 15
 0.1738
16
+    - micro optimization
2 17
     - removed example that query pass execute method in documentation
3 18
       this is many bug reason much more than I have expected
4 19
       and passing query to execute method is DEPRECATED!
+63 -50
lib/DBIx/Custom.pm
... ...
@@ -52,6 +52,14 @@ has [qw/connector dsn password quote user exclude_table user_table_info
52 52
     },
53 53
     last_sql => '',
54 54
     models => sub { {} },
55
+    now => sub {
56
+        sub {
57
+            my ($sec, $min, $hour, $mday, $mon, $year) = localtime;
58
+            $mon++;
59
+            $year += 1900;
60
+            return sprintf("%04d-%02d-%02d %02d:%02d:%02d");
61
+        }
62
+    },
55 63
     query_builder => sub {
56 64
         my $self = shift;
57 65
         my $builder = DBIx::Custom::QueryBuilder->new(dbi => $self);
... ...
@@ -428,6 +436,19 @@ sub execute {
428 436
         }
429 437
     }
430 438
     
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
+    
431 452
     # Cleanup tables(DEPRECATED!)
432 453
     $tables = $self->_remove_duplicate_table($tables, $main_table)
433 454
       if @$tables > 1;
... ...
@@ -607,6 +628,7 @@ sub insert {
607 628
     
608 629
     # Timestamp
609 630
     if ($opt{timestamp} && (my $insert_timestamp = $self->insert_timestamp)) {
631
+        warn "insert timestamp option is DEPRECATED! use created_at with now attribute";
610 632
         my $columns = $insert_timestamp->[0];
611 633
         $columns = [$columns] unless ref $columns eq 'ARRAY';
612 634
         my $value = $insert_timestamp->[1];
... ...
@@ -656,6 +678,8 @@ sub _merge_id_to_param {
656 678
 sub insert_timestamp {
657 679
     my $self = shift;
658 680
     
681
+    warn "insert_timestamp method is DEPRECATED! use now attribute";
682
+    
659 683
     if (@_) {
660 684
         $self->{insert_timestamp} = [@_];
661 685
         
... ...
@@ -1063,6 +1087,7 @@ sub update {
1063 1087
     
1064 1088
     # Timestamp
1065 1089
     if ($opt{timestamp} && (my $update_timestamp = $self->update_timestamp)) {
1090
+        warn "update timestamp option is DEPRECATED! use updated_at and now method";
1066 1091
         my $columns = $update_timestamp->[0];
1067 1092
         $columns = [$columns] unless ref $columns eq 'ARRAY';
1068 1093
         my $value = $update_timestamp->[1];
... ...
@@ -1128,6 +1153,8 @@ sub update_or_insert {
1128 1153
 sub update_timestamp {
1129 1154
     my $self = shift;
1130 1155
     
1156
+    warn "update_timestamp method is DEPRECATED! use now method";
1157
+    
1131 1158
     if (@_) {
1132 1159
         $self->{update_timestamp} = [@_];
1133 1160
         
... ...
@@ -2142,6 +2169,22 @@ Filters, registered by C<register_filter> method.
2142 2169
 
2143 2170
 Get last successed SQL executed by C<execute> method.
2144 2171
 
2172
+=head2 C<now EXPERIMENTAL>
2173
+
2174
+    my $now = $dbi->now;
2175
+    $dbi = $dbi->now($now);
2176
+
2177
+Code reference which return time now, default to the following code reference.
2178
+
2179
+    sub {
2180
+        my ($sec, $min, $hour, $mday, $mon, $year) = localtime;
2181
+        $mon++;
2182
+        $year += 1900;
2183
+        return sprintf("%04d-%02d-%02d %02d:%02d:%02d");
2184
+    }
2185
+
2186
+This return the time like "2011-10-14 05:05:27".
2187
+
2145 2188
 =head2 C<models>
2146 2189
 
2147 2190
     my $models = $dbi->models;
... ...
@@ -2553,6 +2596,14 @@ This is used to bind parameter by C<bind_param> of statment handle.
2553 2596
 
2554 2597
     $sth->bind_param($pos, $value, DBI::SQL_BLOB);
2555 2598
 
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
+
2556 2607
 =item C<filter>
2557 2608
     
2558 2609
     filter => {
... ...
@@ -2670,6 +2721,14 @@ Turn C<into1> type rule off.
2670 2721
 
2671 2722
 Turn C<into2> type rule off.
2672 2723
 
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
+
2673 2732
 =back
2674 2733
 
2675 2734
 =head2 C<get_column_info>
... ...
@@ -2771,14 +2830,6 @@ prefix before table name section
2771 2830
 
2772 2831
 Table name.
2773 2832
 
2774
-=item C<timestamp>
2775
-
2776
-    timestamp => 1
2777
-
2778
-If this value is set to 1,
2779
-automatically created timestamp column is set based on
2780
-C<timestamp> attribute's C<insert> value.
2781
-
2782 2833
 =item C<wrap>
2783 2834
 
2784 2835
     wrap => {price => sub { "max($_[0])" }}
... ...
@@ -2843,22 +2894,6 @@ You can get model object by C<model>.
2843 2894
 
2844 2895
 See L<DBIx::Custom::Model> to know model features.
2845 2896
 
2846
-=head2 C<insert_timestamp>
2847
-
2848
-    $dbi->insert_timestamp(
2849
-      [qw/created_at updated_at/]
2850
-        => sub { Time::Piece->localtime->strftime("%Y-%m-%d %H:%M:%S") }
2851
-    );
2852
-
2853
-Timestamp value when C<insert> method is executed
2854
-with C<timestamp> option.
2855
-
2856
-If C<insert_timestamp> is set and C<insert> method is executed
2857
-with C<timestamp> option, column C<created_at> and C<update_at>
2858
-is automatically set to the value like "2010-10-11 10:12:54".
2859
-
2860
-    $dbi->insert($param, table => 'book', timestamp => 1);
2861
-
2862 2897
 =head2 C<like_value>
2863 2898
 
2864 2899
     my $like_value = $dbi->like_value
... ...
@@ -3253,14 +3288,6 @@ prefix before table name section
3253 3288
 
3254 3289
 Table name.
3255 3290
 
3256
-=item C<timestamp>
3257
-
3258
-    timestamp => 1
3259
-
3260
-If this value is set to 1,
3261
-automatically updated timestamp column is set based on
3262
-C<timestamp> attribute's C<update> value.
3263
-
3264 3291
 =item C<where>
3265 3292
 
3266 3293
 Same as C<select> method's C<where> option.
... ...
@@ -3329,24 +3356,6 @@ The following opitons are available adding to C<update> option.
3329 3356
 select method option,
3330 3357
 select method is used to check the row is already exists.
3331 3358
 
3332
-=head2 C<update_timestamp>
3333
-
3334
-    $dbi->update_timestamp(
3335
-      updated_at
3336
-        => sub { Time::Piece->localtime->strftime("%Y-%m-%d %H:%M:%S") }
3337
-    );
3338
-
3339
-Timestamp value when C<update> method is executed
3340
-with C<timestamp> option.
3341
-
3342
-If C<insert_timestamp> is set and C<insert> method is executed
3343
-with C<timestamp> option, column C<update_at>
3344
-is automatically set to the value like "2010-10-11 10:12:54".
3345
-
3346
->|perl|
3347
-$dbi->update($param, table => 'book', timestamp => 1);
3348
-||<
3349
-
3350 3359
 =head2 C<show_datatype>
3351 3360
 
3352 3361
     $dbi->show_datatype($table);
... ...
@@ -3423,6 +3432,8 @@ L<DBIx::Custom>
3423 3432
     cache_method # will be removed at 2017/1/1
3424 3433
     
3425 3434
     # Methods
3435
+    update_timestamp # will be removed at 2017/1/1
3436
+    insert_timestamp # will be removed at 2017/1/1
3426 3437
     method # will be removed at 2017/1/1
3427 3438
     assign_param # will be removed at 2017/1/1
3428 3439
     update_param # will be removed at 2017/1/1
... ...
@@ -3442,6 +3453,8 @@ L<DBIx::Custom>
3442 3453
     update_param_tag # will be removed at 2017/1/1
3443 3454
     
3444 3455
     # Options
3456
+    update timestamp option # will be removed 2017/1/1
3457
+    insert timestamp option # will be removed 2017/1/1
3445 3458
     select method where_param option # will be removed 2017/1/1
3446 3459
     delete method where_param option # will be removed 2017/1/1
3447 3460
     update method where_param option # will be removed 2017/1/1