Showing 25 changed files with 232 additions and 13 deletions
+2
Changes
... ...
@@ -1,3 +1,5 @@
1
+0.24
2
+  - support full-qualified table name.
1 3
 0.23
2 4
   - DBIx::Custom::Mapper::map method support
3 5
     {value => '%<value>%'} syntax
+34 -5
lib/DBIx/Custom.pm
... ...
@@ -790,16 +790,32 @@ sub include_model {
790 790
       if $@;
791 791
     
792 792
     # Search model modules
793
+    $DB::single = 1;
793 794
     my $path = $INC{"$name_space.pm"};
794 795
     $path =~ s/\.pm$//;
795 796
     opendir my $dh, $path
796 797
       or croak qq{Can't open directory "$path": $! } . _subname
797
-    $model_infos = [];
798
-    while (my $module = readdir $dh) {
799
-      push @$model_infos, $module
800
-        if $module =~ s/\.pm$//;
798
+    my @modules;
799
+    while (my $file = readdir $dh) {
800
+      my $file_abs = "$path/$file";
801
+      if (-d $file_abs) {
802
+        next if $file eq '.' || $file eq '..';
803
+        opendir my $fq_dh, $file_abs
804
+          or croak qq{Can't open directory "$file_abs": $! } . _subname;
805
+        while (my $fq_file = readdir $fq_dh) {
806
+          my $fq_file_abs = "$file_abs/$fq_file";
807
+          push @modules, "${file}::$fq_file" if -f $fq_file_abs;
808
+        }
809
+        close $fq_dh;
810
+      }
811
+      elsif(-f $file_abs) { push @modules, $file }
801 812
     }
802 813
     close $dh;
814
+    
815
+    $model_infos = [];
816
+    for my $module (@modules) {
817
+      if ($module =~ s/\.pm$//) { push @$model_infos, $module }
818
+    }
803 819
   }
804 820
   
805 821
   # Include models
... ...
@@ -817,7 +833,12 @@ sub include_model {
817 833
       $model_name  ||= $model_class;
818 834
       $model_table ||= $model_name;
819 835
     }
820
-    else { $model_class = $model_name = $model_table = $model_info }
836
+    else {
837
+      $model_class = $model_name = $model_table = $model_info;
838
+      $model_name =~ s/::/./;
839
+      $model_table =~ s/::/./;
840
+    }
841
+   
821 842
     my $mclass = "${name_space}::$model_class";
822 843
     croak qq{"$mclass" is invalid class name } . _subname
823 844
       if $mclass =~ /[^\w:]/;
... ...
@@ -3100,6 +3121,14 @@ You can get model object by C<model>.
3100 3121
   my $book_model = $dbi->model('book');
3101 3122
   my $company_model = $dbi->model('company');
3102 3123
 
3124
+You can include full-qualified table name like C<main.book>
3125
+
3126
+  lib / MyModel.pm
3127
+      / MyModel / main / book.pm
3128
+                       / company.pm
3129
+
3130
+  my $main_book = $self->model('main.book');
3131
+
3103 3132
 See L<DBIx::Custom::Model> to know model features.
3104 3133
 
3105 3134
 =head2 C<like_value>
+1 -1
t/common-sqlite-fullqualified.t
... ...
@@ -2,7 +2,7 @@ use strict;
2 2
 use warnings;
3 3
 
4 4
 use FindBin;
5
-use lib "$FindBin::Bin/common";
5
+use lib "$FindBin::Bin/common_fullqualified";
6 6
 $ENV{DBIX_CUSTOM_TEST_RUN} = 1;
7 7
 
8 8
 use DBIx::Custom;
-1
t/common.t
... ...
@@ -1325,7 +1325,6 @@ is_deeply($rows, [{$key1 => 1, $key2 => 2}], "filter");
1325 1325
 eval { $dbi->execute("drop table $table2") };
1326 1326
 $dbi->execute($create_table2);
1327 1327
 $dbi->insert({$key1 => 1, $key3 => 5}, table => $table2);
1328
-$DB::single = 1;
1329 1328
 $rows = $dbi->select(
1330 1329
   table => [$table1, $table2],
1331 1330
   column => "$table1.$key1 as " . u("${table1}_$key1")
+19
t/common_fullqualified/MyDBI1.pm
... ...
@@ -0,0 +1,19 @@
1
+package MyDBI1;
2
+
3
+use strict;
4
+use warnings;
5
+
6
+use base 'DBIx::Custom';
7
+
8
+sub connect {
9
+  my $self = shift->SUPER::connect(@_);
10
+  
11
+  $self->include_model(
12
+    MyModel1 => [
13
+      $self->table1,
14
+      $self->table2
15
+    ]
16
+  );
17
+}
18
+
19
+1;
+13
t/common_fullqualified/MyModel1/main/table1.pm
... ...
@@ -0,0 +1,13 @@
1
+package MyModel1::main::table1;
2
+
3
+use DBIx::Custom::Model -base;
4
+
5
+sub insert {
6
+  my ($self, $param) = @_;
7
+  
8
+  return $self->SUPER::insert(param => $param);
9
+}
10
+
11
+sub list { shift->select; }
12
+
13
+1;
+17
t/common_fullqualified/MyModel1/main/table2.pm
... ...
@@ -0,0 +1,17 @@
1
+package MyModel1::main::table2;
2
+
3
+use strict;
4
+use warnings;
5
+
6
+use base 'DBIx::Custom::Model';
7
+
8
+
9
+sub insert {
10
+  my ($self, $param) = @_;
11
+  
12
+  return $self->SUPER::insert(param => $param);
13
+}
14
+
15
+sub list { shift->select; }
16
+
17
+1;
+5
t/common_fullqualified/MyModel4.pm
... ...
@@ -0,0 +1,5 @@
1
+package MyModel4;
2
+
3
+use base 'DBIx::Custom::Model';
4
+
5
+1;
+9
t/common_fullqualified/MyModel4/main/table1.pm
... ...
@@ -0,0 +1,9 @@
1
+package MyModel4::main::table1;
2
+
3
+use MyModel4 -base;
4
+
5
+has table => 'main.table1';
6
+
7
+sub list { shift->select }
8
+
9
+1;
+8
t/common_fullqualified/MyModel4/main/table2.pm
... ...
@@ -0,0 +1,8 @@
1
+package MyModel4::main::table2;
2
+
3
+use base 'MyModel4';
4
+
5
+sub insert { shift->SUPER::insert(param => $_[0]) }
6
+sub list { shift->select }
7
+
8
+1;
+5
t/common_fullqualified/MyModel5.pm
... ...
@@ -0,0 +1,5 @@
1
+package MyModel5;
2
+
3
+use base 'DBIx::Custom::Model';
4
+
5
+1;
+7
t/common_fullqualified/MyModel5/main/table1.pm
... ...
@@ -0,0 +1,7 @@
1
+package MyModel5::main::table1;
2
+
3
+use MyModel5 -base;
4
+
5
+has primary_key => sub { ['key1', 'key2'] };
6
+
7
+1;
+12
t/common_fullqualified/MyModel5/main/table2.pm
... ...
@@ -0,0 +1,12 @@
1
+package MyModel5::main::table2;
2
+
3
+use strict;
4
+use warnings;
5
+
6
+use base 'MyModel5';
7
+
8
+__PACKAGE__->attr(table => 'main.table2');
9
+
10
+__PACKAGE__->attr('primary_key' => sub { ['key1', 'key2'] });
11
+
12
+1;
+13
t/common_fullqualified/MyModel5/main/table3.pm
... ...
@@ -0,0 +1,13 @@
1
+package MyModel5::main::table3;
2
+
3
+use strict;
4
+use warnings;
5
+
6
+use base 'MyModel5';
7
+
8
+__PACKAGE__->attr(name => 'main.table3');
9
+__PACKAGE__->attr(table => 'main.table3');
10
+
11
+__PACKAGE__->attr('primary_key' => sub { ['key1', 'key2'] });
12
+
13
+1;
+5
t/common_fullqualified/MyModel6.pm
... ...
@@ -0,0 +1,5 @@
1
+package MyModel6;
2
+
3
+use base 'DBIx::Custom::Model';
4
+
5
+1;
+14
t/common_fullqualified/MyModel6/main/table1.pm
... ...
@@ -0,0 +1,14 @@
1
+package MyModel6::main::table1;
2
+
3
+use base 'MyModel6';
4
+
5
+__PACKAGE__->attr(
6
+    join => sub {
7
+      [
8
+        'left outer join main.table2 on main.table1.key1 = main.table2.key1'
9
+      ]
10
+    },
11
+    primary_key => sub { ['key1'] }
12
+);
13
+
14
+1;
+5
t/common_fullqualified/MyModel6/main/table2.pm
... ...
@@ -0,0 +1,5 @@
1
+package MyModel6::main::table2;
2
+
3
+use base 'MyModel6';
4
+
5
+1;
+11
t/common_fullqualified/MyModel6/main/table3.pm
... ...
@@ -0,0 +1,11 @@
1
+package MyModel6::main::table3;
2
+
3
+use base 'MyModel6';
4
+
5
+__PACKAGE__->attr(filter => sub {
6
+  [
7
+    key1 => {in => sub { uc $_[0] }}
8
+  ]
9
+});
10
+
11
+1;
+5
t/common_fullqualified/MyModel7.pm
... ...
@@ -0,0 +1,5 @@
1
+package MyModel7;
2
+
3
+use base 'DBIx::Custom::Model';
4
+
5
+1;
+14
t/common_fullqualified/MyModel7/main/table1.pm
... ...
@@ -0,0 +1,14 @@
1
+package MyModel7::main::table1;
2
+
3
+use base 'MyModel7';
4
+
5
+__PACKAGE__->attr(
6
+  primary_key => sub { ['key1'] },
7
+  join => sub {
8
+    [
9
+      'left outer join main.table2 on main.table1.key1 = main.table2.key1'
10
+    ]
11
+  },
12
+);
13
+
14
+1;
+5
t/common_fullqualified/MyModel7/main/table2.pm
... ...
@@ -0,0 +1,5 @@
1
+package MyModel7::main::table2;
2
+
3
+use base 'MyModel7';
4
+
5
+1;
+4
t/common_fullqualified/MyModel8.pm
... ...
@@ -0,0 +1,4 @@
1
+package MyModel8;
2
+use DBIx::Custom::Model -base;
3
+
4
+1;
+7
t/common_fullqualified/MyModel8/main/table1.pm
... ...
@@ -0,0 +1,7 @@
1
+package MyModel8::main::table1;
2
+use MyModel8 -base;
3
+
4
+has join => sub { ['left join main.table2 main.table2_alias on main.table1.key1 = main.table2_alias.key1'] };
5
+
6
+
7
+1;
+10
t/common_fullqualified/MyModel8/main/table2.pm
... ...
@@ -0,0 +1,10 @@
1
+package MyModel8::main::table2;
2
+use MyModel8 -base;
3
+
4
+has filter => sub {
5
+  {
6
+    key3 => {out => sub { $_[0] * 2}, in => sub { $_[0] * 3}, end => sub { $_[0] * 4 }}
7
+  }
8
+};
9
+
10
+1;
+7 -6
t/create_fullqualified_module.pl
... ...
@@ -4,6 +4,7 @@ use warnings;
4 4
 use FindBin;
5 5
 use File::Basename qw/basename fileparse/;
6 6
 use File::Copy 'copy';
7
+use File::Path 'mkpath';
7 8
 
8 9
 my $top = $FindBin::Bin;
9 10
 my $common = "$top/common";
... ...
@@ -31,14 +32,14 @@ for my $dir (@dirs) {
31 32
         local $/;
32 33
         <$fh>;
33 34
       };
35
+
36
+      $content =~ s/::table(\d)/::main::table$1/g;
37
+      $content =~ s/([^:])table(\d)/$1main.table$2/g;
34 38
       
35
-      $content =~ s/table(\d)/TABLE$1/g;
36
-      $content =~ s/TABLE2_alias/TABLE2_ALIAS/g;
37
-      $content =~ s/key(\d)/KEY$1/g;
38
-      
39
+      mkpath "$common_fullqualified/$base_dir/main";
39 40
       my $base_name = (fileparse($file, qr/\..+$/))[0];
40
-      $base_name = uc $base_name;
41
-      my $new_file = "$common_fullqualified/$base_dir/$base_name.pm";
41
+      $base_name = $base_name;
42
+      my $new_file = "$common_fullqualified/$base_dir/main/$base_name.pm";
42 43
       
43 44
       open my $fh, '>', $new_file
44 45
         or die "Can't write file: $!";