X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FStorage%2FDBI%2FSybase.pm;h=2e01e8e5ff594f5d17f51b833d3403aa730baa38;hb=1a58752c42ba9acf33e2943b678de4948cf3ee3f;hp=e28ee63cf0155997473ec4bc6c4098671f2c9da7;hpb=0ac07712a87c97ec1676410be95ddfef768bfe23;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Storage/DBI/Sybase.pm b/lib/DBIx/Class/Storage/DBI/Sybase.pm index e28ee63..2e01e8e 100644 --- a/lib/DBIx/Class/Storage/DBI/Sybase.pm +++ b/lib/DBIx/Class/Storage/DBI/Sybase.pm @@ -10,11 +10,18 @@ use base qw/ use mro 'c3'; use Carp::Clan qw/^DBIx::Class/; use List::Util (); +use Sub::Name (); __PACKAGE__->mk_group_accessors('simple' => - qw/_identity _blob_log_on_update insert_txn/ + qw/_identity _blob_log_on_update _writer_storage _is_writer_storage + _identity_method/ ); +my @also_proxy_to_writer_storage = qw/ + disconnect _connect_info _sql_maker _sql_maker_opts disable_sth_caching + auto_savepoint unsafe cursor_class debug debugobj schema +/; + =head1 NAME DBIx::Class::Storage::DBI::Sybase - Sybase support for DBIx::Class @@ -33,10 +40,7 @@ also enable that driver explicitly, see the documentation for more details. With this driver there is unfortunately no way to get the C without doing a C when placeholders are enabled. - -When using C transactions are -disabled. - -To turn off transactions for inserts (for an application that doesn't need -concurrency, or a loader, for example) use this setting in -L, - - on_connect_call => ['unsafe_insert'] - -To manipulate this setting at runtime, use: - - $schema->storage->insert_txn(0); # 1 to re-enable - -=cut - -sub connect_call_unsafe_insert { - my $self = shift; - $self->insert_txn(0); -} - sub _is_lob_type { my $self = shift; my $type = shift; @@ -261,7 +261,7 @@ sub _native_data_type { my ($self, $type) = @_; $type = lc $type; - $type =~ s/ identity//; + $type =~ s/\s* identity//x; return uc($TYPE_MAPPING{$type} || $type); } @@ -269,7 +269,9 @@ sub _native_data_type { sub _fetch_identity_sql { my ($self, $source, $col) = @_; - return "SELECT MAX($col) FROM ".$source->from; + return sprintf ("SELECT MAX(%s) FROM %s", + map { $self->sql_maker->_quote ($_) } ($col, $source->from) + ); } sub _execute { @@ -288,52 +290,82 @@ sub _execute { sub last_insert_id { shift->_identity } -# override to handle TEXT/IMAGE and to do a transaction if necessary +# handles TEXT/IMAGE and transaction for last_insert_id sub insert { my $self = shift; - my ($ident, $source, $to_insert) = @_; + my ($source, $to_insert) = @_; my $blob_cols = $self->_remove_blob_cols($source, $to_insert); - my $need_last_insert_id = 0; - - my ($identity_col) = - map $_->[0], - grep $_->[1]{is_auto_increment}, - map [ $_, $source->column_info($_) ], + my $identity_col = List::Util::first + { $source->column_info($_)->{is_auto_increment} } $source->columns; - $need_last_insert_id = 1 - if $identity_col && (not exists $to_insert->{$identity_col}); - - # We have to do the insert in a transaction to avoid race conditions with the - # SELECT MAX(COL) identity method used when placeholders are enabled. - my $updated_cols = do { - if ($need_last_insert_id && $self->insert_txn && - (not $self->{transaction_depth})) { - my $guard = $self->txn_scope_guard; - my $upd_cols = $self->next::method (@_); - $guard->commit; - return $upd_cols; - } - else { - $self->next::method(@_); - } + # do we need the horrific SELECT MAX(COL) hack? + my $dumb_last_insert_id = + $identity_col + && (not exists $to_insert->{$identity_col}) + && ($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 && !$dumb_last_insert_id) + ) { + return $self->_insert ( + $next, $source, $to_insert, $blob_cols, $identity_col + ); + } + + # 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 + ); + + $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 => $self->last_insert_id($source, $identity_col), + %$to_insert, + %$updated_cols, }; - $self->_insert_blobs($source, $blob_cols, $to_insert) if %$blob_cols; + $self->_insert_blobs ($source, $blob_cols, $final_row) if $blob_cols; return $updated_cols; } sub update { my $self = shift; - my ($source, $fields, $ident_cond) = @_; + my ($source, $fields, $where) = @_; my $wantarray = wantarray; - my $blob_cols = $self->_remove_blob_cols($source, $fields); + if (not $blob_cols) { + return $self->next::method(@_); + } + +# update+blob update(s) done atomically on separate connection + $self = $self->_writer_storage; + + my $guard = $self->txn_scope_guard; + my @res; if ($wantarray) { @res = $self->next::method(@_); @@ -345,11 +377,70 @@ sub update { $self->next::method(@_); } - $self->_update_blobs($source, $blob_cols, $ident_cond) if %$blob_cols; + $self->_update_blobs($source, $blob_cols, $where); + + $guard->commit; return $wantarray ? @res : $res[0]; } +### the insert_bulk stuff stolen from DBI/MSSQL.pm + +sub _set_identity_insert { + my ($self, $table) = @_; + + my $sql = sprintf ( + 'SET IDENTITY_INSERT %s ON', + $self->sql_maker->_quote ($table), + ); + + my $dbh = $self->_get_dbh; + eval { $dbh->do ($sql) }; + if ($@) { + $self->throw_exception (sprintf "Error executing '%s': %s", + $sql, + $dbh->errstr, + ); + } +} + +sub _unset_identity_insert { + my ($self, $table) = @_; + + my $sql = sprintf ( + 'SET IDENTITY_INSERT %s OFF', + $self->sql_maker->_quote ($table), + ); + + my $dbh = $self->_get_dbh; + $dbh->do ($sql); +} + +# XXX this should use the DBD::Sybase bulk API, where possible +sub insert_bulk { + my $self = shift; + my ($source, $cols, $data) = @_; + + my $is_identity_insert = (List::Util::first + { $source->column_info ($_)->{is_auto_increment} } + (@{$cols}) + ) + ? 1 + : 0; + + if ($is_identity_insert) { + $self->_set_identity_insert ($source->name); + } + + $self->next::method(@_); + + if ($is_identity_insert) { + $self->_unset_identity_insert ($source->name); + } +} + +### end of stolen insert_bulk section + sub _remove_blob_cols { my ($self, $source, $fields) = @_; @@ -362,37 +453,33 @@ sub _remove_blob_cols { } } - return \%blob_cols; + return keys %blob_cols ? \%blob_cols : undef; } sub _update_blobs { - my ($self, $source, $blob_cols, $ident_cond) = @_; + my ($self, $source, $blob_cols, $where) = @_; my (@primary_cols) = $source->primary_columns; - croak "Cannot update TEXT/IMAGE column(s) without a primary key" + $self->throw_exception('Cannot update TEXT/IMAGE column(s) without a primary key') unless @primary_cols; # check if we're updating a single row by PK my $pk_cols_in_where = 0; for my $col (@primary_cols) { - $pk_cols_in_where++ if defined $ident_cond->{$col}; + $pk_cols_in_where++ if defined $where->{$col}; } my @rows; if ($pk_cols_in_where == @primary_cols) { my %row_to_update; - @row_to_update{@primary_cols} = @{$ident_cond}{@primary_cols}; + @row_to_update{@primary_cols} = @{$where}{@primary_cols}; @rows = \%row_to_update; } else { - my $rs = $source->resultset->search( - $ident_cond, - { - result_class => 'DBIx::Class::ResultClass::HashRefInflator', - select => \@primary_cols - } - ); - @rows = $rs->all; # statement must finish + my $cursor = $self->select ($source, \@primary_cols, $where, {}); + @rows = map { + my %row; @row{@primary_cols} = @$_; \%row + } $cursor->all; } for my $row (@rows) { @@ -402,32 +489,25 @@ sub _update_blobs { sub _insert_blobs { my ($self, $source, $blob_cols, $row) = @_; - my $dbh = $self->dbh; + my $dbh = $self->_get_dbh; my $table = $source->from; my %row = %$row; my (@primary_cols) = $source->primary_columns; - croak "Cannot update TEXT/IMAGE column(s) without a primary key" + $self->throw_exception('Cannot update TEXT/IMAGE column(s) without a primary key') unless @primary_cols; - if ((grep { defined $row{$_} } @primary_cols) != @primary_cols) { - if (@primary_cols == 1) { - my $col = $primary_cols[0]; - $row{$col} = $self->last_insert_id($source, $col); - } else { - croak "Cannot update TEXT/IMAGE column(s) without primary key values"; - } - } + $self->throw_exception('Cannot update TEXT/IMAGE column(s) without primary key values') + if ((grep { defined $row{$_} } @primary_cols) != @primary_cols); for my $col (keys %$blob_cols) { my $blob = $blob_cols->{$col}; my %where = map { ($_, $row{$_}) } @primary_cols; - my $cursor = $source->resultset->search(\%where, { - select => [$col] - })->cursor; + + my $cursor = $self->select ($source, [$col], \%where, {}); $cursor->next; my $sth = $cursor->sth; @@ -454,12 +534,12 @@ sub _insert_blobs { $sth->finish if $sth; if ($exception) { if ($self->using_freetds) { - croak ( + $self->throw_exception ( 'TEXT/IMAGE operation failed, probably because you are using FreeTDS: ' . $exception ); } else { - croak $exception; + $self->throw_exception($exception); } } } @@ -518,7 +598,7 @@ sub _dbh_begin_work { my $self = shift; $self->next::method(@_); if ($self->using_freetds) { - $self->dbh->do('BEGIN TRAN'); + $self->_get_dbh->do('BEGIN TRAN'); } } @@ -543,7 +623,7 @@ sub _dbh_rollback { sub _svp_begin { my ($self, $name) = @_; - $self->dbh->do("SAVE TRANSACTION $name"); + $self->_get_dbh->do("SAVE TRANSACTION $name"); } # A new SAVE TRANSACTION with the same name releases the previous one. @@ -552,7 +632,7 @@ sub _svp_release { 1 } sub _svp_rollback { my ($self, $name) = @_; - $self->dbh->do("ROLLBACK TRANSACTION $name"); + $self->_get_dbh->do("ROLLBACK TRANSACTION $name"); } 1; @@ -601,6 +681,55 @@ Open Client libraries. Inserts or updates of TEXT/IMAGE columns will B work with FreeTDS. +=head1 INSERTS WITH PLACEHOLDERS + +With placeholders enabled, inserts are done in a transaction so that there are +no concurrency issues with getting the inserted identity value using +C as it's a +session variable. + +=head1 TRANSACTIONS + +Due to limitations of the TDS protocol, L, or both; you cannot +begin a transaction while there are active cursors. An active cursor is, for +example, a L that has been executed using +C or C but has not been exhausted or +L. + +For example, this will not work: + + $schema->txn_do(sub { + my $rs = $schema->resultset('Book'); + while (my $row = $rs->next) { + $schema->resultset('MetaData')->create({ + book_id => $row->id, + ... + }); + } + }); + +Transactions done for inserts in C mode when placeholders are in use +are not affected, as they are done on an extra database handle. + +Some workarounds: + +=over 4 + +=item * use L + +=item * L another L + +=item * load the data from your cursor with L + +=back + =head1 MAXIMUM CONNECTIONS The TDS protocol makes separate connections to the server for active statements @@ -642,7 +771,7 @@ C command on connection. See L for a L setting you need to work with C columns. -=head1 AUTHORS +=head1 AUTHOR See L.