... | ... |
@@ -1,3 +1,5 @@ |
1 |
+0.1656 |
|
2 |
+ - fixed some select() join opition bug |
|
1 | 3 |
0.1655 |
2 | 4 |
- added experimental DBIx::Custom::Model join attribute |
3 | 5 |
- added experimental select() join option |
... | ... |
@@ -1,6 +1,6 @@ |
1 | 1 |
package DBIx::Custom; |
2 | 2 |
|
3 |
-our $VERSION = '0.1655'; |
|
3 |
+our $VERSION = '0.1656'; |
|
4 | 4 |
|
5 | 5 |
use 5.008001; |
6 | 6 |
use strict; |
... | ... |
@@ -658,10 +658,6 @@ sub select { |
658 | 658 |
croak qq{"join" must be array reference} |
659 | 659 |
unless ref $join eq 'ARRAY'; |
660 | 660 |
|
661 |
- # Join tables |
|
662 |
- my @join_tables; |
|
663 |
- unshift @join_tables, $tables->[-1]; |
|
664 |
- |
|
665 | 661 |
# Add relation tables(DEPRECATED!); |
666 | 662 |
$self->_add_relation_table($tables, $args{relation}); |
667 | 663 |
|
... | ... |
@@ -670,9 +666,12 @@ sub select { |
670 | 666 |
push @sql, 'select'; |
671 | 667 |
|
672 | 668 |
# Selection |
673 |
- if ($selection) { |
|
669 |
+ if ($selection) { |
|
674 | 670 |
push @sql, $selection; |
675 |
- push @join_tables, @{$self->_tables($selection)}; |
|
671 |
+ if ($selection =~ /from\s+(?:\{table\s+)?([^\s\{]+?)\b/) { |
|
672 |
+ unshift @$tables, $1; |
|
673 |
+ } |
|
674 |
+ unshift @$tables, @{$self->_tables($selection)}; |
|
676 | 675 |
} |
677 | 676 |
|
678 | 677 |
# Column names and table name |
... | ... |
@@ -680,7 +679,7 @@ sub select { |
680 | 679 |
# Column names |
681 | 680 |
if (@$columns) { |
682 | 681 |
foreach my $column (@$columns) { |
683 |
- push @join_tables, @{$self->_tables($column)}; |
|
682 |
+ unshift @$tables, @{$self->_tables($column)}; |
|
684 | 683 |
push @sql, ($column, ','); |
685 | 684 |
} |
686 | 685 |
pop @sql if $sql[-1] eq ','; |
... | ... |
@@ -688,14 +687,21 @@ sub select { |
688 | 687 |
else { push @sql, '*' } |
689 | 688 |
|
690 | 689 |
# Table |
691 |
- croak qq{"table" option must be specified} unless @$tables; |
|
692 | 690 |
push @sql, 'from'; |
693 |
- foreach my $table (@$tables) { |
|
694 |
- push @sql, ($table, ','); |
|
691 |
+ if ($args{relation}) { |
|
692 |
+ my $found = {}; |
|
693 |
+ foreach my $table (@$tables) { |
|
694 |
+ push @sql, ($table, ',') unless $found->{$table}; |
|
695 |
+ $found->{$table} = 1; |
|
696 |
+ } |
|
695 | 697 |
} |
696 |
- pop @sql if $sql[-1] eq ','; |
|
698 |
+ else { push @sql, $tables->[-1] } |
|
699 |
+ pop @sql if ($sql[-1] || '') eq ','; |
|
697 | 700 |
} |
698 | 701 |
|
702 |
+ # Main table |
|
703 |
+ croak "Not found table name" unless $tables->[-1]; |
|
704 |
+ |
|
699 | 705 |
# Where |
700 | 706 |
my $w; |
701 | 707 |
if (ref $where eq 'HASH') { |
... | ... |
@@ -714,43 +720,11 @@ sub select { |
714 | 720 |
# String where |
715 | 721 |
my $swhere = "$w"; |
716 | 722 |
|
717 |
- # Add table names in where clause to join talbes. |
|
718 |
- unshift @join_tables, @{$self->_tables($swhere)}; |
|
723 |
+ # Add table names in where clause |
|
724 |
+ unshift @$tables, @{$self->_tables($swhere)}; |
|
719 | 725 |
|
720 |
- # Join |
|
721 |
- if (@$join) { |
|
722 |
- my $tree = {}; |
|
723 |
- |
|
724 |
- for (my $i = 0; $i < @$join; $i++) { |
|
725 |
- |
|
726 |
- my $join_clause = $join->[$i]; |
|
727 |
- |
|
728 |
- if ($join_clause =~ /\s([^\.\s]+?)\..+\s([^\.\s]+?)\./) { |
|
729 |
- |
|
730 |
- my $table1 = $1; |
|
731 |
- my $table2 = $2; |
|
732 |
- |
|
733 |
- croak qq{right side table of "$join_clause" must be uniq} |
|
734 |
- if exists $tree->{$table2}; |
|
735 |
- |
|
736 |
- $tree->{$table2} |
|
737 |
- = {position => $i, parent => $table1, join => $join_clause}; |
|
738 |
- } |
|
739 |
- else { |
|
740 |
- croak qq{join "$join_clause" must be two table name}; |
|
741 |
- } |
|
742 |
- } |
|
743 |
- |
|
744 |
- my $need_tables = {}; |
|
745 |
- $self->_need_tables($tree, $need_tables, \@join_tables); |
|
746 |
- |
|
747 |
- |
|
748 |
- my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } keys %$need_tables; |
|
749 |
- |
|
750 |
- foreach my $need_table (@need_tables) { |
|
751 |
- push @sql, $tree->{$need_table}{join}; |
|
752 |
- } |
|
753 |
- } |
|
726 |
+ # Push join |
|
727 |
+ $self->_push_join(\@sql, $join, $tables); |
|
754 | 728 |
|
755 | 729 |
# Add where clause |
756 | 730 |
push @sql, $swhere; |
... | ... |
@@ -768,8 +742,6 @@ sub select { |
768 | 742 |
my $query = $self->create_query($sql); |
769 | 743 |
return $query if $args{query}; |
770 | 744 |
|
771 |
- unshift @$tables, @join_tables; |
|
772 |
- |
|
773 | 745 |
# Execute query |
774 | 746 |
my $result = $self->execute( |
775 | 747 |
$query, param => $where, filter => $filter, |
... | ... |
@@ -1167,7 +1139,42 @@ sub _tables { |
1167 | 1139 |
return $tables; |
1168 | 1140 |
} |
1169 | 1141 |
|
1142 |
+sub _push_join { |
|
1143 |
+ my ($self, $sql, $join, $join_tables) = @_; |
|
1144 |
+ |
|
1145 |
+ return unless @$join; |
|
1146 |
+ |
|
1147 |
+ my $tree = {}; |
|
1148 |
+ |
|
1149 |
+ for (my $i = 0; $i < @$join; $i++) { |
|
1150 |
+ |
|
1151 |
+ my $join_clause = $join->[$i]; |
|
1152 |
+ |
|
1153 |
+ if ($join_clause =~ /\s([^\.\s]+?)\..+\s([^\.\s]+?)\./) { |
|
1154 |
+ |
|
1155 |
+ my $table1 = $1; |
|
1156 |
+ my $table2 = $2; |
|
1157 |
+ |
|
1158 |
+ croak qq{right side table of "$join_clause" must be uniq} |
|
1159 |
+ if exists $tree->{$table2}; |
|
1160 |
+ |
|
1161 |
+ $tree->{$table2} |
|
1162 |
+ = {position => $i, parent => $table1, join => $join_clause}; |
|
1163 |
+ } |
|
1164 |
+ else { |
|
1165 |
+ croak qq{join "$join_clause" must be two table name}; |
|
1166 |
+ } |
|
1167 |
+ } |
|
1168 |
+ |
|
1169 |
+ my $need_tables = {}; |
|
1170 |
+ $self->_need_tables($tree, $need_tables, $join_tables); |
|
1171 |
+ |
|
1172 |
+ my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } keys %$need_tables; |
|
1170 | 1173 |
|
1174 |
+ foreach my $need_table (@need_tables) { |
|
1175 |
+ push @$sql, $tree->{$need_table}{join}; |
|
1176 |
+ } |
|
1177 |
+} |
|
1171 | 1178 |
|
1172 | 1179 |
# DEPRECATED! |
1173 | 1180 |
__PACKAGE__->attr( |