Massive rewrite of bind handling, and overall simplification of ::Storage::DBI
[dbsrgits/DBIx-Class.git] / t / prefetch / with_limit.t
index 4acdcab..977a3f9 100644 (file)
@@ -31,6 +31,66 @@ my $use_prefetch = $no_prefetch->search(
   }
 );
 
+# add a floating +select to make sure it does not throw things off
+# we also expect it to appear in both selectors, as we can not know
+# for sure which part of the query it applies to (may be order_by,
+# maybe something else)
+#
+# we use a reference to the same array in bind vals, because
+# is_deeply picks up this difference too (not sure if bug or
+# feature)
+$use_prefetch = $use_prefetch->search({}, {
+  '+select' => \[ 'me.artistid + ?', [ \ 'inTEger' => 1 ] ],
+});
+
+my $bind_int_resolved = sub { [ { sqlt_datatype => 'inTEger' } => 1 ] };
+my $bind_vc_resolved = sub { [
+  { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'tracks.title' }
+    => 'blah-blah-1234568'
+] };
+is_same_sql_bind (
+  $use_prefetch->as_query,
+  '(
+    SELECT  me.artistid, me.name,
+            cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track,
+            me.artistid + ?
+      FROM (
+        SELECT me.artistid, me.name,
+               me.artistid + ?
+          FROM artist me
+          LEFT JOIN cd cds
+            ON cds.artist = me.artistid
+          LEFT JOIN cd_artwork artwork
+            ON artwork.cd_id = cds.cdid
+          LEFT JOIN track tracks
+            ON tracks.cd = cds.cdid
+        WHERE   artwork.cd_id IS NULL
+             OR tracks.title != ?
+        GROUP BY me.artistid, me.name, me.artistid + ?
+        ORDER BY name DESC LIMIT 3
+      ) me
+      LEFT JOIN cd cds
+        ON cds.artist = me.artistid
+      LEFT JOIN cd_artwork artwork
+        ON artwork.cd_id = cds.cdid
+      LEFT JOIN track tracks
+        ON tracks.cd = cds.cdid
+    WHERE artwork.cd_id IS NULL
+       OR tracks.title != ?
+    GROUP BY me.artistid, me.name, cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track, me.artistid + ?
+    ORDER BY name DESC, cds.artist, cds.year ASC
+  )',
+  [
+    $bind_int_resolved->(),  # outer select
+    $bind_int_resolved->(),  # inner select
+    $bind_vc_resolved->(), # inner where
+    $bind_int_resolved->(),  # inner group_by
+    $bind_vc_resolved->(), # outer where
+    $bind_int_resolved->(),  # outer group_by
+  ],
+  'Expected SQL on complex limited prefetch'
+);
+
 is($no_prefetch->count, $use_prefetch->count, '$no_prefetch->count == $use_prefetch->count');
 is(
   scalar ($no_prefetch->all),
@@ -128,12 +188,12 @@ is_same_sql_bind (
     WHERE ( ( artist.name = ? AND me.year = ? ) )
     ORDER BY tracks.cd
   )',
-  [
-    [ 'artist.name' => 'foo' ],
-    [ 'me.year'     => 2010  ],
-    [ 'artist.name' => 'foo' ],
-    [ 'me.year'     => 2010  ],
-  ],
+  [ map {
+    [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'artist.name' }
+      => 'foo' ],
+    [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
+      => 2010 ],
+  } (1,2)],
   'No grouping of non-multiplying resultsets',
 );