Fix (and test) quoting with the older style of WhereJoins used by Oracle
[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
105This module was originally written to support Oracle < 9i where ANSI joins
106weren't supported at all, but became the module for Oracle >= 8 because
48580715 107Oracle's optimising of ANSI joins is horrible.
6f4ddea1 108
109=head1 SYNOPSIS
110
111Not intended for use directly; used as the sql_maker_class for schemas and components.
112
113=head1 DESCRIPTION
114
115Implements pre-ANSI joins specified in the where clause. Instead of:
116
117 SELECT x FROM y JOIN z ON y.id = z.id
118
119It will write:
120
121 SELECT x FROM y, z WHERE y.id = z.id
122
123It should properly support left joins, and right joins. Full outer joins are
124not possible due to the fact that Oracle requires the entire query be written
125to union the results of a left and right join, and by the time this module is
126called to create the where query and table definition part of the sql query,
127it's already too late.
128
129=head1 METHODS
130
131=over
132
f116ff4e 133=item select
6f4ddea1 134
f116ff4e 135Overrides DBIx::Class::SQLMaker's select() method, which calls _oracle_joins()
136to modify the column and table list before calling next::method().
6f4ddea1 137
138=back
139
140=head1 BUGS
141
f116ff4e 142Does not support full outer joins (however neither really does DBIC itself)
6f4ddea1 143
144=head1 SEE ALSO
145
146=over
147
148=item L<DBIx::Class::Storage::DBI::Oracle::WhereJoins> - Storage class using this
149
d5dedbd6 150=item L<DBIx::Class::SQLMaker> - Parent module
6f4ddea1 151
152=item L<DBIx::Class> - Duh
153
154=back
155
156=head1 AUTHOR
157
158Justin Wheeler C<< <jwheeler@datademons.com> >>
159
160=head1 CONTRIBUTORS
161
162David Jack Olrik C<< <djo@cpan.org> >>
163
164=head1 LICENSE
165
166This module is licensed under the same terms as Perl itself.
167
168=cut
169