Showing 3 changed files with 92 additions and 3 deletions
+3
Changes
... ...
@@ -1,4 +1,7 @@
1 1
 0.1722
2
+    - added EXPERIMENTAL timestamp option to DBIx::Custom insert
3
+      and update method.
4
+    - added EXPERIMENTAL timestamp attribute to DBIx::Custom
2 5
     - added {KEY => {OPTION_KEY => OPTION_VALUE} syntax
3 6
       to EXPERIMENTAL DBIx::Custom::Mapper map method
4 7
     - added {KEY => sub { VALUE }} syntax
+68 -3
lib/DBIx/Custom.pm
... ...
@@ -65,7 +65,8 @@ has [qw/connector dsn password quote user exclude_table user_table_info
65 65
     safety_character => '\w',
66 66
     separator => '.',
67 67
     stash => sub { {} },
68
-    tag_parse => 1;
68
+    tag_parse => 1,
69
+    timestamp => sub { {} };
69 70
 
70 71
 sub available_datatype {
71 72
     my $self = shift;
... ...
@@ -374,8 +375,8 @@ sub each_table {
374 375
 
375 376
 our %VALID_ARGS = map { $_ => 1 } qw/append allow_delete_all
376 377
   allow_update_all bind_type column filter id join param prefix primary_key
377
-  query relation sqlfilter table table_alias type type_rule_off type_rule1_off
378
-  type_rule2_off wrap/;
378
+  query relation sqlfilter table table_alias timestamp type type_rule_off
379
+  type_rule1_off type_rule2_off wrap/;
379 380
 
380 381
 sub execute {
381 382
     my $self = shift;
... ...
@@ -614,6 +615,19 @@ sub insert {
614 615
     $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY';
615 616
     my $prefix = delete $args{prefix};
616 617
     my $wrap = delete $args{wrap};
618
+    my $timestamp = $args{timestamp};
619
+    
620
+    # Timestamp
621
+    if ($timestamp) {
622
+        my $column = $self->timestamp->{insert}[0];
623
+        my $v   = $self->timestamp->{insert}[1];
624
+        my $value;
625
+        
626
+        if (defined $column && defined $v) {
627
+            $value = ref $v eq 'SCALAR' ? $v : \$v;
628
+            $param->{$column} = $value;
629
+        }
630
+    }
617 631
 
618 632
     # Merge parameter
619 633
     if (defined $id) {
... ...
@@ -1149,6 +1163,20 @@ sub update {
1149 1163
     $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY';
1150 1164
     my $prefix = delete $args{prefix};
1151 1165
     my $wrap = delete $args{wrap};
1166
+    my $timestamp = $args{timestamp};
1167
+    
1168
+    # Timestamp
1169
+    if ($timestamp) {
1170
+        my $column = $self->timestamp->{update}[0];
1171
+        my $v   = $self->timestamp->{update}[1];
1172
+        my $value;
1173
+        
1174
+        if (defined $column && defined $v) {
1175
+            $value = ref $v eq 'SCALAR' ? $v : \$v;
1176
+            $param->{$column} = $value;
1177
+        }
1178
+    }
1179
+
1152 1180
 
1153 1181
     # Update clause
1154 1182
     my $update_clause = $self->update_param($param, {wrap => $wrap});
... ...
@@ -2185,6 +2213,27 @@ The performance is up.
2185 2213
 Enable DEPRECATED tag parsing functionality, default to 1.
2186 2214
 If you want to disable tag parsing functionality, set to 0.
2187 2215
 
2216
+=head2 C<timestamp EXPERIMENTAL>
2217
+
2218
+    my $timestamp = $dbi->timestamp($timestamp);
2219
+    $dbi = $dbi->timestamp;
2220
+
2221
+Timestamp information.
2222
+
2223
+    $dbi->timestamp({
2224
+        insert => [created_at => 'NOW()'],
2225
+        update => [updated_at => 'NOW()']
2226
+    });
2227
+
2228
+This value is used when C<insert> or C<update> method's C<timestamp>
2229
+option is specified.
2230
+
2231
+C<insert> is used by C<insert> method, and C<update> is
2232
+used by C<update> method.
2233
+
2234
+Key, such as C<create_at> is column name.
2235
+value such as C<NOW()> is DB function.
2236
+
2188 2237
 =head2 C<user>
2189 2238
 
2190 2239
     my $user = $dbi->user;
... ...
@@ -2776,6 +2825,14 @@ Table name.
2776 2825
 
2777 2826
 Same as C<execute> method's C<type_rule_off> option.
2778 2827
 
2828
+=item C<timestamp EXPERIMENTAL>
2829
+
2830
+    timestamp => 1
2831
+
2832
+If this value is set to 1,
2833
+automatically created timestamp column is set based on
2834
+C<timestamp> attribute's C<insert> value.
2835
+
2779 2836
 =item C<type_rule1_off> EXPERIMENTAL
2780 2837
 
2781 2838
     type_rule1_off => 1
... ...
@@ -3366,6 +3423,14 @@ Same as C<execute> method's C<sqlfilter> option.
3366 3423
 
3367 3424
 Table name.
3368 3425
 
3426
+=item C<timestamp EXPERIMENTAL>
3427
+
3428
+    timestamp => 1
3429
+
3430
+If this value is set to 1,
3431
+automatically updated timestamp column is set based on
3432
+C<timestamp> attribute's C<update> value.
3433
+
3369 3434
 =item C<type_rule_off> EXPERIMENTAL
3370 3435
 
3371 3436
 Same as C<execute> method's C<type_rule_off> option.
+21
t/common.t
... ...
@@ -506,6 +506,16 @@ $result = $dbi->execute("select * from $table1");
506 506
 $rows   = $result->all;
507 507
 is_deeply($rows, [{$key1 => 0, $key2 => 2}, {$key1 => 3, $key2 => 4}], "basic");
508 508
 
509
+eval { $dbi->execute("drop table $table1") };
510
+$dbi->execute($create_table1);
511
+$dbi->timestamp({
512
+    insert => [$key1 => '5']
513
+});
514
+$dbi->insert(table => $table1, param => {$key2 => 2}, timestamp => 1);
515
+$result = $dbi->execute("select * from $table1");
516
+$rows   = $result->all;
517
+is_deeply($rows, [{$key1 => 5, $key2 => 2}], "basic");
518
+
509 519
 test 'update';
510 520
 eval { $dbi->execute("drop table $table1") };
511 521
 $dbi->execute($create_table1_2);
... ...
@@ -653,6 +663,17 @@ is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
653 663
                   {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
654 664
                   "basic");
655 665
 
666
+eval { $dbi->execute("drop table $table1") };
667
+$dbi->execute($create_table1);
668
+$dbi->timestamp({
669
+    update => [$key1 => '5']
670
+});
671
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
672
+$dbi->update(table => $table1, timestamp => 1, where => {$key2 => 2});
673
+$result = $dbi->execute("select * from $table1");
674
+$rows   = $result->all;
675
+is_deeply($rows, [{$key1 => 5, $key2 => 2}], "basic");
676
+
656 677
 test 'update_all';
657 678
 eval { $dbi->execute("drop table $table1") };
658 679
 $dbi->execute($create_table1_2);