Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bdee325

Browse files
committed
Work on quick pagination for larger data-sets.
This provides support for running pagination without a separate count query for data-sets that do not need page numbers.
1 parent c17c5a1 commit bdee325

7 files changed

Lines changed: 34 additions & 35 deletions

File tree

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,23 @@ protected function ungroupedPaginate($paginator, $perPage, $columns)
284284
}
285285

286286
/**
287-
* Get a paginator in cursor mode for the "select" statement.
287+
* Get a paginator only supporting simple next and previous links.
288288
*
289+
* This is more efficient on larger data-sets, etc.
290+
*
291+
* @param \Illuminate\Pagination\Factory $paginator
289292
* @param int $perPage
290293
* @param array $columns
291294
* @return \Illuminate\Pagination\Paginator
292295
*/
293-
public function cursor($perPage = null, $columns = array('*'))
296+
public function quickPaginate($perPage = null, $columns = array('*'))
294297
{
295298
$paginator = $this->query->getConnection()->getPaginator();
296299

297300
$page = $paginator->getCurrentPage();
301+
298302
$perPage = $perPage ?: $this->model->getPerPage();
299303

300-
// Use skip method to set correct offset and take perPage + 1 items.
301304
$this->query->skip(($page - 1) * $perPage)->take($perPage + 1);
302305

303306
return $paginator->make($this->get($columns)->all(), $perPage);

src/Illuminate/Database/Query/Builder.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,20 +1568,22 @@ public function getPaginationCount()
15681568
}
15691569

15701570
/**
1571-
* Get a paginator in cursor mode for the "select" statement.
1571+
* Get a paginator only supporting simple next and previous links.
1572+
*
1573+
* This is more efficient on larger data-sets, etc.
15721574
*
15731575
* @param int $perPage
15741576
* @param array $columns
15751577
* @return \Illuminate\Pagination\Paginator
15761578
*/
1577-
public function cursor($perPage = null, $columns = array('*'))
1579+
public function quickPaginate($perPage = null, $columns = array('*'))
15781580
{
15791581
$paginator = $this->connection->getPaginator();
15801582

15811583
$page = $paginator->getCurrentPage();
1584+
15821585
$perPage = $perPage ?: $this->model->getPerPage();
15831586

1584-
// Use skip method to set correct offset and take perPage + 1 items.
15851587
$this->skip(($page - 1) * $perPage)->take($perPage + 1);
15861588

15871589
return $paginator->make($this->get($columns), $perPage);

src/Illuminate/Pagination/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected function setupPaginationEnvironment()
9595
*
9696
* @param array $items
9797
* @param int $total
98-
* @param mixed $perPage
98+
* @param int|null $perPage
9999
* @return \Illuminate\Pagination\Paginator
100100
*/
101101
public function make(array $items, $total, $perPage = null)

src/Illuminate/Pagination/Paginator.php

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class Paginator implements ArrayableInterface, ArrayAccess, Countable, IteratorA
3232
protected $total;
3333

3434
/**
35-
* Item, array or object indicating if next page is available.
35+
* Indicates if a pagination doing "quick" pagination has more items.
3636
*
37-
* @var mixed
37+
* @var bool
3838
*/
39-
protected $cursor;
39+
protected $hasMore;
4040

4141
/**
4242
* The amount of items to show per page.
@@ -100,15 +100,11 @@ public function __construct(Factory $factory, array $items, $total, $perPage = n
100100
{
101101
$this->factory = $factory;
102102

103-
// Paginator received only 3 parameters which means that it's being used
104-
// in cursor mode. In this mode third argument $total is used as $perPage.
105103
if (is_null($perPage))
106104
{
107-
$this->items = array_slice($items, 0, $perPage);
108105
$this->perPage = (int) $total;
109-
110-
$cursor = array_slice($items, $this->perPage, 1);
111-
$this->cursor = empty($cursor) ? null : $cursor[0];
106+
$this->items = array_slice($items, 0, $perPage);
107+
$this->hasMore = count(array_slice($items, $this->perPage, 1)) > 0;
112108
}
113109
else
114110
{
@@ -139,12 +135,11 @@ public function setupPaginationContext()
139135
*/
140136
protected function calculateCurrentAndLastPages()
141137
{
142-
if (is_null($this->total))
138+
if ($this->isQuickPaginating())
143139
{
144-
$page = $this->factory->getCurrentPage();
145-
$this->currentPage = $this->isValidPageNumber($page) ? (int) $page : 1;
140+
$this->currentPage = $this->factory->getCurrentPage();
146141

147-
$this->lastPage = is_null($this->cursor) ? $this->currentPage : $this->currentPage + 1;
142+
$this->lastPage = $this->hasMore ? $this->currentPage + 1 : $this->currentPage;
148143
}
149144
else
150145
{
@@ -301,6 +296,16 @@ public function addQuery($key, $value)
301296
return $this;
302297
}
303298

299+
/**
300+
* Deteremine if the paginator is doing "quick" pagination.
301+
*
302+
* @return bool
303+
*/
304+
public function isQuickPaginating()
305+
{
306+
return is_null($this->total);
307+
}
308+
304309
/**
305310
* Get the current page for the request.
306311
*
@@ -400,16 +405,6 @@ public function getTotal()
400405
return $this->total;
401406
}
402407

403-
/**
404-
* Get cursor for this paginator.
405-
*
406-
* @return mixed
407-
*/
408-
public function getCursor()
409-
{
410-
return $this->cursor;
411-
}
412-
413408
/**
414409
* Set the base URL in use by the paginator.
415410
*

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public function testPaginateMethodWithGroupedQuery()
222222
}
223223

224224

225-
public function testCursorMethod()
225+
public function testQuickPaginateMethod()
226226
{
227227
$query = $this->getMock('Illuminate\Database\Query\Builder', array('from', 'getConnection', 'skip', 'take'), array(
228228
m::mock('Illuminate\Database\ConnectionInterface'),
@@ -243,7 +243,7 @@ public function testCursorMethod()
243243
$builder->expects($this->once())->method('get')->with($this->equalTo(array('*')))->will($this->returnValue(new Collection(array('results'))));
244244
$paginator->shouldReceive('make')->once()->with(array('results'), 15)->andReturn(array('results'));
245245

246-
$this->assertEquals(array('results'), $builder->cursor());
246+
$this->assertEquals(array('results'), $builder->quickPaginate());
247247
}
248248

249249

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ public function testGetPaginationCountGetsResultCount()
690690
}
691691

692692

693-
public function testCursorCorrectlyCreatesPaginatorInstance()
693+
public function testQuickPaginateCorrectlyCreatesPaginatorInstance()
694694
{
695695
$connection = m::mock('Illuminate\Database\ConnectionInterface');
696696
$grammar = m::mock('Illuminate\Database\Query\Grammars\Grammar');
@@ -704,7 +704,7 @@ public function testCursorCorrectlyCreatesPaginatorInstance()
704704
$builder->expects($this->once())->method('get')->with($this->equalTo(array('*')))->will($this->returnValue(array('foo')));
705705
$paginator->shouldReceive('make')->once()->with(array('foo'), 15)->andReturn(array('results'));
706706

707-
$this->assertEquals(array('results'), $builder->cursor(15, array('*')));
707+
$this->assertEquals(array('results'), $builder->quickPaginate(15, array('*')));
708708
}
709709

710710

tests/Pagination/PaginationPaginatorTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public function testPaginationContextIsSetupCorrectlyInCursorMode()
3030

3131
$this->assertEquals(2, $p->getLastPage());
3232
$this->assertEquals(1, $p->getCurrentPage());
33-
$this->assertEquals('baz', $p->getCursor());
3433
}
3534

3635

0 commit comments

Comments
 (0)