No more $self->search in Positioned.
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / Positioned.pm
index 8fe2d36..d3b7cf7 100644 (file)
@@ -100,7 +100,7 @@ excluding the one you called it on.
 sub siblings {
     my( $self ) = @_;
     my $position_column = $self->position_column;
-    my $rs = $self->search(
+    my $rs = $self->result_source->resultset->search(
         {
             $position_column => { '!=' => $self->get_column($position_column) },
             $self->_collection_clause(),
@@ -121,9 +121,11 @@ Returns the first sibling object.
 
 sub first_sibling {
     my( $self ) = @_;
-    return ($self->search(
-        { $self->_collection_clause() },
-        { rows=>1, order_by => $self->position_column },
+    return ($self->result_source->resultset->search(
+        {
+            $self->position_column => 1,
+            $self->_collection_clause(),
+        },
     )->all())[0];
 }
 
@@ -137,9 +139,12 @@ Return the last sibling.
 
 sub last_sibling {
     my( $self ) = @_;
-    return ($self->search(
-        { $self->_collection_clause() },
-        { rows=>1, order_by => $self->position_column.' DESC' },
+    my $count = $self->result_source->resultset->search({$self->_collection_clause()})->count();
+    return ($self->result_source->resultset->search(
+        {
+            $self->position_column => $count,
+            $self->_collection_clause(),
+        },
     )->all())[0];
 }
 
@@ -155,12 +160,13 @@ is returned if the current object is the first one.
 sub previous_sibling {
     my( $self ) = @_;
     my $position_column = $self->position_column;
-    return ($self->search(
+    my $position = $self->get_column( $position_column );
+    return 0 if ($position==1);
+    return ($self->result_source->resultset->search(
         {
-            $position_column => { '<' => $self->get_column($position_column) },
+            $position_column => $position - 1,
             $self->_collection_clause(),
-        },
-        { rows=>1, order_by => $position_column.' DESC' },
+        }
     )->all())[0];
 }
 
@@ -176,12 +182,14 @@ is returned if the current object is the last one.
 sub next_sibling {
     my( $self ) = @_;
     my $position_column = $self->position_column;
+    my $position = $self->get_column( $position_column );
+    my $count = $self->result_source->resultset->search({$self->_collection_clause()})->count();
+    return 0 if ($position==$count);
     return ($self->result_source->resultset->search(
         {
-            $position_column => { '>' => $self->get_column($position_column) },
+            $position_column => $position + 1,
             $self->_collection_clause(),
         },
-        { rows=>1, order_by => $position_column },
     )->all())[0];
 }