Merge 'trunk' into 'sybase'
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase / Base.pm
index e1ebe88..08b5ca4 100644 (file)
@@ -3,28 +3,30 @@ package DBIx::Class::Storage::DBI::Sybase::Base;
 use strict;
 use warnings;
 
+use base qw/DBIx::Class::Storage::DBI/;
+use mro 'c3';
+
 =head1 NAME
 
 DBIx::Class::Storage::DBI::Sybase::Base - Common functionality for drivers using
 DBD::Sybase
 
-=head1 METHODS
-
-=head2 connected
+=head1 DESCRIPTION
 
-Returns true if we have an open (and working) database connection, false if it
-is not (yet) open (or does not work). (Executes a simple SELECT to make sure it
-works.)
+This is the base class for L<DBIx::Class::Storage::DBI::Sybase> and
+L<DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server>. It provides some
+utility methods related to L<DBD::Sybase> and the supported functions of the
+database you are connecting to.
 
-The reason this is needed is that L<DBD::Sybase>'s ping() does not work with an
-active statement handle, leading to masked database errors.
+=head1 METHODS
 
 =cut
 
-sub connected {
+sub _ping {
   my $self = shift;
 
-  my $dbh = $self->_dbh;
+  my $dbh = $self->_dbh or return 0;
+
   local $dbh->{RaiseError} = 1;
   eval {
     $dbh->do('select 1');
@@ -33,6 +35,98 @@ sub connected {
   return $@ ? 0 : 1;
 }
 
+=head2 placeholders_supported
+
+Whether or not string placeholders work. Does not check for implicit conversion
+errors, see L</placeholders_with_type_conversion_supported>.
+
+=cut
+
+sub placeholders_supported {
+  my $self = shift;
+  my $dbh  = $self->_get_dbh;
+
+  return eval {
+# There's also $dbh->{syb_dynamic_supported} but it can be inaccurate for this
+# purpose.
+    local $dbh->{PrintError} = 0;
+    local $dbh->{RaiseError} = 1;
+    $dbh->selectrow_array('select ?', {}, 1);
+  };
+}
+
+=head2 placeholders_with_type_conversion_supported 
+
+Checks if placeholders bound to non-string types throw implicit type conversion
+errors or not.
+
+See L<DBIx::Class::Storage::DBI::Sybase/connect_call_set_auto_cast>.
+
+=cut
+
+sub placeholders_with_type_conversion_supported {
+  my $self = shift;
+  my $dbh  = $self->_dbh;
+
+  return eval {
+    local $dbh->{PrintError} = 0;
+    local $dbh->{RaiseError} = 1;
+# this specifically tests a bind that is NOT a string
+    $dbh->selectrow_array('select 1 where 1 = ?', {}, 1);
+  };
+}
+
+sub _set_max_connect {
+  my $self = shift;
+  my $val  = shift || 256;
+
+  my $dsn = $self->_dbi_connect_info->[0];
+
+  return if ref($dsn) eq 'CODE';
+
+  if ($dsn !~ /maxConnect=/) {
+    $self->_dbi_connect_info->[0] = "$dsn;maxConnect=$val";
+    my $connected = defined $self->_dbh;
+    $self->disconnect;
+    $self->ensure_connected if $connected;
+  }
+}
+
+=head2 using_freetds
+
+Whether or not L<DBD::Sybase> was compiled against FreeTDS. If false, it means
+the Sybase OpenClient libraries were used.
+
+=cut
+
+sub using_freetds {
+  my $self = shift;
+
+  return $self->_dbh->{syb_oc_version} =~ /freetds/i;
+}
+
+=head2 set_textsize
+
+When using FreeTDS and/or MSSQL, C<< $dbh->{LongReadLen} >> is not available,
+use this function instead. It does:
+
+  $dbh->do("SET TEXTSIZE $bytes");
+
+Takes the number of bytes, or uses the C<LongReadLen> value from your
+L<DBIx::Class/connect_info> if omitted.
+
+=cut
+
+sub set_textsize {
+  my $self = shift;
+  my $text_size = shift ||
+    eval { $self->_dbi_connect_info->[-1]->{LongReadLen} };
+
+  return unless defined $text_size;
+
+  $self->_dbh->do("SET TEXTSIZE $text_size");
+}
+
 1;
 
 =head1 AUTHORS