[Bast-commits] r9317 - in DBIx-Class/0.08/trunk: lib/DBIx/Class/Storage/DBI t

caelum at dev.catalyst.perl.org caelum at dev.catalyst.perl.org
Fri May 7 09:57:24 GMT 2010


Author: caelum
Date: 2010-05-07 10:57:24 +0100 (Fri, 07 May 2010)
New Revision: 9317

Modified:
   DBIx-Class/0.08/trunk/lib/DBIx/Class/Storage/DBI/MSSQL.pm
   DBIx-Class/0.08/trunk/t/74mssql.t
Log:
detect row_number() over support in MSSQL if version detection fails

Modified: DBIx-Class/0.08/trunk/lib/DBIx/Class/Storage/DBI/MSSQL.pm
===================================================================
--- DBIx-Class/0.08/trunk/lib/DBIx/Class/Storage/DBI/MSSQL.pm	2010-05-07 08:15:48 UTC (rev 9316)
+++ DBIx-Class/0.08/trunk/lib/DBIx/Class/Storage/DBI/MSSQL.pm	2010-05-07 09:57:24 UTC (rev 9317)
@@ -205,11 +205,23 @@
 
   unless ($self->_sql_maker) {
     unless ($self->{_sql_maker_opts}{limit_dialect}) {
+      my $have_rno = 0;
 
-      my $version = $self->_server_info->{normalized_dbms_version} || 0;
+      if (exists $self->_server_info->{normalized_dbms_version}) {
+        $have_rno = 1 if $self->_server_info->{normalized_dbms_version} >= 9;
+      }
+      else {
+        # User is connecting via DBD::Sybase and has no permission to run
+        # stored procedures like xp_msver, or version detection failed for some
+        # other reason.
+        # So, we use a query to check if RNO is implemented.
+        $have_rno = 1 if (eval { local $@; ($self->_get_dbh
+          ->selectrow_array('SELECT row_number() OVER (ORDER BY rand())')
+          )[0] } || 0);
+      }
 
       $self->{_sql_maker_opts} = {
-        limit_dialect => ($version >= 9 ? 'RowNumberOver' : 'Top'),
+        limit_dialect => ($have_rno ? 'RowNumberOver' : 'Top'),
         %{$self->{_sql_maker_opts}||{}}
       };
     }

Modified: DBIx-Class/0.08/trunk/t/74mssql.t
===================================================================
--- DBIx-Class/0.08/trunk/t/74mssql.t	2010-05-07 08:15:48 UTC (rev 9316)
+++ DBIx-Class/0.08/trunk/t/74mssql.t	2010-05-07 09:57:24 UTC (rev 9317)
@@ -172,6 +172,38 @@
 
   is $rs->first, undef, 'rolled back';
   $rs->reset;
+
+  # test RNO detection when version detection fails
+  SKIP: {
+    my $storage = $schema->storage;
+    my $version = $storage->_server_info->{normalized_dbms_version};
+    
+    skip 1, 'could not detect SQL Server version' if not defined $version;
+
+    my $have_rno = $version >= 9 ? 1 : 0;
+
+    # Delete version information to force RNO check when rebuilding SQLA
+    # instance.
+    no strict 'refs';
+    no warnings 'redefine';
+    local *{(ref $storage).'::_get_server_version'} = sub { undef };
+
+    my $server_info = { %{ $storage->_server_info_hash } }; # clone
+
+    delete @$server_info{qw/dbms_version normalized_dbms_version/};
+
+    local $storage->{_server_info_hash} = $server_info;
+    local $storage->{_sql_maker}        = undef;
+    local $storage->{_sql_maker_opts}   = undef;
+
+    $storage->sql_maker;
+
+    my $rno_detected =
+      ($storage->{_sql_maker_opts}{limit_dialect} eq 'RowNumberOver');
+
+    ok ((not ($have_rno xor $rno_detected)),
+      'row_number() over support detected correctly');
+  }
 }
 
 # test op-induced autoconnect




More information about the Bast-commits mailing list