minor cleanups
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase.pm
index 05cc32a..8e65c99 100644 (file)
@@ -2,11 +2,12 @@ package DBIx::Class::Storage::DBI::Sybase;
 
 use strict;
 use warnings;
-use mro 'c3';
+
 use base qw/
     DBIx::Class::Storage::DBI::Sybase::Base
     DBIx::Class::Storage::DBI
 /;
+use mro 'c3';
 use Carp::Clan qw/^DBIx::Class/;
 
 =head1 NAME
@@ -30,6 +31,9 @@ without doing a C<select max(col)>.
 
 But your queries will be cached.
 
+You need at least version C<1.09> of L<DBD::Sybase> for placeholder support.
+Otherwise your storage will be automatically reblessed into C<::NoBindVars>.
+
 A recommended L<DBIx::Class::Storage::DBI/connect_info> settings:
 
   on_connect_call => [['datetime_setup'], [blob_setup => log_on_update => 0]]
@@ -57,16 +61,50 @@ sub _rebless {
     if (!$exception && $dbtype && $self->load_optional_class($subclass)) {
       bless $self, $subclass;
       $self->_rebless;
-    } else {
-      # real Sybase
+    } else { # real Sybase
+      my $no_bind_vars = 'DBIx::Class::Storage::DBI::Sybase::NoBindVars';
+
       if (not $self->dbh->{syb_dynamic_supported}) {
-        bless $self, 'DBIx::Class::Storage:DBI::Sybase::NoBindVars';
+        $self->ensure_class_loaded($no_bind_vars);
+        bless $self, $no_bind_vars;
         $self->_rebless;
       }
+      
+      if ($DBD::Sybase::VERSION < 1.09) {
+        carp <<'EOF';
+
+Your version of Sybase potentially supports placeholders and query caching,
+however your version of DBD::Sybase is too old to do this properly. Please
+upgrade to at least version 1.09 if you want this feature.
+
+TEXT/IMAGE column support will also not work in older versions of DBD::Sybase.
+
+See perldoc DBIx::Class::Storage::DBI::Sybase for more details.
+EOF
+        $self->ensure_class_loaded($no_bind_vars);
+        bless $self, $no_bind_vars;
+        $self->_rebless;
+      }
+      $self->_set_maxConnect;
     }
   }
 }
 
+sub _set_maxConnect {
+  my $self = shift;
+
+  my $dsn = $self->_dbi_connect_info->[0];
+
+  return if ref($dsn) eq 'CODE';
+
+  if ($dsn !~ /maxConnect=/) {
+    $self->_dbi_connect_info->[0] = "$dsn;maxConnect=256";
+    my $connected = defined $self->_dbh;
+    $self->disconnect;
+    $self->ensure_connected if $connected;
+  }
+}
+
 =head2 connect_call_blob_setup
 
 Used as:
@@ -98,9 +136,33 @@ sub connect_call_blob_setup {
 
 sub _is_lob_type {
   my $self = shift;
-  shift =~ /(?:text|image|lob|bytea|binary)/i;
+  my $type = shift;
+  $type && $type =~ /(?:text|image|lob|bytea|binary)/i;
 }
 
+## This will be useful if we ever implement BLOB filehandle inflation and will
+## need to use the API, but for now it isn't.
+#
+#sub order_columns_for_select {
+#  my ($self, $source) = @_;
+#
+#  my (@non_blobs, @blobs);
+#
+#  for my $col ($source->columns) {
+#    if ($self->_is_lob_type($source->column_info($col)->{data_type})) {
+#      push @blobs, $col;
+#    } else {
+#      push @non_blobs, $col;
+#    }
+#  }
+#
+#  croak "cannot select more than a one TEXT/IMAGE column at a time"
+#    if @blobs > 1;
+#
+#  return (@non_blobs, @blobs);
+#}
+
+# override to handle TEXT/IMAGE
 sub insert {
   my ($self, $source, $to_insert) = splice @_, 0, 3;
 
@@ -140,8 +202,10 @@ sub _remove_blob_cols {
   my %blob_cols;
 
   for my $col (keys %$fields) {
-    $blob_cols{$col} = delete $fields->{$col}
-      if $self->_is_lob_type($source->column_info($col)->{data_type});
+    if ($self->_is_lob_type($source->column_info($col)->{data_type})) {
+      $blob_cols{$col} = delete $fields->{$col};
+      $fields->{$col} = \"''";
+    }
   }
 
   return \%blob_cols;
@@ -153,28 +217,40 @@ sub _update_blobs {
 
   my $table = $source->from;
 
+  my %inserted = %$inserted;
   my (@primary_cols) = $source->primary_columns;
 
-  croak "Cannot update TEXT/IMAGE without a primary key!"
+  croak "Cannot update TEXT/IMAGE column(s) without a primary key"
     unless @primary_cols;
 
-  my $search_cond = join ',' => map "$_ = ?", @primary_cols;
+  if ((grep { defined $inserted{$_} } @primary_cols) != @primary_cols) {
+    if (@primary_cols == 1) {
+      my $col = $primary_cols[0];
+      $inserted{$col} = $self->last_insert_id($source, $col);
+    } else {
+      croak "Cannot update TEXT/IMAGE column(s) without primary key values";
+    }
+  }
 
   for my $col (keys %$blob_cols) {
     my $blob = $blob_cols->{$col};
+    my $sth;
 
-# First update to empty string in case it's NULL, can't update a NULL blob using
-# the API.
-    my $sth = $dbh->prepare_cached(
-      qq{update $table set $col = '' where $search_cond}
-    );
-    $sth->execute(map $inserted->{$_}, @primary_cols) or die $sth->errstr;
-    $sth->finish;
+    if (not $self->isa('DBIx::Class::Storage::DBI::NoBindVars')) {
+      my $search_cond = join ',' => map "$_ = ?", @primary_cols;
 
-    $sth = $dbh->prepare_cached(
-      "select $col from $table where $search_cond"
-    );
-    $sth->execute(map $inserted->{$_}, @primary_cols);
+      $sth = $self->sth(
+        "select $col from $table where $search_cond"
+      );
+      $sth->execute(map $inserted{$_}, @primary_cols);
+    } else {
+      my $search_cond = join ',' => map "$_ = $inserted{$_}", @primary_cols;
+
+      $sth = $dbh->prepare(
+        "select $col from $table where $search_cond"
+      );
+      $sth->execute;
+    }
 
     eval {
       while ($sth->fetch) {
@@ -246,74 +322,28 @@ sub _dbh_last_insert_id {
   my ($self, $dbh, $source, $col) = @_;
 
   # sorry, there's no other way!
-  my $sth = $dbh->prepare_cached("select max($col) from ".$source->from);
-  return ($dbh->selectrow_array($sth))[0];
+  my $sth = $self->sth("select max($col) from ".$source->from);
+  my ($id) = $dbh->selectrow_array($sth);
+  $sth->finish;
+
+  return $id;
 }
 
-# previous implementation of limited count for Sybase, does not include
-# count_grouped.
+1;
 
-#sub _copy_attributes_for_count {
-#  my ($self, $source, $attrs) = @_;
-#  my %attrs = %$attrs;
-#
-#  # take off any column specs, any pagers, record_filter is cdbi, and no point of ordering a count
-#  delete @attrs{qw/select as rows offset page order_by record_filter/};
-#
-#  return \%attrs;
-#}
-#
-#=head2 count
-#
-#Counts for limited queries are emulated by executing select queries and
-#returning the number of successful executions minus the offset.
-#
-#This is necessary due to the limitations of Sybase.
-#
-#=cut
-#
-#sub count {
-#  my $self = shift;
-#  my ($source, $attrs) = @_;
-#
-#  my $new_attrs = $self->_copy_attributes_for_count($source, $attrs);
-#
-#  if (exists $attrs->{rows}) {
-#    my $offset = $attrs->{offset} || 0;
-#    my $total  = $attrs->{rows} + $offset;
-#
-#    my $first_pk = ($source->primary_columns)[0];
-#
-#    $new_attrs->{select} = $first_pk ? "me.$first_pk" : 1;
-#
-#    my $tmp_rs = $source->resultset_class->new($source, $new_attrs);
-#
-#    $self->dbh->{syb_rowcount} = $total;
-#
-#    my $count = 0;
-#    $count++ while $tmp_rs->cursor->next;
-#
-#    $self->dbh->{syb_rowcount} = 0;
-#
-#    return $count - $offset;
-#  } else {
-#    # overwrite the selector
-#    $new_attrs->{select} = { count => '*' };
-#
-#    my $tmp_rs = $source->resultset_class->new($source, $new_attrs);
-#    my ($count) = $tmp_rs->cursor->next;
-#
-#    # if the offset/rows attributes are still present, we did not use
-#    # a subquery, so we need to make the calculations in software
-#    $count -= $attrs->{offset} if $attrs->{offset};
-#    $count = $attrs->{rows} if $attrs->{rows} and $attrs->{rows} < $count;
-#    $count = 0 if ($count < 0);
-#
-#    return $count;
-#  }
-#}
+=head1 MAXIMUM CONNECTIONS
 
-1;
+L<DBD::Sybase> makes separate connections to the server for active statements in
+the background. By default the number of such connections is limited to 25, on
+both the client side and the server side.
+
+This is a bit too low, so on connection the clientside setting is set to C<256>
+(see L<DBD::Sybase/maxConnect>.) You can override it to whatever setting you
+like in the DSN.
+
+See
+L<http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.sag1/html/sag1/sag1272.htm>
+for information on changing the setting on the server side.
 
 =head1 DATES
 
@@ -322,12 +352,12 @@ for L<DBIx::Class::InflateColumn::DateTime>.
 
 =head1 IMAGE AND TEXT COLUMNS
 
+You need at least version C<1.09> of L<DBD::Sybase> for C<TEXT/IMAGE> column
+support.
+
 See L</connect_call_blob_setup> for a L<DBIx::Class::Storage::DBI/connect_info>
 setting you need to work with C<IMAGE> columns.
 
-Due to limitations in L<DBD::Sybase> and this driver, it is only possible to
-select one C<TEXT> or C<IMAGE> column at a time.
-
 =head1 AUTHORS
 
 See L<DBIx::Class/CONTRIBUTORS>.