X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FStorage%2FDBI%2FSybase%2FASE.pm;h=c471bf8fb2b4876b4aca614a9b714d3a2ea1a7d9;hb=b8e92dac9f2f65895700dbd0d0606f75b900a8e0;hp=280f250475f40f59c34ef3bbdf97c5d64ed0c244;hpb=5241250d8ed0c5b8e186382eb365147c9de6ffd5;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Storage/DBI/Sybase/ASE.pm b/lib/DBIx/Class/Storage/DBI/Sybase/ASE.pm index 280f250..c471bf8 100644 --- a/lib/DBIx/Class/Storage/DBI/Sybase/ASE.pm +++ b/lib/DBIx/Class/Storage/DBI/Sybase/ASE.pm @@ -256,23 +256,6 @@ sub _is_lob_column { sub _prep_for_execute { my ($self, $op, $ident, $args) = @_; - # -### This is commented out because all tests pass. However I am leaving it -### here as it may prove necessary (can't think through all combinations) -### BTW it doesn't currently work exactly - need better sensitivity to - # currently set value - # - #my ($op, $ident) = @_; - # - # inherit these from the parent for the duration of _prep_for_execute - # Don't know how to make a localizing loop with if's, otherwise I would - #local $self->{_autoinc_supplied_for_op} - # = $self->_parent_storage->_autoinc_supplied_for_op - #if ($op eq 'insert' or $op eq 'update') and $self->_parent_storage; - #local $self->{_perform_autoinc_retrieval} - # = $self->_parent_storage->_perform_autoinc_retrieval - #if ($op eq 'insert' or $op eq 'update') and $self->_parent_storage; - my $limit; # extract and use shortcut on limit without offset if ($op eq 'select' and ! $args->[4] and $limit = $args->[3]) { $args = [ @$args ]; @@ -353,10 +336,12 @@ sub insert { my $columns_info = $source->columns_info; - my $identity_col = - (first { $columns_info->{$_}{is_auto_increment} } - keys %$columns_info ) - || ''; + my ($identity_col) = grep + { $columns_info->{$_}{is_auto_increment} } + keys %$columns_info + ; + + $identity_col = '' if ! defined $identity_col; # FIXME - this is duplication from DBI.pm. When refactored towards # the LobWriter this can be folded back where it belongs. @@ -364,10 +349,10 @@ sub insert { ? 1 : 0 ; - local $self->{_perform_autoinc_retrieval} = - ($identity_col and ! exists $to_insert->{$identity_col}) - ? $identity_col - : undef + + local $self->{_perform_autoinc_retrieval} = $self->{_autoinc_supplied_for_op} + ? undef + : $identity_col ; # check for empty insert @@ -391,53 +376,42 @@ sub insert { my $blob_cols = $self->_remove_blob_cols($source, $to_insert); - # do we need the horrific SELECT MAX(COL) hack? - my $need_dumb_last_insert_id = ( - $self->_perform_autoinc_retrieval - && - ($self->_identity_method||'') ne '@@IDENTITY' - ); - - my $next = $self->next::can; - - # we are already in a transaction, or there are no blobs - # and we don't need the PK - just (try to) do it - if ($self->{transaction_depth} - || (!$blob_cols && !$need_dumb_last_insert_id) + # if a new txn is needed - it must happen on the _writer/new connection (for now) + my $guard; + if ( + ! $self->transaction_depth + and + ( + $blob_cols + or + # do we need the horrific SELECT MAX(COL) hack? + ( + $self->_perform_autoinc_retrieval + and + ( ($self->_identity_method||'') ne '@@IDENTITY' ) + ) + ) ) { - return $self->_insert ( - $next, $source, $to_insert, $blob_cols, $identity_col - ); + $self = $self->_writer_storage; + $guard = $self->txn_scope_guard; } - # otherwise use the _writer_storage to do the insert+transaction on another - # connection - my $guard = $self->_writer_storage->txn_scope_guard; - - my $updated_cols = $self->_writer_storage->_insert ( - $next, $source, $to_insert, $blob_cols, $identity_col - ); + my $updated_cols = $self->next::method ($source, $to_insert); - $self->_identity($self->_writer_storage->_identity); - - $guard->commit; - - return $updated_cols; -} - -sub _insert { - my ($self, $next, $source, $to_insert, $blob_cols, $identity_col) = @_; - - my $updated_cols = $self->$next ($source, $to_insert); - - my $final_row = { - ($identity_col ? - ($identity_col => $self->last_insert_id($source, $identity_col)) : ()), - %$to_insert, - %$updated_cols, - }; + $self->_insert_blobs ( + $source, + $blob_cols, + { + ( $identity_col + ? ( $identity_col => $self->last_insert_id($source, $identity_col) ) + : () + ), + %$to_insert, + %$updated_cols, + }, + ) if $blob_cols; - $self->_insert_blobs ($source, $blob_cols, $final_row) if $blob_cols; + $guard->commit if $guard; return $updated_cols; } @@ -535,10 +509,10 @@ sub _insert_bulk { # next::method uses a txn anyway, but it ends too early in case we need to # select max(col) to get the identity for inserting blobs. - ($self, my $guard) = $self->{transaction_depth} == 0 ? - ($self->_writer_storage, $self->_writer_storage->txn_scope_guard) - : - ($self, undef); + ($self, my $guard) = $self->transaction_depth + ? ($self, undef) + : ($self->_writer_storage, $self->_writer_storage->txn_scope_guard) + ; $self->next::method(@_); @@ -757,7 +731,7 @@ sub _update_blobs { if ( ref $where eq 'HASH' and - @primary_cols == grep { defined $where->{$_} } @primary_cols + ! grep { ! defined $where->{$_} } @primary_cols ) { my %row_to_update; @row_to_update{@primary_cols} = @{$where}{@primary_cols}; @@ -776,12 +750,10 @@ sub _update_blobs { } sub _insert_blobs { - my ($self, $source, $blob_cols, $row) = @_; - my $dbh = $self->_get_dbh; + my ($self, $source, $blob_cols, $row_data) = @_; my $table = $source->name; - my %row = %$row; my @primary_cols = try { $source->_pri_cols_or_die } catch { @@ -789,13 +761,18 @@ sub _insert_blobs { }; $self->throw_exception('Cannot update TEXT/IMAGE column(s) without primary key values') - if ((grep { defined $row{$_} } @primary_cols) != @primary_cols); + if grep { ! defined $row_data->{$_} } @primary_cols; + + # if we are 2-phase inserting a blob - there is nothing to retrieve anymore, + # regardless of the previous state of the flag + local $self->{_perform_autoinc_retrieval} + if $self->_perform_autoinc_retrieval; + + my %where = map {( $_ => $row_data->{$_} )} @primary_cols; for my $col (keys %$blob_cols) { my $blob = $blob_cols->{$col}; - my %where = map { ($_, $row{$_}) } @primary_cols; - my $cursor = $self->select ($source, [$col], \%where, {}); $cursor->next; my $sth = $cursor->sth;