Showing 3 changed files with 110 additions and 56 deletions
+4
Changes
... ...
@@ -1,3 +1,7 @@
1
+0.1723
2
+    - added EXPERIMENTAL update_timestamp method to DBIx::Custom
3
+    - added EXPERIMENTAL insert_timestamp method to DBIx::Custom
4
+    - removed EXPERIMENTAL timestamp attribute from DBIx::Custom
1 5
 0.1722
2 6
     - added EXPERIMENTAL timestamp option to DBIx::Custom insert
3 7
       and update method.
+58 -50
lib/DBIx/Custom.pm
... ...
@@ -1,7 +1,7 @@
1 1
 package DBIx::Custom;
2 2
 use Object::Simple -base;
3 3
 
4
-our $VERSION = '0.1722';
4
+our $VERSION = '0.1723';
5 5
 use 5.008001;
6 6
 
7 7
 use Carp 'croak';
... ...
@@ -65,8 +65,7 @@ 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,
69
-    timestamp => sub { {} };
68
+    tag_parse => 1;
70 69
 
71 70
 sub available_datatype {
72 71
     my $self = shift;
... ...
@@ -618,15 +617,12 @@ sub insert {
618 617
     my $timestamp = $args{timestamp};
619 618
     
620 619
     # 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
-        }
620
+    if ($timestamp && (my $insert_timestamp = $self->insert_timestamp)) {
621
+        my $columns = $insert_timestamp->[0];
622
+        $columns = [$columns] unless ref $columns eq 'ARRAY';
623
+        my $value = $insert_timestamp->[1];
624
+        $value = $value->() if ref $value eq 'CODE';
625
+        $param->{$_} = $value for @$columns;
630 626
     }
631 627
 
632 628
     # Merge parameter
... ...
@@ -674,6 +670,17 @@ sub insert_param {
674 670
            '(' . join(', ', @placeholders) . ')'
675 671
 }
676 672
 
673
+sub insert_timestamp {
674
+    my $self = shift;
675
+    
676
+    if (@_) {
677
+        $self->{insert_timestamp} = [@_];
678
+        
679
+        return $self;
680
+    }
681
+    return $self->{insert_timestamp};
682
+}
683
+
677 684
 sub include_model {
678 685
     my ($self, $name_space, $model_infos) = @_;
679 686
     
... ...
@@ -1166,18 +1173,14 @@ sub update {
1166 1173
     my $timestamp = $args{timestamp};
1167 1174
     
1168 1175
     # 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
-        }
1176
+    if ($timestamp && (my $update_timestamp = $self->update_timestamp)) {
1177
+        my $columns = $update_timestamp->[0];
1178
+        $columns = [$columns] unless ref $columns eq 'ARRAY';
1179
+        my $value = $update_timestamp->[1];
1180
+        $value = $value->() if ref $value eq 'CODE';
1181
+        $param->{$_} = $value for @$columns;
1178 1182
     }
1179 1183
 
1180
-
1181 1184
     # Update clause
1182 1185
     my $update_clause = $self->update_param($param, {wrap => $wrap});
1183 1186
 
... ...
@@ -1230,6 +1233,17 @@ sub update_param {
1230 1233
     return $tag;
1231 1234
 }
1232 1235
 
1236
+sub update_timestamp {
1237
+    my $self = shift;
1238
+    
1239
+    if (@_) {
1240
+        $self->{update_timestamp} = [@_];
1241
+        
1242
+        return $self;
1243
+    }
1244
+    return $self->{update_timestamp};
1245
+}
1246
+
1233 1247
 sub where { DBIx::Custom::Where->new(dbi => shift, @_) }
1234 1248
 
1235 1249
 sub _create_query {
... ...
@@ -2213,27 +2227,6 @@ The performance is up.
2213 2227
 Enable DEPRECATED tag parsing functionality, default to 1.
2214 2228
 If you want to disable tag parsing functionality, set to 0.
2215 2229
 
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
-
2237 2230
 =head2 C<user>
2238 2231
 
2239 2232
     my $user = $dbi->user;
... ...
@@ -2917,6 +2910,14 @@ You can get model object by C<model>.
2917 2910
 
2918 2911
 See L<DBIx::Custom::Model> to know model features.
2919 2912
 
2913
+=head2 C<insert_timestamp EXPERIMENTAL>
2914
+
2915
+    $dbi->insert_timestamp(
2916
+      [qw/created_at updated_at/] => sub { localtime });
2917
+
2918
+Parameter for timestamp columns when C<insert> method is executed
2919
+with C<timestamp> option.
2920
+
2920 2921
 =head2 C<mapper EXPERIMETNAL>
2921 2922
 
2922 2923
     my $mapper = $dbi->mapper(param => $param);
... ...
@@ -3483,6 +3484,13 @@ Create update parameter tag.
3483 3484
 
3484 3485
     set title = :title, author = :author
3485 3486
 
3487
+=head2 C<insert_timestamp EXPERIMENTAL>
3488
+
3489
+    $dbi->update_timestamp(updated_at => sub { localtime });
3490
+
3491
+Parameter for timestamp columns when C<update> method is executed
3492
+with C<timestamp> option.
3493
+
3486 3494
 =head2 C<where>
3487 3495
 
3488 3496
     my $where = $dbi->where(
... ...
@@ -3499,13 +3507,6 @@ Create a new L<DBIx::Custom::Where> object.
3499 3507
 Setup all model objects.
3500 3508
 C<columns> of model object is automatically set, parsing database information.
3501 3509
 
3502
-=head1 ENVIRONMENTAL VARIABLES
3503
-
3504
-=head2 C<DBIX_CUSTOM_DEBUG>
3505
-
3506
-If environment variable C<DBIX_CUSTOM_DEBUG> is set to true,
3507
-executed SQL and bind values are printed to STDERR.
3508
-
3509 3510
 =head2 C<show_datatype EXPERIMENTAL>
3510 3511
 
3511 3512
     $dbi->show_datatype($table);
... ...
@@ -3536,6 +3537,13 @@ Show type name of the columns of specified table.
3536 3537
 
3537 3538
 This type name is used in C<type_rule>'s C<into1> and C<into2>.
3538 3539
 
3540
+=head1 ENVIRONMENTAL VARIABLES
3541
+
3542
+=head2 C<DBIX_CUSTOM_DEBUG>
3543
+
3544
+If environment variable C<DBIX_CUSTOM_DEBUG> is set to true,
3545
+executed SQL and bind values are printed to STDERR.
3546
+
3539 3547
 =head2 C<DBIX_CUSTOM_DEBUG_ENCODING>
3540 3548
 
3541 3549
 DEBUG output encoding. Default to UTF-8.
+48 -6
t/common.t
... ...
@@ -508,14 +508,34 @@ is_deeply($rows, [{$key1 => 0, $key2 => 2}, {$key1 => 3, $key2 => 4}], "basic");
508 508
 
509 509
 eval { $dbi->execute("drop table $table1") };
510 510
 $dbi->execute($create_table1);
511
-$dbi->timestamp({
512
-    insert => [$key1 => '5']
513
-});
511
+$dbi->insert_timestamp(
512
+    $key1 => '5'
513
+);
514 514
 $dbi->insert(table => $table1, param => {$key2 => 2}, timestamp => 1);
515 515
 $result = $dbi->execute("select * from $table1");
516 516
 $rows   = $result->all;
517 517
 is_deeply($rows, [{$key1 => 5, $key2 => 2}], "basic");
518 518
 
519
+eval { $dbi->execute("drop table $table1") };
520
+$dbi->execute($create_table1);
521
+$dbi->insert_timestamp(
522
+    [$key1, $key2] => sub { 5 }
523
+);
524
+$dbi->insert(table => $table1, timestamp => 1);
525
+$result = $dbi->execute("select * from $table1");
526
+$rows   = $result->all;
527
+is_deeply($rows, [{$key1 => 5, $key2 => 5}], "basic");
528
+
529
+eval { $dbi->execute("drop table $table1") };
530
+$dbi->execute($create_table1);
531
+$dbi->insert_timestamp(
532
+    [$key1, $key2] => sub { "" . DBIx::Custom->new }
533
+);
534
+$dbi->insert(table => $table1, timestamp => 1);
535
+$result = $dbi->execute("select * from $table1");
536
+$rows   = $result->all;
537
+is($rows->[0]->{$key1}, $rows->[0]->{$key2});
538
+
519 539
 test 'update';
520 540
 eval { $dbi->execute("drop table $table1") };
521 541
 $dbi->execute($create_table1_2);
... ...
@@ -665,15 +685,37 @@ is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
665 685
 
666 686
 eval { $dbi->execute("drop table $table1") };
667 687
 $dbi->execute($create_table1);
668
-$dbi->timestamp({
669
-    update => [$key1 => '5']
670
-});
688
+$dbi->update_timestamp(
689
+    $key1 => '5'
690
+);
671 691
 $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
672 692
 $dbi->update(table => $table1, timestamp => 1, where => {$key2 => 2});
673 693
 $result = $dbi->execute("select * from $table1");
674 694
 $rows   = $result->all;
675 695
 is_deeply($rows, [{$key1 => 5, $key2 => 2}], "basic");
676 696
 
697
+eval { $dbi->execute("drop table $table1") };
698
+$dbi->execute($create_table1);
699
+$dbi->update_timestamp(
700
+    [$key1, $key2] => sub { '5' }
701
+);
702
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
703
+$dbi->update_all(table => $table1, timestamp => 1);
704
+$result = $dbi->execute("select * from $table1");
705
+$rows   = $result->all;
706
+is_deeply($rows, [{$key1 => 5, $key2 => 5}], "basic");
707
+
708
+eval { $dbi->execute("drop table $table1") };
709
+$dbi->execute($create_table1);
710
+$dbi->update_timestamp(
711
+    [$key1, $key2] => sub { "" . DBIx::Custom->new }
712
+);
713
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
714
+$dbi->update_all(table => $table1, timestamp => 1);
715
+$result = $dbi->execute("select * from $table1");
716
+$rows   = $result->all;
717
+is($rows->[0]->{$key1}, $rows->[0]->{$key2});
718
+
677 719
 test 'update_all';
678 720
 eval { $dbi->execute("drop table $table1") };
679 721
 $dbi->execute($create_table1_2);