);
my @also_proxy_to_writer_storage = qw/
+ connect_call_set_auto_cast auto_cast connect_call_blob_setup
+ connect_call_datetime_setup
+
disconnect _connect_info _sql_maker _sql_maker_opts disable_sth_caching
auto_savepoint unsafe cursor_class debug debugobj schema
/;
$writer_storage->_is_writer_storage(1);
$writer_storage->connect_info($self->connect_info);
+ $writer_storage->auto_cast($self->auto_cast);
$self->_writer_storage($writer_storage);
}
for my $method (@also_proxy_to_writer_storage) {
no strict 'refs';
+ no warnings 'redefine';
my $replaced = __PACKAGE__->can($method);
$type && $type =~ /(?:text|image|lob|bytea|binary|memo)/i;
}
+sub _is_lob_column {
+ my ($self, $source, $column) = @_;
+
+ return $self->_is_lob_type($source->column_info($column)->{data_type});
+}
+
sub _prep_for_execute {
my $self = shift;
my ($op, $extra_bind, $ident, $args) = @_;
sub update {
my $self = shift;
- my ($source, $fields, $where) = @_;
+ my ($source, $fields, $where, @rest) = @_;
my $wantarray = wantarray;
$self->_unset_identity_insert($table, 'update') if $is_identity_update;
}
-# check if condition and fields allow for a 2-step update
- $self->_assert_blob_update_possible($source, $fields, $where);
+# 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';
+ }
+ }
# update+blob update(s) done atomically on separate connection
$self = $self->_writer_storage;
my $guard = $self->txn_scope_guard;
- $self->_set_identity_insert($table, 'update') if $is_identity_update;
+# First update the blob columns to be updated to '' (taken from $fields, where
+# it is originally put by _remove_blob_cols .)
+ my %blobs_to_empty = map { ($_ => delete $fields->{$_}) } keys %$blob_cols;
- my @res;
- if ($wantarray) {
- @res = $self->next::method(@_);
- }
- elsif (defined $wantarray) {
- $res[0] = $self->next::method(@_);
- }
- else {
- $self->next::method(@_);
- }
+ $self->next::method($source, \%blobs_to_empty, $where, @rest);
- $self->_unset_identity_insert($table, 'update') if $is_identity_update;
+# Now update the blobs before the other columns in case the update of other
+# columns makes the search condition invalid.
+ $self->_update_blobs($source, $blob_cols, $where);
- my %new_where = map { $_ => ($fields->{$_} || $where->{$_}) } keys %$where;
+ my @res;
+ if (%$fields) {
+ $self->_set_identity_insert($table, 'update') if $is_identity_update;
- $self->_update_blobs($source, $blob_cols, \%new_where);
+ if ($wantarray) {
+ @res = $self->next::method(@_);
+ }
+ elsif (defined $wantarray) {
+ $res[0] = $self->next::method(@_);
+ }
+ else {
+ $self->next::method(@_);
+ }
+
+ $self->_unset_identity_insert($table, 'update') if $is_identity_update;
+ }
$guard->commit;
return $wantarray ? @res : $res[0];
}
-sub _assert_blob_update_possible {
- my ($self, $source, $fields, $where) = @_;
-
- my $table = $source->name;
-
-# If $where condition is mutually exclusive from $fields (what gets updated)
-# then update is safe.
- my %count;
- $count{$_}++ foreach keys %$where, keys %$fields;
- return 1 unless List::Util::first { $_ == 2 } values %count;
-
-# Otherwise check that what is updated includes either a primary or unique key.
- my (@primary_cols) = $source->primary_columns;
- return 1 if (grep exists $fields->{$_}, @primary_cols) == @primary_cols;
-
- my %unique_constraints = $source->unique_constraints;
- for my $uniq_constr (values %unique_constraints) {
- return 1 if (grep exists $fields->{$_}, @$uniq_constr) == @$uniq_constr;
- }
-
-# otherwise throw exception
- 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 sprintf
-"2-step TEXT/IMAGE update on table '$table' impossible for condition: \n%s\n".
-"Setting columns: \n%s\n",
- Data::Dumper::Dumper($where),
- Data::Dumper::Dumper($fields);
-}
-
### the insert_bulk stuff stolen from DBI/MSSQL.pm
sub _set_identity_insert {
$self->_query_end($sql);
}
+# for tests
+sub _can_insert_bulk { 1 }
+
# XXX this should use the DBD::Sybase bulk API, where possible
sub insert_bulk {
my $self = shift;
### end of stolen insert_bulk section
+# Make sure blobs are not bound as placeholders, and return any non-empty ones
+# as a hash.
sub _remove_blob_cols {
my ($self, $source, $fields) = @_;
for my $col (keys %$fields) {
if ($self->_is_lob_type($source->column_info($col)->{data_type})) {
- $blob_cols{$col} = delete $fields->{$col};
- $fields->{$col} = \"''";
+ my $blob_val = delete $fields->{$col};
+ if (not defined $blob_val) {
+ $fields->{$col} = \'NULL';
+ }
+ else {
+ $fields->{$col} = \"''";
+ $blob_cols{$col} = $blob_val unless $blob_val eq '';
+ }
}
}
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 %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};
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"
+ . $self->_pretty_print (\%where)
+ );
}
eval {
$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);
}
}
}