Showing 7 changed files with 79 additions and 5 deletions
+4
Changes
... ...
@@ -1,3 +1,7 @@
1
+0.1658
2
+    - added experimental DBIx::Custom::Model column() prefix option.
3
+    - fixed select_at join column invalid bug
4
+    - added DBIx::Custom::Model column() attribute 
1 5
 0.1657
2 6
     - remaned experimental safty_charcter to safty_name
3 7
     - safty_charcter is changed, set only one character regex.
+3 -3
lib/DBIx/Custom.pm
... ...
@@ -1,6 +1,6 @@
1 1
 package DBIx::Custom;
2 2
 
3
-our $VERSION = '0.1657';
3
+our $VERSION = '0.1658';
4 4
 
5 5
 use 5.008001;
6 6
 use strict;
... ...
@@ -752,7 +752,7 @@ sub select {
752 752
 
753 753
 our %VALID_SELECT_AT_ARGS
754 754
   = map { $_ => 1 } qw/table column where append relation filter query selection
755
-                       param primary_key left_join/;
755
+                       param primary_key join/;
756 756
 
757 757
 sub select_at {
758 758
     my ($self, %args) = @_;
... ...
@@ -1150,7 +1150,7 @@ sub _push_join {
1150 1150
         
1151 1151
         my $join_clause = $join->[$i];
1152 1152
         
1153
-        if ($join_clause =~ /\s([^\.\s]+?)\..+\s([^\.\s]+?)\./) {
1153
+        if ($join_clause =~ /\s([^\.\s]+?)\..+\s([^\.\s]+?)\..+?$/) {
1154 1154
             
1155 1155
             my $table1 = $1;
1156 1156
             my $table2 = $2;
+14 -2
lib/DBIx/Custom/Model.pm
... ...
@@ -11,7 +11,7 @@ use Carp 'croak';
11 11
 push @DBIx::Custom::CARP_NOT, __PACKAGE__;
12 12
 
13 13
 __PACKAGE__->attr(
14
-    ['dbi', 'name', 'table'],
14
+    ['dbi', 'name', 'table', 'column'],
15 15
     columns => sub { [] },
16 16
     filter => sub { [] },
17 17
     primary_key => sub { [] },
... ...
@@ -52,10 +52,11 @@ sub column_clause {
52 52
     my $add     = $args->{add} || [];
53 53
     my $remove  = $args->{remove} || [];
54 54
     my %remove  = map {$_ => 1} @$remove;
55
+    my $prefix  = $args->{prefix} || '';
55 56
     
56 57
     my @column;
57 58
     foreach my $column (@$columns) {
58
-        push @column, "$table.$column as $column"
59
+        push @column, "$table.$column as $prefix$column"
59 60
           unless $remove{$column};
60 61
     }
61 62
     
... ...
@@ -117,6 +118,7 @@ sub select {
117 118
     my $self = shift;
118 119
     $self->dbi->select(
119 120
         table => $self->table,
121
+        column => $self->column,
120 122
         join => $self->join,
121 123
         @_
122 124
     );
... ...
@@ -127,7 +129,9 @@ sub select_at {
127 129
     
128 130
     return $self->dbi->select_at(
129 131
         table => $self->table,
132
+        column => $self->column,
130 133
         primary_key => $self->primary_key,
134
+        join => $self->join,
131 135
         @_
132 136
     );
133 137
 }
... ...
@@ -246,6 +250,14 @@ If you add some column, use C<add> option.
246 250
 
247 251
     my $column_clause = $model->column_clause(add => ['company.id as company__id']);
248 252
 
253
+If you add column name prefix, use C<prefix> option
254
+
255
+    my $column_clause = $model->column_clause(prefix => 'book__');
256
+
257
+The following clause is created.
258
+
259
+    book.id as book__id, book.name as book__name
260
+
249 261
 =head2 C<delete>
250 262
 
251 263
     $table->delete(...);
+26
t/dbix-custom-core-sqlite.t
... ...
@@ -1784,3 +1784,29 @@ $rows = $dbi->select(
1784 1784
               'left outer join table3 on table2.key3 = table3.key3']
1785 1785
 )->fetch_hash_all;
1786 1786
 is_deeply($rows, [{table1__key1 => 1}]);
1787
+
1788
+
1789
+test 'column_clause';
1790
+{
1791
+    package MyDBI8;
1792
+    
1793
+    use base 'DBIx::Custom';
1794
+    
1795
+    sub connect {
1796
+        my $self = shift->SUPER::connect(@_);
1797
+        
1798
+        $self->include_model('MyModel7');
1799
+        
1800
+        return $self;
1801
+    }
1802
+}
1803
+$dbi = MyDBI8->connect($NEW_ARGS->{0});
1804
+$dbi->execute($CREATE_TABLE->{0});
1805
+$dbi->execute($CREATE_TABLE->{2});
1806
+$dbi->setup_model;
1807
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1808
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1809
+$model = $dbi->model('table1');
1810
+$result = $model->select_at(where => 1);
1811
+is_deeply($result->fetch_hash_first,
1812
+          {key1 => 1, key2 => 2, table2__key1 => 1, table2__key3 => 3});
+5
t/dbix-custom-core-sqlite/MyModel7.pm
... ...
@@ -0,0 +1,5 @@
1
+package MyModel7;
2
+
3
+use base 'DBIx::Custom::Model';
4
+
5
+1;
+22
t/dbix-custom-core-sqlite/MyModel7/table1.pm
... ...
@@ -0,0 +1,22 @@
1
+package MyModel7::table1;
2
+
3
+use base 'MyModel7';
4
+
5
+__PACKAGE__->attr(
6
+    primary_key => sub { ['key1'] },
7
+    column => sub {
8
+        my $self = shift;
9
+        
10
+        return [
11
+            $self->column_clause,
12
+            $self->model('table2')->column_clause(prefix => 'table2__')
13
+        ];
14
+    },
15
+    join => sub {
16
+        [
17
+            'left outer join table2 on table1.key1 = table2.key1'
18
+        ]
19
+    },
20
+);
21
+
22
+1;
+5
t/dbix-custom-core-sqlite/MyModel7/table2.pm
... ...
@@ -0,0 +1,5 @@
1
+package MyModel7::table2;
2
+
3
+use base 'MyModel7';
4
+
5
+1;