X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FStorage%2FDBI.pm;h=6f66848347eec7be5921e1a9a8c4248c43bc789a;hb=212cc5c25c31b2ec3ff4b4e20283321617db79e6;hp=6af9431ce7d9fd0ba179d6e46dd8ea5edc25d626;hpb=d7b48618e1b509c0c69b0d6e39acb007e8ea68de;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 6af9431..6f66848 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -1,10 +1,12 @@ package DBIx::Class::Storage::DBI; # -*- mode: cperl; cperl-indent-level: 2 -*- -use base 'DBIx::Class::Storage'; - use strict; use warnings; + +use base 'DBIx::Class::Storage'; +use mro 'c3'; + use Carp::Clan qw/^DBIx::Class/; use DBI; use DBIx::Class::Storage::DBI::Cursor; @@ -669,12 +671,20 @@ sub connected { $self->_verify_pid; return 0 if !$self->_dbh; } - return ($dbh->FETCH('Active') && $dbh->ping); + return ($dbh->FETCH('Active') && $self->_ping); } return 0; } +sub _ping { + my $self = shift; + + my $dbh = $self->_dbh or return 0; + + return $dbh->ping; +} + # handle pid changes correctly # NOTE: assumes $self->_dbh is a valid $dbh sub _verify_pid { @@ -1368,16 +1378,20 @@ sub _select_args_to_query { sub _select_args { my ($self, $ident, $select, $where, $attrs) = @_; + my ($alias2source, $rs_alias) = $self->_resolve_ident_sources ($ident); + my $sql_maker = $self->sql_maker; $sql_maker->{_dbic_rs_attrs} = { %$attrs, select => $select, from => $ident, where => $where, + $rs_alias + ? ( _source_handle => $alias2source->{$rs_alias}->handle ) + : () + , }; - my ($alias2source, $root_alias) = $self->_resolve_ident_sources ($ident); - # calculate bind_attrs before possible $ident mangling my $bind_attrs = {}; for my $alias (keys %$alias2source) { @@ -1388,28 +1402,41 @@ sub _select_args { $bind_attrs->{$fqcn} = $bindtypes->{$col} if $bindtypes->{$col}; # so that unqualified searches can be bound too - $bind_attrs->{$col} = $bind_attrs->{$fqcn} if $alias eq $root_alias; + $bind_attrs->{$col} = $bind_attrs->{$fqcn} if $alias eq $rs_alias; } } - my @limit; - if ($attrs->{software_limit} || - $sql_maker->_default_limit_syntax eq "GenericSubQ") { - $attrs->{software_limit} = 1; - } else { + # adjust limits + if ( + $attrs->{software_limit} + || + $sql_maker->_default_limit_syntax eq "GenericSubQ" + ) { + $attrs->{software_limit} = 1; + } + else { $self->throw_exception("rows attribute must be positive if present") if (defined($attrs->{rows}) && !($attrs->{rows} > 0)); # MySQL actually recommends this approach. I cringe. $attrs->{rows} = 2**48 if not defined $attrs->{rows} and defined $attrs->{offset}; + } - if ($attrs->{rows} && keys %{$attrs->{collapse}}) { - ($ident, $select, $where, $attrs) - = $self->_adjust_select_args_for_limited_prefetch ($ident, $select, $where, $attrs); - } - else { - push @limit, $attrs->{rows}, $attrs->{offset}; - } + my @limit; + + # see if we need to tear the prefetch apart (either limited has_many or grouped prefetch) + # otherwise delegate the limiting to the storage, unless software limit was requested + if ( + ( $attrs->{rows} && keys %{$attrs->{collapse}} ) + || + ( $attrs->{group_by} && @{$attrs->{group_by}} && + $attrs->{prefetch_select} && @{$attrs->{prefetch_select}} ) + ) { + ($ident, $select, $where, $attrs) + = $self->_adjust_select_args_for_complex_prefetch ($ident, $select, $where, $attrs); + } + elsif (! $attrs->{software_limit} ) { + push @limit, $attrs->{rows}, $attrs->{offset}; } ### @@ -1424,45 +1451,61 @@ sub _select_args { my $order = { map { $attrs->{$_} ? ( $_ => $attrs->{$_} ) : () } - (qw/order_by group_by having _virtual_order_by/ ) + (qw/order_by group_by having/ ) }; - return ('select', $attrs->{bind}, $ident, $bind_attrs, $select, $where, $order, @limit); } -sub _adjust_select_args_for_limited_prefetch { +# +# This is the code producing joined subqueries like: +# SELECT me.*, other.* FROM ( SELECT me.* FROM ... ) JOIN other ON ... +# +sub _adjust_select_args_for_complex_prefetch { my ($self, $from, $select, $where, $attrs) = @_; - if ($attrs->{group_by} && @{$attrs->{group_by}}) { - $self->throw_exception ('has_many prefetch with limit (rows/offset) is not supported on grouped resultsets'); - } - - $self->throw_exception ('has_many prefetch with limit (rows/offset) is not supported on resultsets with a custom from attribute') + $self->throw_exception ('Complex prefetches are not supported on resultsets with a custom from attribute') if (ref $from ne 'ARRAY'); + # copies for mangling + $from = [ @$from ]; + $select = [ @$select ]; + $attrs = { %$attrs }; # separate attributes my $sub_attrs = { %$attrs }; - delete $attrs->{$_} for qw/where bind rows offset/; - delete $sub_attrs->{$_} for qw/for collapse select as order_by/; + delete $attrs->{$_} for qw/where bind rows offset group_by having/; + delete $sub_attrs->{$_} for qw/for collapse prefetch_select _collapse_order_by select as/; my $alias = $attrs->{alias}; + my $sql_maker = $self->sql_maker; - # create subquery select list - my $sub_select = [ grep { $_ =~ /^$alias\./ } @{$attrs->{select}} ]; + # create subquery select list - loop only over primary columns + my $sub_select = []; + for my $i (0 .. @{$attrs->{select}} - @{$attrs->{prefetch_select}} - 1) { + my $sel = $attrs->{select}[$i]; + + # alias any functions to the dbic-side 'as' label + # adjust the outer select accordingly + if (ref $sel eq 'HASH' && !$sel->{-select}) { + $sel = { -select => $sel, -as => $attrs->{as}[$i] }; + $select->[$i] = join ('.', $attrs->{alias}, $attrs->{as}[$i]); + } + + push @$sub_select, $sel; + } # bring over all non-collapse-induced order_by into the inner query (if any) # the outer one will have to keep them all + delete $sub_attrs->{order_by}; if (my $ord_cnt = @{$attrs->{order_by}} - @{$attrs->{_collapse_order_by}} ) { $sub_attrs->{order_by} = [ - @{$attrs->{order_by}}[ 0 .. ($#{$attrs->{order_by}} - $ord_cnt - 1) ] + @{$attrs->{order_by}}[ 0 .. $ord_cnt - 1] ]; } # mangle {from} - $from = [ @$from ]; - my $select_root = shift @$from; + my $join_root = shift @$from; my @outer_from = @$from; my %inner_joins; @@ -1472,7 +1515,7 @@ sub _adjust_select_args_for_limited_prefetch { # so always include it in the inner join, and also shift away # from the outer stack, so that the two datasets actually do # meet - if ($select_root->{-alias} ne $alias) { + if ($join_root->{-alias} ne $alias) { $inner_joins{$alias} = 1; while (@outer_from && $outer_from[0][0]{-alias} ne $alias) { @@ -1493,7 +1536,7 @@ sub _adjust_select_args_for_limited_prefetch { # away _any_ branches of the join tree that are: # 1) not mentioned in the condition/order # 2) left-join leaves (or left-join leaf chains) - # Most of the join ocnditions will not satisfy this, but for real + # Most of the join conditions will not satisfy this, but for real # complex queries some might, and we might make some RDBMS happy. # # @@ -1503,7 +1546,6 @@ sub _adjust_select_args_for_limited_prefetch { # It may not be very efficient, but it's a reasonable stop-gap { # produce stuff unquoted, so it can be scanned - my $sql_maker = $self->sql_maker; local $sql_maker->{quote_char}; my @order_by = (map @@ -1543,20 +1585,19 @@ sub _adjust_select_args_for_limited_prefetch { } # construct the inner $from for the subquery - my $inner_from = [ $select_root ]; + my $inner_from = [ $join_root ]; for my $j (@$from) { push @$inner_from, $j if $inner_joins{$j->[0]{-alias}}; } # if a multi-type join was needed in the subquery ("multi" is indicated by # presence in {collapse}) - add a group_by to simulate the collapse in the subq - for my $alias (keys %inner_joins) { # the dot comes from some weirdness in collapse # remove after the rewrite if ($attrs->{collapse}{".$alias"}) { - $sub_attrs->{group_by} = $sub_select; + $sub_attrs->{group_by} ||= $sub_select; last; } } @@ -1572,7 +1613,7 @@ sub _adjust_select_args_for_limited_prefetch { # put it in the new {from} unshift @outer_from, { -alias => $alias, - -source_handle => $select_root->{-source_handle}, + -source_handle => $join_root->{-source_handle}, $alias => $subq, }; @@ -1590,14 +1631,14 @@ sub _resolve_ident_sources { my ($self, $ident) = @_; my $alias2source = {}; - my $root_alias; + my $rs_alias; # the reason this is so contrived is that $ident may be a {from} # structure, specifying multiple tables to join if ( Scalar::Util::blessed($ident) && $ident->isa("DBIx::Class::ResultSource") ) { # this is compat mode for insert/update/delete which do not deal with aliases $alias2source->{me} = $ident; - $root_alias = 'me'; + $rs_alias = 'me'; } elsif (ref $ident eq 'ARRAY') { @@ -1605,7 +1646,7 @@ sub _resolve_ident_sources { my $tabinfo; if (ref $_ eq 'HASH') { $tabinfo = $_; - $root_alias = $tabinfo->{-alias}; + $rs_alias = $tabinfo->{-alias}; } if (ref $_ eq 'ARRAY' and ref $_->[0] eq 'HASH') { $tabinfo = $_->[0]; @@ -1616,7 +1657,7 @@ sub _resolve_ident_sources { } } - return ($alias2source, $root_alias); + return ($alias2source, $rs_alias); } # Takes $ident, \@column_names