Make sure order realiasing remains in proper sequence on sorting
Peter Rabbitson [Fri, 20 Apr 2012 07:33:22 +0000 (09:33 +0200)]
We now support 999 realiased order criteria, this is beyond sufficient

lib/DBIx/Class/SQLMaker/LimitDialects.pm
t/sqlmaker/limit_dialects/fetch_first.t
t/sqlmaker/limit_dialects/rno.t
t/sqlmaker/limit_dialects/toplimit.t
t/sqlmaker/limit_dialects/torture.t

index a0ea4ef..d3ea35c 100644 (file)
@@ -773,7 +773,7 @@ sub _subqueried_limit_attrs {
     next if $in_sel_index->{$chunk};
 
     $extra_order_sel->{$chunk} ||= $self->_quote (
-      'ORDER__BY__' . scalar keys %{$extra_order_sel||{}}
+      'ORDER__BY__' . sprintf '%03d', scalar keys %{$extra_order_sel||{}}
     );
   }
 
index 8c7fa47..12b9804 100644 (file)
@@ -49,58 +49,58 @@ for my $ord_set (
   {
     order_by => \'title DESC',
     order_inner => 'title DESC',
-    order_outer => 'ORDER__BY__1 ASC',
-    order_req => 'ORDER__BY__1 DESC',
-    exselect_outer => 'ORDER__BY__1',
-    exselect_inner => 'title AS ORDER__BY__1',
+    order_outer => 'ORDER__BY__001 ASC',
+    order_req => 'ORDER__BY__001 DESC',
+    exselect_outer => 'ORDER__BY__001',
+    exselect_inner => 'title AS ORDER__BY__001',
   },
   {
     order_by => { -asc => 'title'  },
     order_inner => 'title ASC',
-    order_outer => 'ORDER__BY__1 DESC',
-    order_req => 'ORDER__BY__1 ASC',
-    exselect_outer => 'ORDER__BY__1',
-    exselect_inner => 'title AS ORDER__BY__1',
+    order_outer => 'ORDER__BY__001 DESC',
+    order_req => 'ORDER__BY__001 ASC',
+    exselect_outer => 'ORDER__BY__001',
+    exselect_inner => 'title AS ORDER__BY__001',
   },
   {
     order_by => { -desc => 'title' },
     order_inner => 'title DESC',
-    order_outer => 'ORDER__BY__1 ASC',
-    order_req => 'ORDER__BY__1 DESC',
-    exselect_outer => 'ORDER__BY__1',
-    exselect_inner => 'title AS ORDER__BY__1',
+    order_outer => 'ORDER__BY__001 ASC',
+    order_req => 'ORDER__BY__001 DESC',
+    exselect_outer => 'ORDER__BY__001',
+    exselect_inner => 'title AS ORDER__BY__001',
   },
   {
     order_by => 'title',
     order_inner => 'title',
-    order_outer => 'ORDER__BY__1 DESC',
-    order_req => 'ORDER__BY__1',
-    exselect_outer => 'ORDER__BY__1',
-    exselect_inner => 'title AS ORDER__BY__1',
+    order_outer => 'ORDER__BY__001 DESC',
+    order_req => 'ORDER__BY__001',
+    exselect_outer => 'ORDER__BY__001',
+    exselect_inner => 'title AS ORDER__BY__001',
   },
   {
     order_by => [ qw{ title me.owner}   ],
     order_inner => 'title, me.owner',
-    order_outer => 'ORDER__BY__1 DESC, me.owner DESC',
-    order_req => 'ORDER__BY__1, me.owner',
-    exselect_outer => 'ORDER__BY__1',
-    exselect_inner => 'title AS ORDER__BY__1',
+    order_outer => 'ORDER__BY__001 DESC, me.owner DESC',
+    order_req => 'ORDER__BY__001, me.owner',
+    exselect_outer => 'ORDER__BY__001',
+    exselect_inner => 'title AS ORDER__BY__001',
   },
   {
     order_by => ['title', { -desc => 'bar' } ],
     order_inner => 'title, bar DESC',
-    order_outer => 'ORDER__BY__1 DESC, ORDER__BY__2 ASC',
-    order_req => 'ORDER__BY__1, ORDER__BY__2 DESC',
-    exselect_outer => 'ORDER__BY__1, ORDER__BY__2',
-    exselect_inner => 'title AS ORDER__BY__1, bar AS ORDER__BY__2',
+    order_outer => 'ORDER__BY__001 DESC, ORDER__BY__002 ASC',
+    order_req => 'ORDER__BY__001, ORDER__BY__002 DESC',
+    exselect_outer => 'ORDER__BY__001, ORDER__BY__002',
+    exselect_inner => 'title AS ORDER__BY__001, bar AS ORDER__BY__002',
   },
   {
     order_by => { -asc => [qw{ title bar }] },
     order_inner => 'title ASC, bar ASC',
-    order_outer => 'ORDER__BY__1 DESC, ORDER__BY__2 DESC',
-    order_req => 'ORDER__BY__1 ASC, ORDER__BY__2 ASC',
-    exselect_outer => 'ORDER__BY__1, ORDER__BY__2',
-    exselect_inner => 'title AS ORDER__BY__1, bar AS ORDER__BY__2',
+    order_outer => 'ORDER__BY__001 DESC, ORDER__BY__002 DESC',
+    order_req => 'ORDER__BY__001 ASC, ORDER__BY__002 ASC',
+    exselect_outer => 'ORDER__BY__001, ORDER__BY__002',
+    exselect_inner => 'title AS ORDER__BY__001, bar AS ORDER__BY__002',
   },
   {
     order_by => [
@@ -109,10 +109,10 @@ for my $ord_set (
       { -asc  => [qw{me.owner sensors}]},
     ],
     order_inner => 'title, bar DESC, me.owner ASC, sensors ASC',
-    order_outer => 'ORDER__BY__1 DESC, ORDER__BY__2 ASC, me.owner DESC, ORDER__BY__3 DESC',
-    order_req => 'ORDER__BY__1, ORDER__BY__2 DESC, me.owner ASC, ORDER__BY__3 ASC',
-    exselect_outer => 'ORDER__BY__1, ORDER__BY__2, ORDER__BY__3',
-    exselect_inner => 'title AS ORDER__BY__1, bar AS ORDER__BY__2, sensors AS ORDER__BY__3',
+    order_outer => 'ORDER__BY__001 DESC, ORDER__BY__002 ASC, me.owner DESC, ORDER__BY__003 DESC',
+    order_req => 'ORDER__BY__001, ORDER__BY__002 DESC, me.owner ASC, ORDER__BY__003 ASC',
+    exselect_outer => 'ORDER__BY__001, ORDER__BY__002, ORDER__BY__003',
+    exselect_inner => 'title AS ORDER__BY__001, bar AS ORDER__BY__002, sensors AS ORDER__BY__003',
   },
 ) {
   my $o_sel = $ord_set->{exselect_outer}
@@ -152,11 +152,11 @@ is_same_sql_bind (
   $books_45_and_owners->search ({}, { group_by => 'title', order_by => 'title' })->as_query,
   '(SELECT me.id, me.source, me.owner, me.price, owner.id, owner.name
       FROM (
-        SELECT me.id, me.source, me.owner, me.price, ORDER__BY__1 AS title
+        SELECT me.id, me.source, me.owner, me.price, ORDER__BY__001 AS title
           FROM (
-            SELECT me.id, me.source, me.owner, me.price, ORDER__BY__1
+            SELECT me.id, me.source, me.owner, me.price, ORDER__BY__001
               FROM (
-                SELECT me.id, me.source, me.owner, me.price, title AS ORDER__BY__1
+                SELECT me.id, me.source, me.owner, me.price, title AS ORDER__BY__001
                   FROM books me
                   JOIN owners owner ON owner.id = me.owner
                 WHERE ( source = ? )
@@ -164,10 +164,10 @@ is_same_sql_bind (
                 ORDER BY title
                 FETCH FIRST 5 ROWS ONLY
               ) me
-            ORDER BY ORDER__BY__1 DESC
+            ORDER BY ORDER__BY__001 DESC
             FETCH FIRST 2 ROWS ONLY
           ) me
-        ORDER BY ORDER__BY__1
+        ORDER BY ORDER__BY__001
       ) me
       JOIN owners owner ON owner.id = me.owner
     WHERE ( source = ? )
index 4f24e56..32f67c5 100644 (file)
@@ -101,23 +101,17 @@ my $rs_selectas_rel = $schema->resultset('BooksInLibrary')->search ({}, {
   order_by => 'me.id',
 });
 
-# SELECT [owner_name], [owner_books] FROM (
-#   SELECT [owner_name], [owner_books], [ORDER__BY__1], ROW_NUMBER() OVER(  ORDER BY [ORDER__BY__1] ) AS [rno__row__index] FROM (
-#     SELECT [owner].[name] AS [owner_name], (SELECT COUNT( * ) FROM [owners] [owner] WHERE ( ( [count].[id] = [owner].[id] AND [count].[name] = ? ) )) AS [owner_books], [me].[id] AS [ORDER__BY__1]  FROM [books] [me]  JOIN [owners] [owner] ON [owner].[id] = [me].[owner] WHERE ( [source] = ? )
-#   ) [me]
-# ) [me] WHERE [rno__row__index] >= ? AND [rno__row__index] <= ?
-
 is_same_sql_bind(
   $rs_selectas_rel->as_query,
   '(
     SELECT [owner_name], [owner_books]
       FROM (
-        SELECT [owner_name], [owner_books], ROW_NUMBER() OVER( ORDER BY [ORDER__BY__1] ) AS [rno__row__index]
+        SELECT [owner_name], [owner_books], ROW_NUMBER() OVER( ORDER BY [ORDER__BY__001] ) AS [rno__row__index]
           FROM (
             SELECT  [owner].[name] AS [owner_name],
               ( SELECT COUNT( * ) FROM [owners] [owner]
                 WHERE [count].[id] = [owner].[id] and [count].[name] = ? ) AS [owner_books],
-              [me].[id] AS [ORDER__BY__1]
+              [me].[id] AS [ORDER__BY__001]
                 FROM [books] [me]
                 JOIN [owners] [owner] ON [owner].[id] = [me].[owner]
             WHERE ( [source] = ? )
index 2f86103..3c5b4d8 100644 (file)
@@ -89,58 +89,58 @@ for my $ord_set (
   {
     order_by => \'title DESC',
     order_inner => 'title DESC',
-    order_outer => 'ORDER__BY__1 ASC',
-    order_req => 'ORDER__BY__1 DESC',
-    exselect_outer => 'ORDER__BY__1',
-    exselect_inner => 'title AS ORDER__BY__1',
+    order_outer => 'ORDER__BY__001 ASC',
+    order_req => 'ORDER__BY__001 DESC',
+    exselect_outer => 'ORDER__BY__001',
+    exselect_inner => 'title AS ORDER__BY__001',
   },
   {
     order_by => { -asc => 'title'  },
     order_inner => 'title ASC',
-    order_outer => 'ORDER__BY__1 DESC',
-    order_req => 'ORDER__BY__1 ASC',
-    exselect_outer => 'ORDER__BY__1',
-    exselect_inner => 'title AS ORDER__BY__1',
+    order_outer => 'ORDER__BY__001 DESC',
+    order_req => 'ORDER__BY__001 ASC',
+    exselect_outer => 'ORDER__BY__001',
+    exselect_inner => 'title AS ORDER__BY__001',
   },
   {
     order_by => { -desc => 'title' },
     order_inner => 'title DESC',
-    order_outer => 'ORDER__BY__1 ASC',
-    order_req => 'ORDER__BY__1 DESC',
-    exselect_outer => 'ORDER__BY__1',
-    exselect_inner => 'title AS ORDER__BY__1',
+    order_outer => 'ORDER__BY__001 ASC',
+    order_req => 'ORDER__BY__001 DESC',
+    exselect_outer => 'ORDER__BY__001',
+    exselect_inner => 'title AS ORDER__BY__001',
   },
   {
     order_by => 'title',
     order_inner => 'title',
-    order_outer => 'ORDER__BY__1 DESC',
-    order_req => 'ORDER__BY__1',
-    exselect_outer => 'ORDER__BY__1',
-    exselect_inner => 'title AS ORDER__BY__1',
+    order_outer => 'ORDER__BY__001 DESC',
+    order_req => 'ORDER__BY__001',
+    exselect_outer => 'ORDER__BY__001',
+    exselect_inner => 'title AS ORDER__BY__001',
   },
   {
     order_by => [ qw{ title me.owner}   ],
     order_inner => 'title, me.owner',
-    order_outer => 'ORDER__BY__1 DESC, me.owner DESC',
-    order_req => 'ORDER__BY__1, me.owner',
-    exselect_outer => 'ORDER__BY__1',
-    exselect_inner => 'title AS ORDER__BY__1',
+    order_outer => 'ORDER__BY__001 DESC, me.owner DESC',
+    order_req => 'ORDER__BY__001, me.owner',
+    exselect_outer => 'ORDER__BY__001',
+    exselect_inner => 'title AS ORDER__BY__001',
   },
   {
     order_by => ['title', { -desc => 'bar' } ],
     order_inner => 'title, bar DESC',
-    order_outer => 'ORDER__BY__1 DESC, ORDER__BY__2 ASC',
-    order_req => 'ORDER__BY__1, ORDER__BY__2 DESC',
-    exselect_outer => 'ORDER__BY__1, ORDER__BY__2',
-    exselect_inner => 'title AS ORDER__BY__1, bar AS ORDER__BY__2',
+    order_outer => 'ORDER__BY__001 DESC, ORDER__BY__002 ASC',
+    order_req => 'ORDER__BY__001, ORDER__BY__002 DESC',
+    exselect_outer => 'ORDER__BY__001, ORDER__BY__002',
+    exselect_inner => 'title AS ORDER__BY__001, bar AS ORDER__BY__002',
   },
   {
     order_by => { -asc => [qw{ title bar }] },
     order_inner => 'title ASC, bar ASC',
-    order_outer => 'ORDER__BY__1 DESC, ORDER__BY__2 DESC',
-    order_req => 'ORDER__BY__1 ASC, ORDER__BY__2 ASC',
-    exselect_outer => 'ORDER__BY__1, ORDER__BY__2',
-    exselect_inner => 'title AS ORDER__BY__1, bar AS ORDER__BY__2',
+    order_outer => 'ORDER__BY__001 DESC, ORDER__BY__002 DESC',
+    order_req => 'ORDER__BY__001 ASC, ORDER__BY__002 ASC',
+    exselect_outer => 'ORDER__BY__001, ORDER__BY__002',
+    exselect_inner => 'title AS ORDER__BY__001, bar AS ORDER__BY__002',
   },
   {
     order_by => [
@@ -149,10 +149,10 @@ for my $ord_set (
       { -asc  => [qw{me.owner sensors}]},
     ],
     order_inner => 'title, bar DESC, me.owner ASC, sensors ASC',
-    order_outer => 'ORDER__BY__1 DESC, ORDER__BY__2 ASC, me.owner DESC, ORDER__BY__3 DESC',
-    order_req => 'ORDER__BY__1, ORDER__BY__2 DESC, me.owner ASC, ORDER__BY__3 ASC',
-    exselect_outer => 'ORDER__BY__1, ORDER__BY__2, ORDER__BY__3',
-    exselect_inner => 'title AS ORDER__BY__1, bar AS ORDER__BY__2, sensors AS ORDER__BY__3',
+    order_outer => 'ORDER__BY__001 DESC, ORDER__BY__002 ASC, me.owner DESC, ORDER__BY__003 DESC',
+    order_req => 'ORDER__BY__001, ORDER__BY__002 DESC, me.owner ASC, ORDER__BY__003 ASC',
+    exselect_outer => 'ORDER__BY__001, ORDER__BY__002, ORDER__BY__003',
+    exselect_inner => 'title AS ORDER__BY__001, bar AS ORDER__BY__002, sensors AS ORDER__BY__003',
   },
 ) {
   my $o_sel = $ord_set->{exselect_outer}
@@ -192,22 +192,22 @@ is_same_sql_bind (
   $books_45_and_owners->search ({}, { group_by => 'title', order_by => 'title' })->as_query,
   '(SELECT me.id, me.source, me.owner, me.price, owner.id, owner.name
       FROM (
-        SELECT me.id, me.source, me.owner, me.price, ORDER__BY__1 AS title
+        SELECT me.id, me.source, me.owner, me.price, ORDER__BY__001 AS title
           FROM (
             SELECT TOP 2
-                me.id, me.source, me.owner, me.price, ORDER__BY__1
+                me.id, me.source, me.owner, me.price, ORDER__BY__001
               FROM (
                 SELECT TOP 5
-                    me.id, me.source, me.owner, me.price, title AS ORDER__BY__1
+                    me.id, me.source, me.owner, me.price, title AS ORDER__BY__001
                   FROM books me
                   JOIN owners owner ON owner.id = me.owner
                 WHERE ( source = ? )
                 GROUP BY title
                 ORDER BY title
               ) me
-            ORDER BY ORDER__BY__1 DESC
+            ORDER BY ORDER__BY__001 DESC
           ) me
-        ORDER BY ORDER__BY__1
+        ORDER BY ORDER__BY__001
       ) me
       JOIN owners owner ON owner.id = me.owner
     WHERE ( source = ? )
index 7bb116b..7956f52 100644 (file)
@@ -149,10 +149,10 @@ my $tests = {
     my $ordered_sql = '(
       SELECT me.id, owner__id, owner__name, bar, baz
         FROM (
-          SELECT me.id, owner__id, owner__name, bar, baz, ROW_NUMBER() OVER( ORDER BY ORDER__BY__1, ORDER__BY__2 ) AS rno__row__index
+          SELECT me.id, owner__id, owner__name, bar, baz, ROW_NUMBER() OVER( ORDER BY ORDER__BY__001, ORDER__BY__002 ) AS rno__row__index
             FROM (
               SELECT me.id, owner.id AS owner__id, owner.name AS owner__name, ? * ? AS bar, ? AS baz,
-                     ? / ? AS ORDER__BY__1, ? AS ORDER__BY__2
+                     ? / ? AS ORDER__BY__001, ? AS ORDER__BY__002
                 FROM books me
                 JOIN owners owner
                   ON owner.id = me.owner
@@ -373,9 +373,9 @@ my $tests = {
       '(
         SELECT me.id, owner__id, owner__name, bar, baz
           FROM (
-            SELECT me.id, owner__id, owner__name, bar, baz, ORDER__BY__1, ORDER__BY__2
+            SELECT me.id, owner__id, owner__name, bar, baz, ORDER__BY__001, ORDER__BY__002
               FROM (
-                SELECT me.id, owner.id AS owner__id, owner.name AS owner__name, ? * ? AS bar, ? AS baz, ? / ? AS ORDER__BY__1, ? AS ORDER__BY__2
+                SELECT me.id, owner.id AS owner__id, owner.name AS owner__name, ? * ? AS bar, ? AS baz, ? / ? AS ORDER__BY__001, ? AS ORDER__BY__002
                   FROM books me
                   JOIN owners owner
                     ON owner.id = me.owner
@@ -385,10 +385,10 @@ my $tests = {
                 ORDER BY ? / ?, ?
                 FETCH FIRST 7 ROWS ONLY
               ) me
-            ORDER BY ORDER__BY__1 DESC, ORDER__BY__2 DESC
+            ORDER BY ORDER__BY__001 DESC, ORDER__BY__002 DESC
             FETCH FIRST 4 ROWS ONLY
           ) me
-        ORDER BY ORDER__BY__1, ORDER__BY__2
+        ORDER BY ORDER__BY__001, ORDER__BY__002
       )',
       [
         @select_bind,
@@ -464,9 +464,9 @@ my $tests = {
       '(
         SELECT me.id, owner__id, owner__name, bar, baz
           FROM (
-            SELECT TOP 4 me.id, owner__id, owner__name, bar, baz, ORDER__BY__1, ORDER__BY__2
+            SELECT TOP 4 me.id, owner__id, owner__name, bar, baz, ORDER__BY__001, ORDER__BY__002
               FROM (
-                SELECT TOP 7 me.id, owner.id AS owner__id, owner.name AS owner__name, ? * ? AS bar, ? AS baz, ? / ? AS ORDER__BY__1, ? AS ORDER__BY__2
+                SELECT TOP 7 me.id, owner.id AS owner__id, owner.name AS owner__name, ? * ? AS bar, ? AS baz, ? / ? AS ORDER__BY__001, ? AS ORDER__BY__002
                   FROM books me
                   JOIN owners owner
                     ON owner.id = me.owner
@@ -475,9 +475,9 @@ my $tests = {
                 HAVING ?
                 ORDER BY ? / ?, ?
               ) me
-            ORDER BY ORDER__BY__1 DESC, ORDER__BY__2 DESC
+            ORDER BY ORDER__BY__001 DESC, ORDER__BY__002 DESC
           ) me
-        ORDER BY ORDER__BY__1, ORDER__BY__2
+        ORDER BY ORDER__BY__001, ORDER__BY__002
       )',
       [
         @select_bind,