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