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