DBIx/Class/SQLMaker/OracleJoins.pm

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