[Bast-commits] r8016 - in DBIx-Class/0.08/branches/mssql_rno_pagination: lib/DBIx/Class/Storage/DBI t

ribasushi at dev.catalyst.perl.org ribasushi at dev.catalyst.perl.org
Thu Dec 3 09:03:18 GMT 2009


Author: ribasushi
Date: 2009-12-03 09:03:18 +0000 (Thu, 03 Dec 2009)
New Revision: 8016

Modified:
   DBIx-Class/0.08/branches/mssql_rno_pagination/lib/DBIx/Class/Storage/DBI/MSSQL.pm
   DBIx-Class/0.08/branches/mssql_rno_pagination/t/746mssql.t
Log:
The correct top100 mssql solution and test

Modified: DBIx-Class/0.08/branches/mssql_rno_pagination/lib/DBIx/Class/Storage/DBI/MSSQL.pm
===================================================================
--- DBIx-Class/0.08/branches/mssql_rno_pagination/lib/DBIx/Class/Storage/DBI/MSSQL.pm	2009-12-03 01:48:36 UTC (rev 8015)
+++ DBIx-Class/0.08/branches/mssql_rno_pagination/lib/DBIx/Class/Storage/DBI/MSSQL.pm	2009-12-03 09:03:18 UTC (rev 8016)
@@ -178,6 +178,31 @@
 
 sub last_insert_id { shift->_identity }
 
+#
+# MSSQL is retarded wrt ordered subselects. One needs to add a TOP 100%
+# to *all* subqueries, do it here.
+#
+sub _select_args_to_query {
+  my $self = shift;
+
+  my $sql_maker = $self->sql_maker;
+  my ($op, $bind, $ident, $bind_attrs, $select, $cond, $order) = $self->_select_args(@_);
+
+  if (not scalar $sql_maker->_order_by_chunks ($order->{order_by}) ) {
+    # no ordering, just short circuit
+    return $self->next::method (@_);
+  }
+
+  my ($sql, $bind, @rest) = $self->next::method (@_);
+  $sql =~ s/^ \s* SELECT \s/SELECT TOP 100 PERCENT /xi;
+
+  return wantarray
+    ? ($sql, $bind, @rest)
+    : \[ "($sql)", @$bind ]
+  ;
+}
+
+
 # savepoint syntax is the same as in Sybase ASE
 
 sub _svp_begin {

Modified: DBIx-Class/0.08/branches/mssql_rno_pagination/t/746mssql.t
===================================================================
--- DBIx-Class/0.08/branches/mssql_rno_pagination/t/746mssql.t	2009-12-03 01:48:36 UTC (rev 8015)
+++ DBIx-Class/0.08/branches/mssql_rno_pagination/t/746mssql.t	2009-12-03 09:03:18 UTC (rev 8016)
@@ -249,6 +249,20 @@
   ]);
 }, 'populate without PKs supplied ok' );
 
+# make sure ordered subselects work
+{
+  my $book_owner_ids = $schema->resultset ('BooksInLibrary')
+                               ->search ({}, { join => 'owner', distinct => 1, order_by => { -desc => 'owner'} })
+                                ->get_column ('owner');
+
+  my $owners = $schema->resultset ('Owners')->search ({
+    id => { -in => $book_owner_ids->as_query }
+  });
+
+  is ($owners->count, 8, 'Correct amount of book owners');
+  is ($owners->all, 8, 'Correct amount of book owner objects');
+}
+
 #
 # try a prefetch on tables with identically named columns
 #




More information about the Bast-commits mailing list