Extract two stateless DBIHacks routines into a utility package
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / SQLMaker / OracleJoins.pm
CommitLineData
8bdccca2 1package DBIx::Class::SQLMaker::OracleJoins;
6f4ddea1 2
f116ff4e 3use warnings;
4use strict;
5
ba12b23f 6use base qw( DBIx::Class::SQLMaker::Oracle );
6f4ddea1 7
8sub select {
a6b68a60 9 my ($self, $table, $fields, $where, $rs_attrs, @rest) = @_;
6f4ddea1 10
f116ff4e 11 # pull out all join conds as regular WHEREs from all extra tables
6f4ddea1 12 if (ref($table) eq 'ARRAY') {
f116ff4e 13 $where = $self->_oracle_joins($where, @{ $table }[ 1 .. $#$table ]);
6f4ddea1 14 }
15
f116ff4e 16 return $self->next::method($table, $fields, $where, $rs_attrs, @rest);
6f4ddea1 17}
18
19sub _recurse_from {
20 my ($self, $from, @join) = @_;
21
4c2b30d6 22 my @sqlf = $self->_from_chunk_to_sql($from);
6f4ddea1 23
4c2b30d6 24 for (@join) {
25 my ($to, $on) = @$_;
6f4ddea1 26
27 if (ref $to eq 'ARRAY') {
28 push (@sqlf, $self->_recurse_from(@{ $to }));
29 }
30 else {
4c2b30d6 31 push (@sqlf, $self->_from_chunk_to_sql($to));
6f4ddea1 32 }
33 }
34
35 return join q{, }, @sqlf;
36}
37
38sub _oracle_joins {
f116ff4e 39 my ($self, $where, @join) = @_;
40 my $join_where = $self->_recurse_oracle_joins(@join);
41
6f4ddea1 42 if (keys %$join_where) {
43 if (!defined($where)) {
44 $where = $join_where;
45 } else {
46 if (ref($where) eq 'ARRAY') {
47 $where = { -or => $where };
48 }
49 $where = { -and => [ $join_where, $where ] };
50 }
51 }
52 return $where;
53}
54
55sub _recurse_oracle_joins {
f116ff4e 56 my $self = shift;
6f4ddea1 57
f116ff4e 58 my @where;
59 for my $j (@_) {
6f4ddea1 60 my ($to, $on) = @{ $j };
61
f116ff4e 62 push @where, $self->_recurse_oracle_joins(@{ $to })
63 if (ref $to eq 'ARRAY');
6f4ddea1 64
f116ff4e 65 my $join_opts = ref $to eq 'ARRAY' ? $to->[0] : $to;
6f4ddea1 66 my $left_join = q{};
67 my $right_join = q{};
68
f116ff4e 69 if (ref $join_opts eq 'HASH' and my $jt = $join_opts->{-join_type}) {
6f4ddea1 70 #TODO: Support full outer joins -- this would happen much earlier in
71 #the sequence since oracle 8's full outer join syntax is best
72 #described as INSANE.
70c28808 73 $self->throw_exception("Can't handle full outer joins in Oracle 8 yet!\n")
f116ff4e 74 if $jt =~ /full/i;
6f4ddea1 75
f116ff4e 76 $left_join = q{(+)} if $jt =~ /left/i
77 && $jt !~ /inner/i;
6f4ddea1 78
f116ff4e 79 $right_join = q{(+)} if $jt =~ /right/i
80 && $jt !~ /inner/i;
6f4ddea1 81 }
82
c200d949 83 # FIXME - the code below *UTTERLY* doesn't work with custom conds... sigh
497d0451 84 # for the time being do not do any processing with the likes of
85 # normalize_sqla_condition(), instead only unroll the -and hack if present
c200d949 86 $on = $on->{-and}[0] if (
87 ref $on eq 'HASH'
88 and
89 keys %$on == 1
90 and
91 ref $on->{-and} eq 'ARRAY'
92 and
93 @{$on->{-and}} == 1
94 );
95
1d29e7e2 96
97 push @where, map { \do {
98 my ($sql) = $self->_recurse_where({
99 # FIXME - more borkage, more or less a copy of the kludge in ::SQLMaker::_join_condition()
100 $_ => ( length ref $on->{$_}
101 ? $on->{$_}
102 : { -ident => $on->{$_} }
103 )
104 });
105
106 $sql =~ s/\s*\=/$left_join =/
107 if $left_join;
108
109 "$sql$right_join";
110 }
111 } sort keys %$on;
6f4ddea1 112 }
f116ff4e 113
114 return { -and => \@where };
6f4ddea1 115}
116
1171;
118
a2bd3796 119__END__
6f4ddea1 120
121=head1 NAME
122
d5dedbd6 123DBIx::Class::SQLMaker::OracleJoins - Pre-ANSI Joins-via-Where-Clause Syntax
6f4ddea1 124
125=head1 PURPOSE
126
86b23415 127This module is used with Oracle < 9.0 due to lack of support for standard
128ANSI join syntax.
6f4ddea1 129
130=head1 SYNOPSIS
131
132Not intended for use directly; used as the sql_maker_class for schemas and components.
133
134=head1 DESCRIPTION
135
136Implements pre-ANSI joins specified in the where clause. Instead of:
137
138 SELECT x FROM y JOIN z ON y.id = z.id
139
140It will write:
141
142 SELECT x FROM y, z WHERE y.id = z.id
143
144It should properly support left joins, and right joins. Full outer joins are
145not possible due to the fact that Oracle requires the entire query be written
146to union the results of a left and right join, and by the time this module is
147called to create the where query and table definition part of the sql query,
148it's already too late.
149
150=head1 METHODS
151
152=over
153
f116ff4e 154=item select
6f4ddea1 155
f116ff4e 156Overrides DBIx::Class::SQLMaker's select() method, which calls _oracle_joins()
157to modify the column and table list before calling next::method().
6f4ddea1 158
159=back
160
161=head1 BUGS
162
f116ff4e 163Does not support full outer joins (however neither really does DBIC itself)
6f4ddea1 164
165=head1 SEE ALSO
166
167=over
168
169=item L<DBIx::Class::Storage::DBI::Oracle::WhereJoins> - Storage class using this
170
d5dedbd6 171=item L<DBIx::Class::SQLMaker> - Parent module
6f4ddea1 172
173=item L<DBIx::Class> - Duh
174
175=back
176
a2bd3796 177=head1 FURTHER QUESTIONS?
6f4ddea1 178
a2bd3796 179Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
6f4ddea1 180
a2bd3796 181=head1 COPYRIGHT AND LICENSE
6f4ddea1 182
a2bd3796 183This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
184by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
185redistribute it and/or modify it under the same terms as the
186L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.