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