X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FRow.pm;h=27138693eac2c535f39af9189b17225769d55ad5;hb=c7d50a7de2182fbfe933fa15b8015cb026f10c54;hp=af0f881431271d3967efef0d51bdbf225e626741;hpb=edc89064ca702b47da4fdab5c23315f2f726a7bb;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index af0f881..2713869 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -295,6 +295,8 @@ sub insert { $self->throw_exception("No result_source set on this object; can't insert") unless $source; + my $storage = $source->storage; + my $rollback_guard; # Check if we stored uninserted relobjs here in new() @@ -314,7 +316,7 @@ sub insert { ); # The guard will save us if we blow out of this scope via die - $rollback_guard ||= $source->storage->txn_scope_guard; + $rollback_guard ||= $storage->txn_scope_guard; MULTICREATE_DEBUG and warn "MC $self pre-reconstructing $relname $rel_obj\n"; @@ -343,51 +345,48 @@ sub insert { # start a transaction here if not started yet and there is more stuff # to insert after us if (keys %related_stuff) { - $rollback_guard ||= $source->storage->txn_scope_guard - } - - ## PK::Auto - my %auto_pri; - my $auto_idx = 0; - for ($self->primary_columns) { - if ( - not defined $self->get_column($_) - || - (ref($self->get_column($_)) eq 'SCALAR') - ) { - my $col_info = $source->column_info($_); - $auto_pri{$_} = $auto_idx++ unless $col_info->{auto_nextval}; # auto_nextval's are pre-fetched in the storage - } + $rollback_guard ||= $storage->txn_scope_guard } MULTICREATE_DEBUG and do { no warnings 'uninitialized'; warn "MC $self inserting (".join(', ', $self->get_columns).")\n"; }; - my $updated_cols = $source->storage->insert( + + my %current_rowdata = $self->get_columns; + + # perform the insert - the storage may return some stuff for us right there + # + my $returned_cols = $storage->insert( $source, - { $self->get_columns }, - (keys %auto_pri) && $source->storage->_use_insert_returning - ? { returning => [ sort { $auto_pri{$a} <=> $auto_pri{$b} } keys %auto_pri ] } - : () - , + \%current_rowdata, ); - foreach my $col (keys %$updated_cols) { - $self->store_column($col, $updated_cols->{$col}); - delete $auto_pri{$col}; + for (keys %$returned_cols) { + $self->store_column( + $_, + ( $current_rowdata{$_} = $returned_cols->{$_} ) + ); } - if (keys %auto_pri) { - my @missing = sort { $auto_pri{$a} <=> $auto_pri{$b} } keys %auto_pri; - MULTICREATE_DEBUG and warn "MC $self fetching missing PKs ".join(', ', @missing )."\n"; - my $storage = $self->result_source->storage; + # see if any of the pcols still need filling (or re-querying in case of scalarrefs) + my @missing_pri = grep + { ! defined $current_rowdata{$_} or ref $current_rowdata{$_} eq 'SCALAR' } + $self->primary_columns + ; + + if (@missing_pri) { + MULTICREATE_DEBUG and warn "MC $self fetching missing PKs ".join(', ', @missing_pri )."\n"; + $self->throw_exception( "Missing primary key but Storage doesn't support last_insert_id" ) unless $storage->can('last_insert_id'); - my @ids = $storage->last_insert_id($self->result_source, @missing); + + my @pri_values = $storage->last_insert_id($self->result_source, @missing_pri); + $self->throw_exception( "Can't get last insert id" ) - unless (@ids == @missing); - $self->store_column($missing[$_] => $ids[$_]) for 0 .. $#missing; + unless (@pri_values == @missing_pri); + + $self->store_column($missing_pri[$_] => $pri_values[$_]) for 0 .. $#missing_pri; } $self->{_dirty_columns} = {}; @@ -517,12 +516,12 @@ this method. sub update { my ($self, $upd) = @_; - my $ident_cond = $self->{_orig_ident} || $self->ident_condition; - $self->set_inflated_columns($upd) if $upd; - my %to_update = $self->get_dirty_columns; - return $self unless keys %to_update; + my %to_update = $self->get_dirty_columns + or return $self; + + my $ident_cond = $self->{_orig_ident} || $self->ident_condition; $self->throw_exception( "Not in database" ) unless $self->in_storage; $self->throw_exception($self->{_orig_ident_failreason}) @@ -798,15 +797,14 @@ See L for how to setup inflation. sub get_inflated_columns { my $self = shift; - my %loaded_colinfo = (map - { $_ => $self->column_info($_) } - (grep { $self->has_column_loaded($_) } $self->columns) - ); + my $loaded_colinfo = $self->columns_info ([ + grep { $self->has_column_loaded($_) } $self->columns + ]); my %inflated; - for my $col (keys %loaded_colinfo) { - if (exists $loaded_colinfo{$col}{accessor}) { - my $acc = $loaded_colinfo{$col}{accessor}; + for my $col (keys %$loaded_colinfo) { + if (exists $loaded_colinfo->{$col}{accessor}) { + my $acc = $loaded_colinfo->{$col}{accessor}; $inflated{$col} = $self->$acc if defined $acc; } else { @@ -882,11 +880,31 @@ sub set_column { : 1 ; - # FIXME sadly the update code just checks for keys, not for their value - $self->{_dirty_columns}{$column} = 1 if $dirty; + if ($dirty) { + # FIXME sadly the update code just checks for keys, not for their value + $self->{_dirty_columns}{$column} = 1; + + # Clear out the relation/inflation cache related to this column + # + # FIXME - this is a quick *largely incorrect* hack, pending a more + # serious rework during the merge of single and filter rels + my $rels = $self->result_source->{_relationships}; + for my $rel (keys %$rels) { - # XXX clear out the relation cache for this column - delete $self->{related_resultsets}{$column}; + my $acc = $rels->{$rel}{attrs}{accessor} || ''; + + if ( $acc eq 'single' and $rels->{$rel}{attrs}{fk_columns}{$column} ) { + delete $self->{related_resultsets}{$rel}; + delete $self->{_relationship_data}{$rel}; + #delete $self->{_inflated_column}{$rel}; + } + elsif ( $acc eq 'filter' and $rel eq $column) { + delete $self->{related_resultsets}{$rel}; + #delete $self->{_relationship_data}{$rel}; + delete $self->{_inflated_column}{$rel}; + } + } + } return $new_value; } @@ -1025,9 +1043,11 @@ sub copy { my ($self, $changes) = @_; $changes ||= {}; my $col_data = { %{$self->{_column_data}} }; + + my $colinfo = $self->columns_info([ keys %$col_data ]); foreach my $col (keys %$col_data) { delete $col_data->{$col} - if $self->result_source->column_info($col)->{is_auto_increment}; + if $colinfo->{$col}{is_auto_increment}; } my $new = { _column_data => $col_data };