Add count, paginate and simplePaginate (#37)

This commit is contained in:
jcsoriano
2025-02-27 19:18:07 +05:00
committed by GitHub
parent e919c58330
commit 35af68bcdb
2 changed files with 115 additions and 0 deletions
+81
View File
@@ -6,6 +6,9 @@ namespace PhpClickHouseLaravel;
use ClickHouseDB\Client;
use ClickHouseDB\Statement;
use Closure;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use PhpClickHouseLaravel\Exceptions\QueryException;
use Tinderbox\ClickhouseBuilder\Query\BaseBuilder;
@@ -130,4 +133,82 @@ class Builder extends BaseBuilder
return new static($this->client);
}
public function count(): int
{
$builder = $this->getCountQuery();
$result = $builder->getRows();
return (int) $result[0]['count'] ?? 0;
}
/**
* Paginate the given query.
*
* @param int|null|\Closure $perPage
* @param array|string $columns
* @param string $pageName
* @param int|null $page
* @param \Closure|int|null $total
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*
* @throws \InvalidArgumentException
*/
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);
$count = (clone $this)->count();
$perPage = ($perPage instanceof Closure
? $perPage($count)
: $perPage
) ?: 15;
$results = $this->limit($perPage, $perPage * ($page - 1))
->getRows();
return new LengthAwarePaginator(
$results,
$count,
$perPage,
$page,
[
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
}
/**
* Paginate the given query into a simple paginator.
*
* @param int|null $perPage
* @param array|string $columns
* @param string $pageName
* @param int|null $page
* @return \Illuminate\Contracts\Pagination\Paginator
*/
public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);
$perPage = $perPage ?: 15;
// Next we will set the limit and offset for this query so that when we get the
// results we get the proper section of results. Then, we'll create the full
// paginator instances for these results with the given page and per page.
$results = $this->limit($perPage + 1, ($page - 1) * $perPage)
->getRows();
return new Paginator(
$results,
$perPage,
$page,
[
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
],
);
}
}
+34
View File
@@ -2,6 +2,8 @@
namespace Tests;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use PhpClickHouseLaravel\Builder;
@@ -31,6 +33,38 @@ class BaseTest extends TestCase
$this->assertEquals('zz', $rows[0]['f_string']);
}
public function testSimpleModelInsertAndPaginate()
{
Example::truncate();
Example::insertAssoc([
['f_int' => 1, 'f_string' => 'zz'],
['f_int' => 2, 'f_string' => 'aa'],
['f_int' => 3, 'f_string' => 'bb'],
]);
$result = Example::select()->paginate(2);
$this->assertTrue($result instanceof LengthAwarePaginator);
$this->assertEquals(3, $result->total());
$this->assertCount(2, $result->items());
$this->assertEquals(1, $result->items()[0]['f_int']);
$this->assertEquals('zz', $result->items()[0]['f_string']);
}
public function testSimpleModelInsertAndSimplePaginate()
{
Example::truncate();
Example::insertAssoc([
['f_int' => 1, 'f_string' => 'zz'],
['f_int' => 2, 'f_string' => 'aa'],
['f_int' => 3, 'f_string' => 'bb'],
]);
$result = Example::select()->simplePaginate(2);
$this->assertTrue($result instanceof Paginator);
$this->assertCount(2, $result->items());
$this->assertEquals(2, $result->perPage());
$this->assertEquals(1, $result->items()[0]['f_int']);
$this->assertEquals('zz', $result->items()[0]['f_string']);
}
public function testMultipleWheres()
{
Example::truncate();