a8c4fa916ddca8a52ffd453a0e940ca047012420
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBIHacks.pm
1 package   #hide from PAUSE
2   DBIx::Class::Storage::DBIHacks;
3
4 #
5 # This module contains code that should never have seen the light of day,
6 # does not belong in the Storage, or is otherwise unfit for public
7 # display. The arrival of SQLA2 should immediately oboslete 90% of this
8 #
9
10 use strict;
11 use warnings;
12
13 use base 'DBIx::Class::Storage';
14 use mro 'c3';
15
16 use Carp::Clan qw/^DBIx::Class/;
17
18 #
19 # This is the code producing joined subqueries like:
20 # SELECT me.*, other.* FROM ( SELECT me.* FROM ... ) JOIN other ON ... 
21 #
22 sub _adjust_select_args_for_complex_prefetch {
23   my ($self, $from, $select, $where, $attrs) = @_;
24
25   $self->throw_exception ('Nothing to prefetch... how did we get here?!')
26     if not @{$attrs->{_prefetch_select}};
27
28   $self->throw_exception ('Complex prefetches are not supported on resultsets with a custom from attribute')
29     if (ref $from ne 'ARRAY' || ref $from->[0] ne 'HASH' || ref $from->[1] ne 'ARRAY');
30
31
32   # generate inner/outer attribute lists, remove stuff that doesn't apply
33   my $outer_attrs = { %$attrs };
34   delete $outer_attrs->{$_} for qw/where bind rows offset group_by having/;
35
36   my $inner_attrs = { %$attrs };
37   delete $inner_attrs->{$_} for qw/for collapse _prefetch_select _collapse_order_by select as/;
38
39
40   # bring over all non-collapse-induced order_by into the inner query (if any)
41   # the outer one will have to keep them all
42   delete $inner_attrs->{order_by};
43   if (my $ord_cnt = @{$outer_attrs->{order_by}} - @{$outer_attrs->{_collapse_order_by}} ) {
44     $inner_attrs->{order_by} = [
45       @{$outer_attrs->{order_by}}[ 0 .. $ord_cnt - 1]
46     ];
47   }
48
49
50   # generate the inner/outer select lists
51   # for inside we consider only stuff *not* brought in by the prefetch
52   # on the outside we substitute any function for its alias
53   my $outer_select = [ @$select ];
54   my $inner_select = [];
55   for my $i (0 .. ( @$outer_select - @{$outer_attrs->{_prefetch_select}} - 1) ) {
56     my $sel = $outer_select->[$i];
57
58     if (ref $sel eq 'HASH' ) {
59       $sel->{-as} ||= $attrs->{as}[$i];
60       $outer_select->[$i] = join ('.', $attrs->{alias}, ($sel->{-as} || "inner_column_$i") );
61     }
62
63     push @$inner_select, $sel;
64   }
65
66   # normalize a copy of $from, so it will be easier to work with further
67   # down (i.e. promote the initial hashref to an AoH)
68   $from = [ @$from ];
69   $from->[0] = [ $from->[0] ];
70
71   my ( $restrict_aliases, $select_aliases, $prefetch_aliases ) =
72     $self->_choose_aliases_to_include( $from, $where, $inner_select, $inner_attrs, $outer_select,
73       $outer_attrs, );
74
75
76   # construct the inner $from for the subquery
77   my %inner_joins = (map { %{$_ || {}} } ($restrict_aliases, $select_aliases) );
78   my @inner_from;
79   for my $j (@$from) {
80     push @inner_from, $j if $inner_joins{$j->[0]{-alias}};
81   }
82
83   # if a multi-type join was needed in the subquery ("multi" is indicated by
84   # presence in {collapse}) - add a group_by to simulate the collapse in the subq
85   unless ($inner_attrs->{group_by}) {
86     for my $alias (keys %inner_joins) {
87
88       # the dot comes from some weirdness in collapse
89       # remove after the rewrite
90       if ($attrs->{collapse}{".$alias"}) {
91         $inner_attrs->{group_by} ||= $inner_select;
92         last;
93       }
94     }
95   }
96
97   # demote the inner_from head
98   $inner_from[0] = $inner_from[0][0];
99
100   # generate the subquery
101   my $subq = $self->_select_args_to_query (
102     \@inner_from,
103     $inner_select,
104     $where,
105     $inner_attrs,
106   );
107
108   my $subq_joinspec = {
109     -alias => $attrs->{alias},
110     -source_handle => $inner_from[0]{-source_handle},
111     $attrs->{alias} => $subq,
112   };
113
114   # Generate the outer from - this is relatively easy (really just replace
115   # the join slot with the subquery), with a major caveat - we can not
116   # join anything that is non-selecting (not part of the prefetch), but at
117   # the same time is a multi-type relationship, as it will explode the result.
118   #
119   # There are two possibilities here
120   # - either the join is non-restricting, in which case we simply throw it away
121   # - it is part of the restrictions, in which case we need to collapse the outer
122   #   result by tackling yet another group_by to the outside of the query
123
124   # so first generate the outer_from, up to the substitution point
125   my @outer_from;
126   while (my $j = shift @$from) {
127     if ($j->[0]{-alias} eq $attrs->{alias}) { # time to swap
128       push @outer_from, [
129         $subq_joinspec,
130         @{$j}[1 .. $#$j],
131       ];
132       last; # we'll take care of what's left in $from below
133     }
134     else {
135       push @outer_from, $j;
136     }
137   }
138
139   # see what's left - throw away if not selecting/restricting
140   # also throw in a group_by if restricting to guard against
141   # cross-join explosions
142   #
143   while (my $j = shift @$from) {
144     my $alias = $j->[0]{-alias};
145
146     if ($select_aliases->{$alias} || $prefetch_aliases->{$alias}) {
147       push @outer_from, $j;
148     }
149     elsif ($restrict_aliases->{$alias}) {
150       push @outer_from, $j;
151
152       # FIXME - this should be obviated by SQLA2, as I'll be able to 
153       # have restrict_inner and restrict_outer... or something to that
154       # effect... I think...
155
156       # FIXME2 - I can't find a clean way to determine if a particular join
157       # is a multi - instead I am just treating everything as a potential
158       # explosive join (ribasushi)
159       #
160       # if (my $handle = $j->[0]{-source_handle}) {
161       #   my $rsrc = $handle->resolve;
162       #   ... need to bail out of the following if this is not a multi,
163       #       as it will be much easier on the db ...
164
165           $outer_attrs->{group_by} ||= $outer_select;
166       # }
167     }
168   }
169
170   # demote the outer_from head
171   $outer_from[0] = $outer_from[0][0];
172
173   # This is totally horrific - the $where ends up in both the inner and outer query
174   # Unfortunately not much can be done until SQLA2 introspection arrives, and even
175   # then if where conditions apply to the *right* side of the prefetch, you may have
176   # to both filter the inner select (e.g. to apply a limit) and then have to re-filter
177   # the outer select to exclude joins you didin't want in the first place
178   #
179   # OTOH it can be seen as a plus: <ash> (notes that this query would make a DBA cry ;)
180   return (\@outer_from, $outer_select, $where, $outer_attrs);
181 }
182
183 sub _choose_aliases_to_include {
184   my ( $self, $from, $where, $inner_select, $inner_attrs, $outer_select,
185     $outer_attrs ) = @_;
186
187   my %original_join_info = map { $_->[0]{-alias} => $_->[0] } (@$from);
188   # decide which parts of the join will remain in either part of
189   # the outer/inner query
190
191   # First we compose a list of which aliases are used in restrictions
192   # (i.e. conditions/order/grouping/etc). Since we do not have
193   # introspectable SQLA, we fall back to ugly scanning of raw SQL for
194   # WHERE, and for pieces of ORDER BY in order to determine which aliases
195   # need to appear in the resulting sql.
196   # It may not be very efficient, but it's a reasonable stop-gap
197   # Also unqualified column names will not be considered, but more often
198   # than not this is actually ok
199   #
200   # In the same loop we enumerate part of the selection aliases, as
201   # it requires the same sqla hack for the time being
202   my ($restrict_aliases, $select_aliases, $prefetch_aliases);
203   {
204     # produce stuff unquoted, so it can be scanned
205     my $sql_maker = $self->sql_maker;
206     local $sql_maker->{quote_char};
207     my $sep = $self->_sql_maker_opts->{name_sep} || '.';
208     $sep = "\Q$sep\E";
209
210     my $non_prefetch_select_sql = $sql_maker->_recurse_fields ($inner_select);
211     my $prefetch_select_sql = $sql_maker->_recurse_fields ($outer_attrs->{_prefetch_select});
212     my $where_sql = $sql_maker->where ($where);
213     my $group_by_sql = $sql_maker->_order_by({
214       map { $_ => $inner_attrs->{$_} } qw/group_by having/
215     });
216     my @non_prefetch_order_by_chunks = (map
217       { ref $_ ? $_->[0] : $_ }
218       $sql_maker->_order_by_chunks ($inner_attrs->{order_by})
219     );
220
221
222     for my $alias (keys %original_join_info) {
223       my $seen_re = qr/\b $alias $sep/x;
224
225       for my $piece ($where_sql, $group_by_sql, @non_prefetch_order_by_chunks ) {
226         if ($piece =~ $seen_re) {
227           $restrict_aliases->{$alias} = 1;
228         }
229       }
230
231       if ($non_prefetch_select_sql =~ $seen_re) {
232           $select_aliases->{$alias} = 1;
233       }
234
235       if ($prefetch_select_sql =~ $seen_re) {
236           $prefetch_aliases->{$alias} = 1;
237       }
238
239     }
240   }
241
242   # Add any non-left joins to the restriction list (such joins are indeed restrictions)
243   for my $j (values %original_join_info) {
244     my $alias = $j->{-alias} or next;
245     $restrict_aliases->{$alias} = 1 if (
246       (not $j->{-join_type})
247         or
248       ($j->{-join_type} !~ /^left (?: \s+ outer)? $/xi)
249     );
250   }
251
252   # mark all join parents as mentioned
253   # (e.g.  join => { cds => 'tracks' } - tracks will need to bring cds too )
254   for my $collection ($restrict_aliases, $select_aliases) {
255     for my $alias (keys %$collection) {
256       $collection->{$_} = 1
257         for (@{ $original_join_info{$alias}{-join_path} || [] });
258     }
259   }
260   return ( $restrict_aliases, $select_aliases, $prefetch_aliases );
261 }
262
263 sub _resolve_ident_sources {
264   my ($self, $ident) = @_;
265
266   my $alias2source = {};
267   my $rs_alias;
268
269   # the reason this is so contrived is that $ident may be a {from}
270   # structure, specifying multiple tables to join
271   if ( Scalar::Util::blessed($ident) && $ident->isa("DBIx::Class::ResultSource") ) {
272     # this is compat mode for insert/update/delete which do not deal with aliases
273     $alias2source->{me} = $ident;
274     $rs_alias = 'me';
275   }
276   elsif (ref $ident eq 'ARRAY') {
277
278     for (@$ident) {
279       my $tabinfo;
280       if (ref $_ eq 'HASH') {
281         $tabinfo = $_;
282         $rs_alias = $tabinfo->{-alias};
283       }
284       if (ref $_ eq 'ARRAY' and ref $_->[0] eq 'HASH') {
285         $tabinfo = $_->[0];
286       }
287
288       $alias2source->{$tabinfo->{-alias}} = $tabinfo->{-source_handle}->resolve
289         if ($tabinfo->{-source_handle});
290     }
291   }
292
293   return ($alias2source, $rs_alias);
294 }
295
296 # Takes $ident, \@column_names
297 #
298 # returns { $column_name => \%column_info, ... }
299 # also note: this adds -result_source => $rsrc to the column info
300 #
301 # If no columns_names are supplied returns info about *all* columns
302 # for all sources
303 sub _resolve_column_info {
304   my ($self, $ident, $colnames) = @_;
305   my ($alias2src, $root_alias) = $self->_resolve_ident_sources($ident);
306
307   my $sep = $self->_sql_maker_opts->{name_sep} || '.';
308   my $qsep = quotemeta $sep;
309
310   my (%return, %seen_cols, @auto_colnames);
311
312   # compile a global list of column names, to be able to properly
313   # disambiguate unqualified column names (if at all possible)
314   for my $alias (keys %$alias2src) {
315     my $rsrc = $alias2src->{$alias};
316     for my $colname ($rsrc->columns) {
317       push @{$seen_cols{$colname}}, $alias;
318       push @auto_colnames, "$alias$sep$colname" unless $colnames;
319     }
320   }
321
322   $colnames ||= [
323     @auto_colnames,
324     grep { @{$seen_cols{$_}} == 1 } (keys %seen_cols),
325   ];
326
327   COLUMN:
328   foreach my $col (@$colnames) {
329     my ($alias, $colname) = $col =~ m/^ (?: ([^$qsep]+) $qsep)? (.+) $/x;
330
331     unless ($alias) {
332       # see if the column was seen exactly once (so we know which rsrc it came from)
333       if ($seen_cols{$colname} and @{$seen_cols{$colname}} == 1) {
334         $alias = $seen_cols{$colname}[0];
335       }
336       else {
337         next COLUMN;
338       }
339     }
340
341     my $rsrc = $alias2src->{$alias};
342     $return{$col} = $rsrc && {
343       %{$rsrc->column_info($colname)},
344       -result_source => $rsrc,
345       -source_alias => $alias,
346     };
347   }
348
349   return \%return;
350 }
351
352 # The DBIC relationship chaining implementation is pretty simple - every
353 # new related_relationship is pushed onto the {from} stack, and the {select}
354 # window simply slides further in. This means that when we count somewhere
355 # in the middle, we got to make sure that everything in the join chain is an
356 # actual inner join, otherwise the count will come back with unpredictable
357 # results (a resultset may be generated with _some_ rows regardless of if
358 # the relation which the $rs currently selects has rows or not). E.g.
359 # $artist_rs->cds->count - normally generates:
360 # SELECT COUNT( * ) FROM artist me LEFT JOIN cd cds ON cds.artist = me.artistid
361 # which actually returns the number of artists * (number of cds || 1)
362 #
363 # So what we do here is crawl {from}, determine if the current alias is at
364 # the top of the stack, and if not - make sure the chain is inner-joined down
365 # to the root.
366 #
367 sub _straight_join_to_node {
368   my ($self, $from, $alias) = @_;
369
370   # subqueries and other oddness are naturally not supported
371   return $from if (
372     ref $from ne 'ARRAY'
373       ||
374     @$from <= 1
375       ||
376     ref $from->[0] ne 'HASH'
377       ||
378     ! $from->[0]{-alias}
379       ||
380     $from->[0]{-alias} eq $alias  # this last bit means $alias is the head of $from - nothing to do
381   );
382
383   # find the current $alias in the $from structure
384   my $switch_branch;
385   JOINSCAN:
386   for my $j (@{$from}[1 .. $#$from]) {
387     if ($j->[0]{-alias} eq $alias) {
388       $switch_branch = $j->[0]{-join_path};
389       last JOINSCAN;
390     }
391   }
392
393   # something else went quite wrong
394   return $from unless $switch_branch;
395
396   # So it looks like we will have to switch some stuff around.
397   # local() is useless here as we will be leaving the scope
398   # anyway, and deep cloning is just too fucking expensive
399   # So replace the first hashref in the node arrayref manually 
400   my @new_from = ($from->[0]);
401   my $sw_idx = { map { $_ => 1 } @$switch_branch };
402
403   for my $j (@{$from}[1 .. $#$from]) {
404     my $jalias = $j->[0]{-alias};
405
406     if ($sw_idx->{$jalias}) {
407       my %attrs = %{$j->[0]};
408       delete $attrs{-join_type};
409       push @new_from, [
410         \%attrs,
411         @{$j}[ 1 .. $#$j ],
412       ];
413     }
414     else {
415       push @new_from, $j;
416     }
417   }
418
419   return \@new_from;
420 }
421
422 # Most databases do not allow aliasing of tables in UPDATE/DELETE. Thus
423 # a condition containing 'me' or other table prefixes will not work
424 # at all. What this code tries to do (badly) is introspect the condition
425 # and remove all column qualifiers. If it bails out early (returns undef)
426 # the calling code should try another approach (e.g. a subquery)
427 sub _strip_cond_qualifiers {
428   my ($self, $where) = @_;
429
430   my $cond = {};
431
432   # No-op. No condition, we're updating/deleting everything
433   return $cond unless $where;
434
435   if (ref $where eq 'ARRAY') {
436     $cond = [
437       map {
438         my %hash;
439         foreach my $key (keys %{$_}) {
440           $key =~ /([^.]+)$/;
441           $hash{$1} = $_->{$key};
442         }
443         \%hash;
444       } @$where
445     ];
446   }
447   elsif (ref $where eq 'HASH') {
448     if ( (keys %$where) == 1 && ( (keys %{$where})[0] eq '-and' )) {
449       $cond->{-and} = [];
450       my @cond = @{$where->{-and}};
451        for (my $i = 0; $i < @cond; $i++) {
452         my $entry = $cond[$i];
453         my $hash;
454         if (ref $entry eq 'HASH') {
455           $hash = $self->_strip_cond_qualifiers($entry);
456         }
457         else {
458           $entry =~ /([^.]+)$/;
459           $hash->{$1} = $cond[++$i];
460         }
461         push @{$cond->{-and}}, $hash;
462       }
463     }
464     else {
465       foreach my $key (keys %$where) {
466         $key =~ /([^.]+)$/;
467         $cond->{$1} = $where->{$key};
468       }
469     }
470   }
471   else {
472     return undef;
473   }
474
475   return $cond;
476 }
477
478
479 1;