X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FStorage%2FDBIHacks.pm;h=a54477e071b5251b90056b5a59699e8d59292d1d;hb=e92e86369d8d655c5d62940e9cbd4ed93e4c7985;hp=4481a2e0a78fbf76de0cd310fd78895bd4afd9b2;hpb=7eb76996314f77de7ab9e2f346dd14a9ccc53896;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Storage/DBIHacks.pm b/lib/DBIx/Class/Storage/DBIHacks.pm index 4481a2e..a54477e 100644 --- a/lib/DBIx/Class/Storage/DBIHacks.pm +++ b/lib/DBIx/Class/Storage/DBIHacks.pm @@ -1,4 +1,5 @@ -package DBIx::Class::Storage::DBIHacks; +package #hide from PAUSE + DBIx::Class::Storage::DBIHacks; # # This module contains code that should never have seen the light of day, @@ -287,16 +288,16 @@ sub _resolve_ident_sources { # returns { $column_name => \%column_info, ... } # also note: this adds -result_source => $rsrc to the column info # -# usage: -# my $col_sources = $self->_resolve_column_info($ident, @column_names); +# If no columns_names are supplied returns info about *all* columns +# for all sources sub _resolve_column_info { my ($self, $ident, $colnames) = @_; my ($alias2src, $root_alias) = $self->_resolve_ident_sources($ident); my $sep = $self->_sql_maker_opts->{name_sep} || '.'; - $sep = "\Q$sep\E"; + my $qsep = quotemeta $sep; - my (%return, %seen_cols); + my (%return, %seen_cols, @auto_colnames); # compile a global list of column names, to be able to properly # disambiguate unqualified column names (if at all possible) @@ -304,12 +305,18 @@ sub _resolve_column_info { my $rsrc = $alias2src->{$alias}; for my $colname ($rsrc->columns) { push @{$seen_cols{$colname}}, $alias; + push @auto_colnames, "$alias$sep$colname" unless $colnames; } } + $colnames ||= [ + @auto_colnames, + grep { @{$seen_cols{$_}} == 1 } (keys %seen_cols), + ]; + COLUMN: foreach my $col (@$colnames) { - my ($alias, $colname) = $col =~ m/^ (?: ([^$sep]+) $sep)? (.+) $/x; + my ($alias, $colname) = $col =~ m/^ (?: ([^$qsep]+) $qsep)? (.+) $/x; unless ($alias) { # see if the column was seen exactly once (so we know which rsrc it came from) @@ -402,4 +409,61 @@ sub _straight_join_to_node { return \@new_from; } +# Most databases do not allow aliasing of tables in UPDATE/DELETE. Thus +# a condition containing 'me' or other table prefixes will not work +# at all. What this code tries to do (badly) is introspect the condition +# and remove all column qualifiers. If it bails out early (returns undef) +# the calling code should try another approach (e.g. a subquery) +sub _strip_cond_qualifiers { + my ($self, $where) = @_; + + my $cond = {}; + + # No-op. No condition, we're updating/deleting everything + return $cond unless $where; + + if (ref $where eq 'ARRAY') { + $cond = [ + map { + my %hash; + foreach my $key (keys %{$_}) { + $key =~ /([^.]+)$/; + $hash{$1} = $_->{$key}; + } + \%hash; + } @$where + ]; + } + elsif (ref $where eq 'HASH') { + if ( (keys %$where) == 1 && ( (keys %{$where})[0] eq '-and' )) { + $cond->{-and} = []; + my @cond = @{$where->{-and}}; + for (my $i = 0; $i < @cond; $i++) { + my $entry = $cond[$i]; + my $hash; + if (ref $entry eq 'HASH') { + $hash = $self->_strip_cond_qualifiers($entry); + } + else { + $entry =~ /([^.]+)$/; + $hash->{$1} = $cond[++$i]; + } + push @{$cond->{-and}}, $hash; + } + } + else { + foreach my $key (keys %$where) { + $key =~ /([^.]+)$/; + $cond->{$1} = $where->{$key}; + } + } + } + else { + return undef; + } + + return $cond; +} + + 1;