DESCRIPTION typo fix
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / ODBC / Microsoft_SQL_Server.pm
index 88627d3..911ca48 100644 (file)
@@ -2,11 +2,14 @@ package DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server;
 use strict;
 use warnings;
 
-use base qw/DBIx::Class::Storage::DBI::MSSQL/;
+use base qw/
+  DBIx::Class::Storage::DBI::ODBC
+  DBIx::Class::Storage::DBI::MSSQL
+/;
 use mro 'c3';
 use Scalar::Util 'reftype';
 use Try::Tiny;
-use Carp::Clan qw/^DBIx::Class/;
+use DBIx::Class::Carp;
 use namespace::clean;
 
 __PACKAGE__->mk_group_accessors(simple => qw/
@@ -21,7 +24,7 @@ to Microsoft SQL Server over ODBC
 =head1 DESCRIPTION
 
 This class implements support specific to Microsoft SQL Server over ODBC.  It is
-loaded automatically by by DBIx::Class::Storage::DBI::ODBC when it detects a
+loaded automatically by DBIx::Class::Storage::DBI::ODBC when it detects a
 MSSQL back-end.
 
 Most of the functionality is provided from the superclass
@@ -33,12 +36,13 @@ L<DBIx::Class::Storage::DBI::MSSQL>.
 
   sudo aptitude install tdsodbc libdbd-odbc-perl unixodbc
 
-In case it is not already there put the following in C</etc/odbcinst.ini>:
+In case it is not already there put the following (adjust for non-64bit arch) in
+C</etc/odbcinst.ini>:
 
   [FreeTDS]
   Description = FreeTDS
-  Driver      = /usr/lib/odbc/libtdsodbc.so
-  Setup       = /usr/lib/odbc/libtdsS.so
+  Driver      = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
+  Setup       = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
   UsageCount  = 1
 
 Set your C<$dsn> in L<connect_info|DBIx::Class::Storage::DBI/connect_info> as follows:
@@ -75,6 +79,10 @@ The following options are alternative ways to enable concurrent executing
 statement support. Each has its own advantages and drawbacks and works on
 different platforms. Read each section carefully.
 
+For more details about using MAS in MSSQL over DBD::ODBC see this excellent
+document provided by EasySoft:
+L<http://www.easysoft.com/developer/languages/perl/multiple-active-statements.html>.
+
 In order of preference, they are:
 
 =over 8
@@ -124,7 +132,7 @@ sub connect_call_use_mars {
   }
 
   if ($dsn !~ /MARS_Connection=/) {
-    if ($self->using_freetds) {
+    if ($self->_using_freetds) {
       $self->throw_exception('FreeTDS does not support MARS at the time of '
                             .'writing.');
     }
@@ -135,7 +143,7 @@ sub connect_call_use_mars {
     }
 
     if (my ($data_source) = $dsn =~ /^dbi:ODBC:([\w-]+)\z/i) { # prefix with DSN
-      warn "Bare DSN in ODBC connect string, rewriting as 'dsn=$data_source'"
+      carp_unique "Bare DSN in ODBC connect string, rewriting as 'dsn=$data_source'"
           ." for MARS\n";
       $dsn = "dbi:ODBC:dsn=$data_source";
     }
@@ -174,27 +182,32 @@ C<SCOPE_IDENTITY()>, and C<SELECT @@IDENTITY> will be used instead, which on SQL
 Server 2005 and later will return erroneous results on tables which have an on
 insert trigger that inserts into another table with an C<IDENTITY> column.
 
+B<WARNING:> on FreeTDS, changes made in one statement (e.g. an insert) may not
+be visible from a following statement (e.g. a select.)
+
+B<WARNING:> FreeTDS versions > 0.82 seem to have completely broken the ODBC
+protocol. DBIC will not allow dynamic cursor support with such versions to
+protect your data. Please hassle the authors of FreeTDS to act on the bugs that
+make their driver not overly usable with DBD::ODBC.
+
 =cut
 
 sub connect_call_use_dynamic_cursors {
   my $self = shift;
 
-  my $conn_info = $self->_dbi_connect_info;
+  if (($self->_dbic_connect_attributes->{odbc_cursortype} || 0) < 2) {
 
-  if (ref($conn_info->[0]) eq 'CODE') {
-    $self->throw_exception ('Cannot set DBI attributes on a CODE ref connect_info');
-  }
+    my $dbi_inf = $self->_dbi_connect_info;
+
+    $self->throw_exception ('Cannot set DBI attributes on a CODE ref connect_info')
+      if ref($dbi_inf->[0]) eq 'CODE';
 
-  if (
-    ref($conn_info->[-1]) ne 'HASH'
-      or
-    ($conn_info->[-1]{odbc_cursortype}||0) < 2
-  ) {
     # reenter connection information with the attribute re-set
-    $self->connect_info(
-      @{$conn_info}[0,1,2],
-      { %{$self->_dbix_connect_attributes}, odbc_cursortype => 2 },
-    );
+    $dbi_inf->[3] = {} if @$dbi_inf <= 3;
+    $dbi_inf->[3]{odbc_cursortype} = 2;
+
+    $self->_dbi_connect_info($dbi_inf);
+
     $self->disconnect; # resetting dbi attrs, so have to reconnect
     $self->ensure_connected;
   }
@@ -203,6 +216,8 @@ sub connect_call_use_dynamic_cursors {
 sub _run_connection_actions {
   my $self = shift;
 
+  $self->next::method (@_);
+
   # keep the dynamic_cursors_support and driver-state in sync
   # on every reconnect
   my $use_dyncursors = ($self->_dbic_connect_attributes->{odbc_cursortype} || 0) > 1;
@@ -221,7 +236,7 @@ sub _run_connection_actions {
         $self->throw_exception (
           'Your drivers do not seem to support dynamic cursors (odbc_cursortype => 2).'
          . (
-          $self->using_freetds
+          $self->_using_freetds
             ? ' If you are using FreeTDS, make sure to set tds_version to 8.0 or greater.'
             : ''
           )
@@ -237,7 +252,28 @@ sub _run_connection_actions {
     }
   }
 
-  $self->next::method (@_);
+  $self->_no_scope_identity_query($self->_using_dynamic_cursors
+    ? $self->_using_freetds
+    : undef
+  );
+
+  # freetds is too damn broken, some fixups
+  if ($self->_using_freetds) {
+
+    # no dynamic cursors starting from 0.83
+    if ($self->_using_dynamic_cursors) {
+      my $fv = $self->_using_freetds_version || 999;  # assume large if can't be determined
+      $self->throw_exception(
+        'Dynamic cursors (odbc_cursortype => 2) are not supported with FreeTDS > 0.82 '
+      . "(you have $fv). Please hassle FreeTDS authors to fix the outstanding bugs in "
+      . 'their driver.'
+      ) if $fv > 0.82
+    }
+
+    # FreeTDS is too broken wrt execute_for_fetch batching
+    # just disable it outright until things quiet down
+    $self->_disable_odbc_array_ops;
+  }
 }
 
 =head2 connect_call_use_server_cursors
@@ -272,35 +308,15 @@ sub connect_call_use_server_cursors {
   $self->_get_dbh->{odbc_SQL_ROWSET_SIZE} = $sql_rowset_size;
 }
 
-=head2 using_freetds
-
-Tries to determine, to the best of our ability, whether or not you are using the
-FreeTDS driver with L<DBD::ODBC>.
-
-=cut
-
-sub using_freetds {
-  my $self = shift;
-
-  my $dsn = $self->_dbi_connect_info->[0];
-
-  $dsn = '' if ref $dsn eq 'CODE';
-
-  return 1 if $dsn =~ /driver=FreeTDS/i
-              || ($self->_dbh_get_info(6)||'') =~ /tdsodbc/i;
-
-  return 0;
-}
-
 1;
 
 =head1 AUTHOR
 
-See L<DBIx::Class/CONTRIBUTORS>.
+See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
 
 =head1 LICENSE
 
 You may distribute this code under the same terms as Perl itself.
 
 =cut
-# vim: sw=2 sts=2
+# vim:sw=2 sts=2 et