The Top limit emulation bundled with SQLA::Limit assumes that the limited resultset...
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSet.pm
index d91d944..e5ce095 100644 (file)
@@ -661,6 +661,8 @@ sub cursor {
   my ($self) = @_;
 
   my $attrs = $self->_resolved_attrs_copy;
+  $attrs->{_virtual_order_by} = $self->_gen_virtual_order;
+
   return $self->{cursor}
     ||= $self->result_source->storage->select($attrs->{from}, $attrs->{select},
           $attrs->{where},$attrs);
@@ -712,6 +714,8 @@ sub single {
   }
 
   my $attrs = $self->_resolved_attrs_copy;
+  $attrs->{_virtual_order_by} = $self->_gen_virtual_order;
+
   if ($where) {
     if (defined $attrs->{where}) {
       $attrs->{where} = {
@@ -738,6 +742,32 @@ sub single {
   return (@data ? ($self->_construct_object(@data))[0] : undef);
 }
 
+# _gen_virtual_order
+#
+# This is a horrble hack, but seems like the best we can do at this point
+# Some limit emulations (Top) require an ordered resultset in order to 
+# function at all. So supply a PK order if such a condition is detected
+
+sub _gen_virtual_order {
+  my $self = shift;
+  my $attrs = $self->_resolved_attrs_copy;
+
+  if ($attrs->{rows} or $attrs->{offset} ) {
+
+#   This check requires ensure_connected, so probably cheaper to just calculate all the time
+
+#    my $sm = $self->result_source->storage->_sql_maker;
+#
+#    if ($sm->_default_limit_syntax eq 'Top' and not @{$sm->_resolve_order ($attrs->{order_by}) }) {
+
+      return [ $self->result_source->primary_columns ];
+
+#    }
+  }
+
+  return undef;
+}
+
 # _is_unique_query
 #
 # Try to determine if the specified query is guaranteed to be unique, based on