X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FStorage%2FDBI%2FSybase.pm;h=eeb4f01edf2a3806495f10925641a7d166d036b6;hb=6862815907d42aa2f5fcc70afd4ff2dbc9b48f79;hp=219ca56fe5d305cca599f33a0e234c2840b8a0fa;hpb=905e6f073b89facb367cdedecd42ddbc5d770d61;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Storage/DBI/Sybase.pm b/lib/DBIx/Class/Storage/DBI/Sybase.pm index 219ca56..eeb4f01 100644 --- a/lib/DBIx/Class/Storage/DBI/Sybase.pm +++ b/lib/DBIx/Class/Storage/DBI/Sybase.pm @@ -9,12 +9,13 @@ use base qw/ /; use mro 'c3'; use Carp::Clan qw/^DBIx::Class/; -use List::Util (); -use Sub::Name (); +use List::Util(); +use Sub::Name(); +use Data::Dumper::Concise(); __PACKAGE__->mk_group_accessors('simple' => qw/_identity _blob_log_on_update _writer_storage _is_extra_storage - _bulk_storage _is_bulk_storage _began_bulk_work + _bulk_storage _is_bulk_storage _began_bulk_work _bulk_disabled_due_to_coderef_connect_info_warned _identity_method/ ); @@ -145,7 +146,7 @@ sub _init { # this is why $bulk_storage->_dbi_connect_info->[0] .= ';bulkLogin=1'; - + $self->_bulk_storage($bulk_storage); } @@ -253,41 +254,47 @@ sub _prep_for_execute { my ($sql, $bind) = $self->next::method (@_); - if ($op eq 'insert') { - my $table = $ident->from; + my $table = Scalar::Util::blessed($ident) ? $ident->from : $ident; - my $bind_info = $self->_resolve_column_info( - $ident, [map $_->[0], @{$bind}] + my $bind_info = $self->_resolve_column_info( + $ident, [map $_->[0], @{$bind}] + ); + my $bound_identity_col = List::Util::first + { $bind_info->{$_}{is_auto_increment} } + (keys %$bind_info) + ; + my $identity_col = Scalar::Util::blessed($ident) && + List::Util::first + { $ident->column_info($_)->{is_auto_increment} } + $ident->columns + ; + + if (($op eq 'insert' && $bound_identity_col) || + ($op eq 'update' && exists $args->[0]{$identity_col})) { + $sql = join ("\n", + $self->_set_table_identity_sql($op => $table, 'on'), + $sql, + $self->_set_table_identity_sql($op => $table, 'off'), ); - my $identity_col = List::Util::first - { $bind_info->{$_}{is_auto_increment} } - (keys %$bind_info) - ; - - if ($identity_col) { - $sql = join ("\n", - "SET IDENTITY_INSERT $table ON", - $sql, - "SET IDENTITY_INSERT $table OFF", - ); - } - else { - $identity_col = List::Util::first - { $ident->column_info($_)->{is_auto_increment} } - $ident->columns - ; - } + } - if ($identity_col) { - $sql = - "$sql\n" . - $self->_fetch_identity_sql($ident, $identity_col); - } + if ($op eq 'insert' && (not $bound_identity_col) && $identity_col && + (not $self->{insert_bulk})) { + $sql = + "$sql\n" . + $self->_fetch_identity_sql($ident, $identity_col); } return ($sql, $bind); } +sub _set_table_identity_sql { + my ($self, $op, $table, $on_off) = @_; + + return sprintf 'SET IDENTITY_%s %s %s', + uc($op), $self->sql_maker->_quote($table), uc($on_off); +} + # Stolen from SQLT, with some modifications. This is a makeshift # solution before a sane type-mapping library is available, thus # the 'our' for easy overrides. @@ -347,11 +354,21 @@ sub insert { my $self = shift; my ($source, $to_insert) = @_; - my $blob_cols = $self->_remove_blob_cols($source, $to_insert); - - my $identity_col = List::Util::first + my $identity_col = (List::Util::first { $source->column_info($_)->{is_auto_increment} } - $source->columns; + $source->columns) || ''; + + # check for empty insert + # INSERT INTO foo DEFAULT VALUES -- does not work with Sybase + # try to insert explicit 'DEFAULT's instead (except for identity) + if (not %$to_insert) { + for my $col ($source->columns) { + next if $col eq $identity_col; + $to_insert->{$col} = \'DEFAULT'; + } + } + + my $blob_cols = $self->_remove_blob_cols($source, $to_insert); # do we need the horrific SELECT MAX(COL) hack? my $dumb_last_insert_id = @@ -392,7 +409,8 @@ sub _insert { my $updated_cols = $self->$next ($source, $to_insert); my $final_row = { - $identity_col => $self->last_insert_id($source, $identity_col), + ($identity_col ? + ($identity_col => $self->last_insert_id($source, $identity_col)) : ()), %$to_insert, %$updated_cols, }; @@ -418,19 +436,11 @@ sub update { my $is_identity_update = $identity_col && defined $fields->{$identity_col}; - if (not $blob_cols) { - $self->_set_identity_insert($table, 'update') if $is_identity_update; - return $self->next::method(@_); - $self->_unset_identity_insert($table, 'update') if $is_identity_update; - } + return $self->next::method(@_) unless $blob_cols; -# check that we're not updating a blob column that's also in $where - for my $blob (grep $self->_is_lob_column($source, $_), $source->columns) { - if (exists $where->{$blob} && exists $fields->{$blob}) { - croak -'Update of TEXT/IMAGE column that is also in search condition impossible'; - } - } +# If there are any blobs in $where, Sybase will return a descriptive error +# message. +# XXX blobs can still be used with a LIKE query, and this should be handled. # update+blob update(s) done atomically on separate connection $self = $self->_writer_storage; @@ -441,6 +451,8 @@ sub update { # it is originally put by _remove_blob_cols .) my %blobs_to_empty = map { ($_ => delete $fields->{$_}) } keys %$blob_cols; +# We can't only update NULL blobs, because blobs cannot be in the WHERE clause. + $self->next::method($source, \%blobs_to_empty, $where, @rest); # Now update the blobs before the other columns in case the update of other @@ -449,8 +461,6 @@ sub update { my @res; if (%$fields) { - $self->_set_identity_insert($table, 'update') if $is_identity_update; - if ($wantarray) { @res = $self->next::method(@_); } @@ -460,8 +470,6 @@ sub update { else { $self->next::method(@_); } - - $self->_unset_identity_insert($table, 'update') if $is_identity_update; } $guard->commit; @@ -469,53 +477,6 @@ sub update { return $wantarray ? @res : $res[0]; } -### the insert_bulk partially stolen from DBI/MSSQL.pm - -sub _set_identity_insert { - my ($self, $table, $op) = @_; - - my $sql = sprintf ( - 'SET IDENTITY_%s %s ON', - (uc($op) || 'INSERT'), - $self->sql_maker->_quote ($table), - ); - - $self->_query_start($sql); - - my $dbh = $self->_get_dbh; - eval { $dbh->do ($sql) }; - my $exception = $@; - - $self->_query_end($sql); - - if ($exception) { - $self->throw_exception (sprintf "Error executing '%s': %s", - $sql, - $dbh->errstr, - ); - } -} - -sub _unset_identity_insert { - my ($self, $table, $op) = @_; - - my $sql = sprintf ( - 'SET IDENTITY_%s %s OFF', - (uc($op) || 'INSERT'), - $self->sql_maker->_quote ($table), - ); - - $self->_query_start($sql); - - my $dbh = $self->_get_dbh; - $dbh->do ($sql); - - $self->_query_end($sql); -} - -# for tests -sub _can_insert_bulk { 1 } - sub insert_bulk { my $self = shift; my ($source, $cols, $data) = @_; @@ -525,14 +486,14 @@ sub insert_bulk { $source->columns; my $is_identity_insert = (List::Util::first - { $source->column_info ($_)->{is_auto_increment} } + { $_ eq $identity_col } @{$cols} ) ? 1 : 0; my @source_columns = $source->columns; my $use_bulk_api = - $self->_bulk_storage && + $self->_bulk_storage && $self->_get_dbh->{syb_has_blk}; if ((not $use_bulk_api) && @@ -540,7 +501,7 @@ sub insert_bulk { (not $self->_bulk_disabled_due_to_coderef_connect_info_warned)) { carp <<'EOF'; Bulk API support disabled due to use of a CODEREF connect_info. Reverting to -array inserts. +regular array inserts. EOF $self->_bulk_disabled_due_to_coderef_connect_info_warned(1); } @@ -548,24 +509,16 @@ EOF if (not $use_bulk_api) { my $blob_cols = $self->_remove_blob_cols_array($source, $cols, $data); - my $dumb_last_insert_id = - $identity_col - && (not $is_identity_insert) - && ($self->_identity_method||'') ne '@@IDENTITY'; +# _execute_array 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)) = do { - if ($self->{transaction_depth} == 0 && - ($blob_cols || $dumb_last_insert_id)) { - ($self->_writer_storage, $self->_writer_storage->txn_scope_guard); - } - else { - ($self, undef); - } - }; + local $self->{insert_bulk} = 1; - $self->_set_identity_insert ($source->name) if $is_identity_insert; $self->next::method(@_); - $self->_unset_identity_insert ($source->name) if $is_identity_insert; if ($blob_cols) { if ($is_identity_insert) { @@ -592,6 +545,7 @@ EOF } $guard->commit if $guard; + return; } @@ -629,12 +583,12 @@ EOF return 1 if $errno == 36; - carp + carp "Layer: $layer, Origin: $origin, Severity: $severity, Error: $errno" . ($errmsg ? "\n$errmsg" : '') . ($osmsg ? "\n$osmsg" : '') . ($blkmsg ? "\n$blkmsg" : ''); - + return 0; }); @@ -648,7 +602,7 @@ EOF # $bulk->next::method($source, \@source_columns, \@new_data, { # syb_bcp_attribs => { # identity_flag => $is_identity_insert, -# identity_column => $identity_idx, +# identity_column => $identity_idx, # } # }); my $sql = 'INSERT INTO ' . @@ -665,58 +619,28 @@ EOF { syb_bcp_attribs => { identity_flag => $is_identity_insert, - identity_column => $identity_idx, + identity_column => $identity_idx, } } ); - my $bind_attributes = $self->source_bind_attributes($source); - - foreach my $slice_idx (0..$#source_columns) { - my $col = $source_columns[$slice_idx]; - - my $attributes = $bind_attributes->{$col} - if $bind_attributes && defined $bind_attributes->{$col}; - - my @slice = map $_->[$slice_idx], @new_data; - - $sth->bind_param_array(($slice_idx + 1), \@slice, $attributes); - } - - $bulk->_query_start($sql); - -# this is stolen from DBI::insert_bulk - my $tuple_status = []; - my $rv = eval { $sth->execute_array({ArrayTupleStatus => $tuple_status}) }; - - if (my $err = $@ || $sth->errstr) { - my $i = 0; - ++$i while $i <= $#$tuple_status && !ref $tuple_status->[$i]; - - $self->throw_exception("Unexpected populate error: $err") - if ($i > $#$tuple_status); - - require Data::Dumper; - local $Data::Dumper::Terse = 1; - local $Data::Dumper::Indent = 1; - local $Data::Dumper::Useqq = 1; - local $Data::Dumper::Quotekeys = 0; - local $Data::Dumper::Sortkeys = 1; - - $self->throw_exception(sprintf "%s for populate slice:\n%s", - ($tuple_status->[$i][1] || $err), - Data::Dumper::Dumper( - { map { $source_columns[$_] => $new_data[$i][$_] } (0 .. $#$cols) } - ), - ); - } + my @bind = do { + my $idx = 0; + map [ $_, $idx++ ], @source_columns; + }; - $guard->commit; - $sth->finish; + $self->_execute_array( + $source, $sth, \@bind, \@source_columns, \@new_data, sub { + $guard->commit + } + ); $bulk->_query_end($sql); }; + my $exception = $@; + DBD::Sybase::set_cslib_cb($orig_cslib_cb); + if ($exception =~ /-Y option/) { carp <<"EOF"; @@ -725,21 +649,26 @@ to regular array inserts: *** Try unsetting the LANG environment variable. -$@ +$exception EOF $self->_bulk_storage(undef); - DBD::Sybase::set_cslib_cb($orig_cslib_cb); unshift @_, $self; goto \&insert_bulk; } elsif ($exception) { - DBD::Sybase::set_cslib_cb($orig_cslib_cb); # rollback makes the bulkLogin connection unusable $self->_bulk_storage->disconnect; $self->throw_exception($exception); } +} - DBD::Sybase::set_cslib_cb($orig_cslib_cb); +sub _dbh_execute_array { + my ($self, $sth, $tuple_status, $cb) = @_; + + my $rv = $self->next::method($sth, $tuple_status); + $cb->() if $cb; + + return $rv; } # Make sure blobs are not bound as placeholders, and return any non-empty ones @@ -750,7 +679,7 @@ sub _remove_blob_cols { my %blob_cols; for my $col (keys %$fields) { - if ($self->_is_lob_type($source->column_info($col)->{data_type})) { + if ($self->_is_lob_column($source, $col)) { my $blob_val = delete $fields->{$col}; if (not defined $blob_val) { $fields->{$col} = \'NULL'; @@ -762,7 +691,7 @@ sub _remove_blob_cols { } } - return keys %blob_cols ? \%blob_cols : undef; + return %blob_cols ? \%blob_cols : undef; } # same for insert_bulk @@ -774,7 +703,7 @@ sub _remove_blob_cols_array { for my $i (0..$#$cols) { my $col = $cols->[$i]; - if ($self->_is_lob_type($source->column_info($col)->{data_type})) { + if ($self->_is_lob_column($source, $col)) { for my $j (0..$#$data) { my $blob_val = delete $data->[$j][$i]; if (not defined $blob_val) { @@ -797,7 +726,7 @@ sub _update_blobs { 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 @@ -832,12 +761,11 @@ sub _insert_blobs { 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) { - 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}; @@ -849,15 +777,11 @@ sub _insert_blobs { my $sth = $cursor->sth; if (not $sth) { - require Data::Dumper; - local $Data::Dumper::Terse = 1; - local $Data::Dumper::Indent = 1; - local $Data::Dumper::Useqq = 1; - local $Data::Dumper::Quotekeys = 0; - local $Data::Dumper::Sortkeys = 1; - - croak "\nCould not find row in table '$table' for blob update:\n". - Data::Dumper::Dumper(\%where)."\n"; + + $self->throw_exception( + "Could not find row in table '$table' for blob update:\n" + . Data::Dumper::Concise::Dumper (\%where) + ); } eval { @@ -883,12 +807,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); } } } @@ -938,7 +862,7 @@ C columns only have minute precision. sub connect_call_datetime_setup { my $self = shift; - my $dbh = $self->_dbh; + my $dbh = $self->_get_dbh; if ($dbh->can('syb_date_fmt')) { # amazingly, this works with FreeTDS @@ -1075,10 +999,10 @@ 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. +begin a transaction while there are active cursors; nor can you use multiple +active cursors within a transaction. 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: @@ -1092,6 +1016,11 @@ For example, this will not work: } }); +This won't either: + + my $first_row = $large_rs->first; + $schema->txn_do(sub { ... }); + Transactions done for inserts in C mode when placeholders are in use are not affected, as they are done on an extra database handle. @@ -1169,6 +1098,33 @@ loading your app, if it doesn't match the character set of your database. When inserting IMAGE columns using this method, you'll need to use L as well. +=head1 TODO + +=over + +=item * + +Transitions to AutoCommit=0 (starting a transaction) mode by exhausting +any active cursors, using eager cursors. + +=item * + +Real limits and limited counts using stored procedures deployed on startup. + +=item * + +Adaptive Server Anywhere (ASA) support, with possible SQLA::Limit support. + +=item * + +Blob update with a LIKE query on a blob, without invalidating the WHERE condition. + +=item * + +bulk_insert using prepare_cached (see comments.) + +=back + =head1 AUTHOR See L.