Commit | Line | Data |
6f4ddea1 |
1 | package # Hide from PAUSE |
d5dedbd6 |
2 | DBIx::Class::SQLMaker::OracleJoins; |
6f4ddea1 |
3 | |
ba12b23f |
4 | use base qw( DBIx::Class::SQLMaker::Oracle ); |
87aa29e2 |
5 | use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/; |
9c1700e3 |
6 | use namespace::clean; |
6f4ddea1 |
7 | |
8 | sub select { |
a6b68a60 |
9 | my ($self, $table, $fields, $where, $rs_attrs, @rest) = @_; |
6f4ddea1 |
10 | |
11 | if (ref($table) eq 'ARRAY') { |
12 | $where = $self->_oracle_joins($where, @{ $table }); |
13 | } |
14 | |
a6b68a60 |
15 | return $self->SUPER::select($table, $fields, $where, $rs_attrs, @rest); |
6f4ddea1 |
16 | } |
17 | |
18 | sub _recurse_from { |
19 | my ($self, $from, @join) = @_; |
20 | |
4c2b30d6 |
21 | my @sqlf = $self->_from_chunk_to_sql($from); |
6f4ddea1 |
22 | |
4c2b30d6 |
23 | for (@join) { |
24 | my ($to, $on) = @$_; |
6f4ddea1 |
25 | |
26 | if (ref $to eq 'ARRAY') { |
27 | push (@sqlf, $self->_recurse_from(@{ $to })); |
28 | } |
29 | else { |
4c2b30d6 |
30 | push (@sqlf, $self->_from_chunk_to_sql($to)); |
6f4ddea1 |
31 | } |
32 | } |
33 | |
34 | return join q{, }, @sqlf; |
35 | } |
36 | |
37 | sub _oracle_joins { |
38 | my ($self, $where, $from, @join) = @_; |
39 | my $join_where = {}; |
40 | $self->_recurse_oracle_joins($join_where, $from, @join); |
41 | if (keys %$join_where) { |
42 | if (!defined($where)) { |
43 | $where = $join_where; |
44 | } else { |
45 | if (ref($where) eq 'ARRAY') { |
46 | $where = { -or => $where }; |
47 | } |
48 | $where = { -and => [ $join_where, $where ] }; |
49 | } |
50 | } |
51 | return $where; |
52 | } |
53 | |
54 | sub _recurse_oracle_joins { |
55 | my ($self, $where, $from, @join) = @_; |
56 | |
57 | foreach my $j (@join) { |
58 | my ($to, $on) = @{ $j }; |
59 | |
60 | if (ref $to eq 'ARRAY') { |
61 | $self->_recurse_oracle_joins($where, @{ $to }); |
62 | } |
63 | |
64 | my $to_jt = ref $to eq 'ARRAY' ? $to->[0] : $to; |
65 | my $left_join = q{}; |
66 | my $right_join = q{}; |
67 | |
68 | if (ref $to_jt eq 'HASH' and exists $to_jt->{-join_type}) { |
69 | #TODO: Support full outer joins -- this would happen much earlier in |
70 | #the sequence since oracle 8's full outer join syntax is best |
71 | #described as INSANE. |
e8fcf76f |
72 | croak "Can't handle full outer joins in Oracle 8 yet!\n" |
6f4ddea1 |
73 | if $to_jt->{-join_type} =~ /full/i; |
74 | |
75 | $left_join = q{(+)} if $to_jt->{-join_type} =~ /left/i |
76 | && $to_jt->{-join_type} !~ /inner/i; |
77 | |
78 | $right_join = q{(+)} if $to_jt->{-join_type} =~ /right/i |
79 | && $to_jt->{-join_type} !~ /inner/i; |
80 | } |
81 | |
82 | foreach my $lhs (keys %{ $on }) { |
83 | $where->{$lhs . $left_join} = \"= $on->{ $lhs }$right_join"; |
84 | } |
85 | } |
86 | } |
87 | |
88 | 1; |
89 | |
90 | =pod |
91 | |
92 | =head1 NAME |
93 | |
d5dedbd6 |
94 | DBIx::Class::SQLMaker::OracleJoins - Pre-ANSI Joins-via-Where-Clause Syntax |
6f4ddea1 |
95 | |
96 | =head1 PURPOSE |
97 | |
98 | This module was originally written to support Oracle < 9i where ANSI joins |
99 | weren't supported at all, but became the module for Oracle >= 8 because |
48580715 |
100 | Oracle's optimising of ANSI joins is horrible. |
6f4ddea1 |
101 | |
102 | =head1 SYNOPSIS |
103 | |
104 | Not intended for use directly; used as the sql_maker_class for schemas and components. |
105 | |
106 | =head1 DESCRIPTION |
107 | |
108 | Implements pre-ANSI joins specified in the where clause. Instead of: |
109 | |
110 | SELECT x FROM y JOIN z ON y.id = z.id |
111 | |
112 | It will write: |
113 | |
114 | SELECT x FROM y, z WHERE y.id = z.id |
115 | |
116 | It should properly support left joins, and right joins. Full outer joins are |
117 | not possible due to the fact that Oracle requires the entire query be written |
118 | to union the results of a left and right join, and by the time this module is |
119 | called to create the where query and table definition part of the sql query, |
120 | it's already too late. |
121 | |
122 | =head1 METHODS |
123 | |
124 | =over |
125 | |
126 | =item select ($\@$;$$@) |
127 | |
d5dedbd6 |
128 | Replaces DBIx::Class::SQLMaker's select() method, which calls _oracle_joins() |
6f4ddea1 |
129 | to modify the column and table list before calling SUPER::select(). |
130 | |
131 | =item _recurse_from ($$\@) |
132 | |
133 | Recursive subroutine that builds the table list. |
134 | |
135 | =item _oracle_joins ($$$@) |
136 | |
137 | Creates the left/right relationship in the where query. |
138 | |
139 | =back |
140 | |
141 | =head1 BUGS |
142 | |
143 | Does not support full outer joins. |
144 | Probably lots more. |
145 | |
146 | =head1 SEE ALSO |
147 | |
148 | =over |
149 | |
150 | =item L<DBIx::Class::Storage::DBI::Oracle::WhereJoins> - Storage class using this |
151 | |
d5dedbd6 |
152 | =item L<DBIx::Class::SQLMaker> - Parent module |
6f4ddea1 |
153 | |
154 | =item L<DBIx::Class> - Duh |
155 | |
156 | =back |
157 | |
158 | =head1 AUTHOR |
159 | |
160 | Justin Wheeler C<< <jwheeler@datademons.com> >> |
161 | |
162 | =head1 CONTRIBUTORS |
163 | |
164 | David Jack Olrik C<< <djo@cpan.org> >> |
165 | |
166 | =head1 LICENSE |
167 | |
168 | This module is licensed under the same terms as Perl itself. |
169 | |
170 | =cut |
171 | |