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