From: Guillermo Roditi Date: Wed, 16 Jul 2008 20:17:07 +0000 (+0000) Subject: _is_deteministic_value X-Git-Tag: v0.08240~404^2~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=561f003c5b971a8156d1932d34dad8c05d730fb4;p=dbsrgits%2FDBIx-Class.git _is_deteministic_value --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index bcb27a4..be7d6be 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -12,6 +12,7 @@ use Storable; use DBIx::Class::ResultSetColumn; use DBIx::Class::ResultSourceHandle; use List::Util (); +use Scalar::Util (); use base qw/DBIx::Class/; __PACKAGE__->mk_group_accessors('simple' => qw/result_class _source_handle/); @@ -1527,8 +1528,18 @@ sub new_result { # precendence must be given to passed values over values inherited from the cond, # so the order here is important. - my %new = ( - %{ $self->_remove_alias($collapsed_cond, $alias) }, + my %new; + my %implied = %{$self->_remove_alias($collapsed_cond, $alias)}; + while( my($col,$value) = each %implied ){ + if(ref($value) eq 'HASH' && keys(%$value) && (keys %$value)[0] eq '='){ + $new{$col} = $value->{'='}; + next; + } + $new{$col} = $value if $self->_is_deterministic_value($value); + } + + %new = ( + %new, %{ $self->_remove_alias($values, $alias) }, -source_handle => $self->_source_handle, -result_source => $self->result_source, # DO NOT REMOVE THIS, REQUIRED @@ -1537,6 +1548,20 @@ sub new_result { return $self->result_class->new(\%new); } +# _is_deterministic_value +# +# Make an effor to strip non-deterministic values from the condition, +# to make sure new_result chokes less + +sub _is_deterministic_value { + my $self = shift; + my $value = shift; + my $ref_type = ref $value; + return 1 if $ref_type eq '' || $ref_type eq 'SCALAR'; + return 1 if Scalar::Util::blessed($value); + return 0; +} + # _collapse_cond # # Recursively collapse the condition. diff --git a/t/96_is_deteministic_value.t b/t/96_is_deteministic_value.t new file mode 100644 index 0000000..6e04252 --- /dev/null +++ b/t/96_is_deteministic_value.t @@ -0,0 +1,65 @@ +use strict; +use warnings; + +# 6 tests + +use Test::More qw(no_plan); +use lib qw(t/lib); +use DBICTest; +use DateTime; +use DateTime::Format::Strptime; +use Test::Exception; + +my $schema = DBICTest->init_schema(); +my $artist_rs = $schema->resultset('Artist'); +my $cd_rs = $schema->resultset('CD'); + + { + my $cd; + lives_ok { + $cd = $cd_rs->search({ year => {'=' => 1999}})->create + ({ + artist => {name => 'Guillermo1'}, + title => 'Guillermo 1', + }); + }; + is($cd->year, 1999); + } + + { + my $formatter = DateTime::Format::Strptime->new(pattern => '%Y'); + my $dt = DateTime->new(year => 2006, month => 06, day => 06, + formatter => $formatter ); + my $cd; + lives_ok { + $cd = $cd_rs->search({ year => $dt})->create + ({ + artist => {name => 'Guillermo2'}, + title => 'Guillermo 2', + }); + }; + is($cd->year, 2006); + } + + +{ + my $artist; + lives_ok { + $artist = $artist_rs->search({ name => {'!=' => 'Killer'}}) + ->create({artistid => undef}); + }; + is($artist->name, undef); +} + + +{ + my $artist; + lives_ok { + $artist = $artist_rs->search({ name => [ q/ some stupid names here/]}) + ->create({artistid => undef}); + }; + is($artist->name, undef); +} + + +1;