Showing 4 changed files with 96 additions and 29 deletions
+3 -1
Changes
... ...
@@ -1,4 +1,6 @@
1
-0.1653
1
+0.1655
2
+    - remove experimental DBIx::Custom::Model relation
3
+0.1654
2 4
     - selection can contain where clause.
3 5
 0.1653
4 6
     - added experimental DBIx::Custom::Result remove_filter()
+90 -5
lib/DBIx/Custom.pm
... ...
@@ -1,6 +1,6 @@
1 1
 package DBIx::Custom;
2 2
 
3
-our $VERSION = '0.1654';
3
+our $VERSION = '0.1655';
4 4
 
5 5
 use 5.008001;
6 6
 use strict;
... ...
@@ -369,7 +369,9 @@ sub execute{
369 369
     $arg_tables = [$arg_tables]
370 370
       unless ref $arg_tables eq 'ARRAY';
371 371
     push @$tables, @$arg_tables;
372
+    
372 373
     foreach my $table (@$tables) {
374
+        next unless $table;
373 375
         $filter = {
374 376
             %$filter,
375 377
             %{$self->{filter}{out}->{$table} || {}}
... ...
@@ -409,6 +411,7 @@ sub execute{
409 411
         my $in_filter  = {};
410 412
         my $end_filter = {};
411 413
         foreach my $table (@$tables) {
414
+            next unless $table;
412 415
             $in_filter = {
413 416
                 %$in_filter,
414 417
                 %{$self->{filter}{in}{$table} || {}}
... ...
@@ -532,6 +535,22 @@ sub insert_at {
532 535
     return $self->insert(param => $param, %args);
533 536
 }
534 537
 
538
+sub insert_param {
539
+    my ($self, $param) = @_;
540
+    
541
+    my @tag;
542
+    
543
+    push @tag, '{insert_param';
544
+    
545
+    foreach my $column (keys %$param) {
546
+        push @tag, $column;
547
+    }
548
+    
549
+    push @tag, '}';
550
+    
551
+    return join ' ', @tag;
552
+}
553
+
535 554
 sub each_column {
536 555
     my ($self, $cb) = @_;
537 556
     
... ...
@@ -593,7 +612,7 @@ sub register_filter {
593 612
 sub register_tag { shift->query_builder->register_tag(@_) }
594 613
 
595 614
 our %VALID_SELECT_ARGS
596
-  = map { $_ => 1 } qw/table column where append relation filter query selection/;
615
+  = map { $_ => 1 } qw/table column where append relation filter query selection left_join/;
597 616
 
598 617
 sub select {
599 618
     my ($self, %args) = @_;
... ...
@@ -616,7 +635,11 @@ sub select {
616 635
     my $relation  = $args{relation} || {};
617 636
     my $append    = $args{append};
618 637
     my $filter    = $args{filter};
619
-
638
+    my $left_join = $args{left_join} || [];
639
+    
640
+    my @left_join_tables;
641
+    unshift @left_join_tables, $tables->[-1];
642
+    
620 643
     # Relation
621 644
     if (!$selection && keys %$relation) {
622 645
         foreach my $rcolumn (keys %$relation) {
... ...
@@ -641,11 +664,13 @@ sub select {
641 664
     
642 665
     if ($selection) {
643 666
         push @sql, $selection;
667
+        push @left_join_tables, @{$self->_tables($selection)};
644 668
     }
645 669
     else {
646 670
         # Column clause
647 671
         if (@$columns) {
648 672
             foreach my $column (@$columns) {
673
+                push @left_join_tables, @{$self->_tables($column)};
649 674
                 push @sql, ($column, ',');
650 675
             }
651 676
             pop @sql if $sql[-1] eq ',';
... ...
@@ -680,6 +705,34 @@ sub select {
680 705
     
681 706
     # String where
682 707
     my $swhere = "$w";
708
+    
709
+    # Table name in Where
710
+    unshift @left_join_tables, @{$self->_tables($swhere)};
711
+    
712
+    # Left join
713
+    if (@$left_join) {
714
+        for (my $i = 0; $i < @$left_join; $i += 2) {
715
+            my $column1 = $left_join->[$i];
716
+            my $column2 = $left_join->[$i + 1];
717
+            
718
+            my $table1 = (split (/\./, $column1))[0];
719
+            my $table2 = (split (/\./, $column2))[0];
720
+            
721
+            my $table1_exists;
722
+            my $table2_exists;
723
+            
724
+            foreach my $table (@left_join_tables) {
725
+                $table1_exists = 1 if $table eq $table1;
726
+                $table2_exists = 1 if $table eq $table2;
727
+            }
728
+            
729
+            if ($table1_exists && $table2_exists) {
730
+                push @sql, "left outer join $table2 on $column1 = $column2";
731
+            }
732
+        }
733
+    }
734
+    
735
+    # Add where
683 736
     push @sql, $swhere;
684 737
     
685 738
     # Relation
... ...
@@ -694,7 +747,7 @@ sub select {
694 747
         }
695 748
     }
696 749
     pop @sql if $sql[-1] eq 'and';
697
-    
750
+        
698 751
     # Append statement
699 752
     push @sql, $append if $append;
700 753
     
... ...
@@ -705,6 +758,8 @@ sub select {
705 758
     my $query = $self->create_query($sql);
706 759
     return $query if $args{query};
707 760
     
761
+    unshift @$tables, @left_join_tables;
762
+    
708 763
     # Execute query
709 764
     my $result = $self->execute(
710 765
         $query, param  => $where, filter => $filter,
... ...
@@ -715,7 +770,7 @@ sub select {
715 770
 
716 771
 our %VALID_SELECT_AT_ARGS
717 772
   = map { $_ => 1 } qw/table column where append relation filter query selection
718
-                       param primary_key/;
773
+                       param primary_key left_join/;
719 774
 
720 775
 sub select_at {
721 776
     my ($self, %args) = @_;
... ...
@@ -999,6 +1054,22 @@ sub update_at {
999 1054
     return $self->update(where => $where, param => $param, %args);
1000 1055
 }
1001 1056
 
1057
+sub update_param {
1058
+    my ($self, $param) = @_;
1059
+    
1060
+    my @tag;
1061
+    
1062
+    push @tag, '{update_param';
1063
+    
1064
+    foreach my $column (keys %$param) {
1065
+        push @tag, $column;
1066
+    }
1067
+    
1068
+    push @tag, '}';
1069
+    
1070
+    return join ' ', @tag;
1071
+}
1072
+
1002 1073
 sub where {
1003 1074
     my $self = shift;
1004 1075
 
... ...
@@ -1070,6 +1141,20 @@ sub _croak {
1070 1141
     }
1071 1142
 }
1072 1143
 
1144
+sub _tables {
1145
+    my ($self, $source) = @_;
1146
+    
1147
+    my $tables = [];
1148
+    
1149
+    my $safety_name = $self->safety_column_name;
1150
+    
1151
+    while ($source =~ /\b(\w+)\./g) {
1152
+        push @$tables, $1;
1153
+    }
1154
+    
1155
+    return $tables;
1156
+}
1157
+
1073 1158
 # DEPRECATED!
1074 1159
 __PACKAGE__->attr(
1075 1160
     dbi_options => sub { {} },
-3
lib/DBIx/Custom/Model.pm
... ...
@@ -15,7 +15,6 @@ __PACKAGE__->attr(
15 15
     columns => sub { [] },
16 16
     filter => sub { [] },
17 17
     primary_key => sub { [] },
18
-    relation => sub { {} }
19 18
 );
20 19
 
21 20
 our $AUTOLOAD;
... ...
@@ -117,7 +116,6 @@ sub select {
117 116
     my $self = shift;
118 117
     $self->dbi->select(
119 118
         table => $self->table,
120
-        relation => $self->relation,
121 119
         @_
122 120
     );
123 121
 }
... ...
@@ -128,7 +126,6 @@ sub select_at {
128 126
     return $self->dbi->select_at(
129 127
         table => $self->table,
130 128
         primary_key => $self->primary_key,
131
-        relation => $self->relation,
132 129
         @_
133 130
     );
134 131
 }
+3 -20
t/dbix-custom-core-sqlite.t
... ...
@@ -1300,7 +1300,7 @@ test 'selection';
1300 1300
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1301 1301
 $dbi->execute($CREATE_TABLE->{0});
1302 1302
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1303
-$result = $dbi->select(selection => '* from table1', where => {key1 => 1});
1303
+$result = $dbi->select(selection => '* from {table table1}', where => {key1 => 1});
1304 1304
 is_deeply($result->fetch_hash_all, [{key1 => 1, key2 => 2}]);
1305 1305
 
1306 1306
 test 'Model class';
... ...
@@ -1586,8 +1586,6 @@ test 'columns';
1586 1586
 use MyDBI1;
1587 1587
 $dbi = MyDBI1->connect($NEW_ARGS->{0});
1588 1588
 $model = $dbi->model('book');
1589
-$model->relation({'book.id' => 'company.id'});
1590
-is_deeply($model->relation, {'book.id' => 'company.id'});
1591 1589
 
1592 1590
 
1593 1591
 test 'model delete_at';
... ...
@@ -1654,7 +1652,7 @@ is($row->{key2}, 2);
1654 1652
 is($row->{key3}, 3);
1655 1653
 
1656 1654
 
1657
-test 'model select relation';
1655
+test 'column_clause';
1658 1656
 {
1659 1657
     package MyDBI7;
1660 1658
     
... ...
@@ -1672,21 +1670,6 @@ test 'model select relation';
1672 1670
 $dbi = MyDBI7->connect($NEW_ARGS->{0});
1673 1671
 $dbi->execute($CREATE_TABLE->{0});
1674 1672
 $dbi->execute($CREATE_TABLE->{2});
1675
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1676
-$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1677
-$result = $dbi->model('table1')->select(column => ['key3'], where => {'table1.key1' => 1});
1678
-is($result->fetch_hash_first->{key3}, 3);
1679
-$result = $dbi->model('table1')->select_at(column => ['key3'], where => [1]);
1680
-is($result->fetch_hash_first->{key3}, 3);
1681
-$dbi->execute('create table table3 (key1);');
1682
-$dbi->model('table3')->insert(param => {key1 => 'a'});
1683
-is_deeply($dbi->model('table3')->select(where => {key1 => 'a'})->fetch_hash_first,
1684
-   {key1 => 'A'});
1685
-
1686
-test 'column_clause';
1687
-$dbi = MyDBI7->connect($NEW_ARGS->{0});
1688
-$dbi->execute($CREATE_TABLE->{0});
1689
-$dbi->execute($CREATE_TABLE->{2});
1690 1673
 $dbi->setup_model;
1691 1674
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1692 1675
 $dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
... ...
@@ -1695,7 +1678,7 @@ $result = $model->select(column => $model->column_clause, where => {'table1.key1
1695 1678
 is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2});
1696 1679
 $result = $model->select(column => $model->column_clause(remove => ['key1']), where => {'table1.key1' => 1});
1697 1680
 is_deeply($result->fetch_hash_first, {key2 => 2});
1698
-$result = $model->select(column => $model->column_clause(add => ['key3']), where => {'table1.key1' => 1});
1681
+$result = $model->select(relation => {'table1.key1' => 'table2.key1'}, column => $model->column_clause(add => ['key3']), where => {'table1.key1' => 1});
1699 1682
 is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2, key3 => 3});
1700 1683
 
1701 1684