Showing 5 changed files with 70 additions and 17 deletions
+2 -2
lib/DBIx/Custom.pm
... ...
@@ -162,7 +162,7 @@ sub column {
162 162
     my $table = $option->{alias} || $real_table;
163 163
     
164 164
     # Columns
165
-    unless ($columns) {
165
+    unless (defined $columns) {
166 166
         $columns ||= $self->model($real_table)->columns;
167 167
     }
168 168
     
... ...
@@ -924,7 +924,7 @@ sub select {
924 924
     else { $sql .= $self->_q($tables->[-1] || '') . ' ' }
925 925
     $sql =~ s/, $/ /;
926 926
     croak "select method table option must be specified " . _subname
927
-      unless $tables->[-1];
927
+      unless defined $tables->[-1];
928 928
 
929 929
     # Add tables in parameter
930 930
     unshift @$tables,
+17 -11
lib/DBIx/Custom/Model.pm
... ...
@@ -7,11 +7,8 @@ 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/],
11
-    bind_type => sub { [] },
12
-    columns => sub { [] },
13
-    join => sub { [] },
14
-    primary_key => sub { [] };
10
+has [qw/dbi table created_at updated_at bind_type join primary_key/],
11
+    columns => sub { [] };
15 12
 
16 13
 our $AUTOLOAD;
17 14
 
... ...
@@ -44,13 +41,22 @@ for my $method (@methods) {
44 41
 
45 42
     my $code = sub {
46 43
         my $self = shift;
44
+        
45
+        unless ($self->{_attribute_cache}{$self}) {
46
+            $self->$_ for qw/table bind_type primary_key
47
+              type join created_at updated_at/;
48
+            $self->{_attribute_cache}{$self} = 1;
49
+        }
50
+
47 51
         $self->dbi->$method(
48 52
             @_ % 2 ? shift : (),
49
-            table => $self->table,
50
-            bind_type => $self->bind_type,
51
-            primary_key => $self->primary_key,
52
-            type => $self->type,
53
-            $method =~ /^select/ ? (join => $self->join) : (), 
53
+            table => $self->{table},
54
+            exists $self->{type} ? (type => $self->{type}) : (),
55
+            exists $self->{created_at} && $method eq 'insert' ? (created_at => $self->{created_at}) : (),
56
+            exists $self->{updated_at} && ($method eq 'insert' || $method eq 'update') ? (updated_at => $self->{updated_at}) : (),
57
+            exists $self->{bind_type} ? (bind_type=> $self->{bind_type}) : (),
58
+            exists $self->{primary_key} ? (primary_key => $self->{primary_key}) : (),
59
+            exists $self->{join} && index($method, 'select') != -1 ? (join => $self->{join}) : (),
54 60
             @_
55 61
         )
56 62
     };
... ...
@@ -112,7 +118,7 @@ sub new {
112 118
 # DEPRECATED!
113 119
 has 'filter';
114 120
 has 'name';
115
-has type => sub { [] };
121
+has 'type';
116 122
 
117 123
 
118 124
 # DEPRECATED!
+47
t/common.t
... ...
@@ -578,6 +578,41 @@ like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
578 578
 like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
579 579
 is($row->{$key2}, $row->{$key3});
580 580
 
581
+eval { $dbi->execute("drop table $table1") };
582
+$dbi->execute($create_table1_2);
583
+$model = $dbi->create_model(table => $table1, created_at => $key2);
584
+$param = {$key1 => 1};
585
+$model->insert($param);
586
+$result = $dbi->select(table => $table1);
587
+is_deeply($param, {$key1 => 1});
588
+$row   = $result->one;
589
+is($row->{$key1}, 1);
590
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
591
+
592
+eval { $dbi->execute("drop table $table1") };
593
+$dbi->execute($create_table1_2);
594
+$param = {$key1 => 1};
595
+$model = $dbi->create_model(table => $table1, updated_at => $key3);
596
+$model->insert($param);
597
+$result = $dbi->select(table => $table1);
598
+is_deeply($param, {$key1 => 1});
599
+$row   = $result->one;
600
+is($row->{$key1}, 1);
601
+like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
602
+
603
+eval { $dbi->execute("drop table $table1") };
604
+$dbi->execute($create_table1_2);
605
+$param = {$key1 => 1};
606
+$model = $dbi->create_model(table => $table1, created_at => $key2, updated_at => $key3);
607
+$model->insert($param);
608
+$result = $dbi->select(table => $table1);
609
+is_deeply($param, {$key1 => 1});
610
+$row   = $result->one;
611
+is($row->{$key1}, 1);
612
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
613
+like($row->{$key3}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
614
+is($row->{$key2}, $row->{$key3});
615
+
581 616
 test 'update_or_insert';
582 617
 eval { $dbi->execute("drop table $table1") };
583 618
 $dbi->execute($create_table1);
... ...
@@ -847,6 +882,18 @@ $row   = $result->one;
847 882
 is($row->{$key3}, 4);
848 883
 like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
849 884
 
885
+eval { $dbi->execute("drop table $table1") };
886
+$dbi->execute($create_table1_2);
887
+$model = $dbi->create_model(table => $table1, updated_at => $key2);
888
+$param = {$key3 => 4};
889
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
890
+$model->update($param, where => {$key1 => 1});
891
+$result = $dbi->select(table => $table1);
892
+is_deeply($param, {$key3 => 4});
893
+$row   = $result->one;
894
+is($row->{$key3}, 4);
895
+like($row->{$key2}, qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
896
+
850 897
 test 'update_all';
851 898
 eval { $dbi->execute("drop table $table1") };
852 899
 $dbi->execute($create_table1_2);
+2 -2
t/common/MyModel4/table1.pm
... ...
@@ -1,8 +1,8 @@
1 1
 package MyModel4::table1;
2 2
 
3
-use base 'MyModel4';
3
+use MyModel4 -base;
4 4
 
5
-sub table { 'table1' }
5
+has table => 'table1';
6 6
 
7 7
 sub list { shift->select }
8 8
 
+2 -2
t/common_uc/MyModel4/TABLE1.pm
... ...
@@ -1,8 +1,8 @@
1 1
 package MyModel4::TABLE1;
2 2
 
3
-use base 'MyModel4';
3
+use MyModel4 -base;
4 4
 
5
-sub table { 'TABLE1' }
5
+has table => 'TABLE1';
6 6
 
7 7
 sub list { shift->select }
8 8