Skip to content

Commit

Permalink
Merge pull request #12 from CottaCush/features/report-pagination
Browse files Browse the repository at this point in the history
(Query): Implement pagination on report queries
  • Loading branch information
otaruMendez authored May 6, 2019
2 parents d816898 + de1a473 commit 391e041
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
21 changes: 18 additions & 3 deletions src/generators/sql/SQLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class SQLGenerator implements GeneratorInterface
const QUERY_ONE = 'queryOne';
const QUERY_COLUMN = 'queryColumn';
const QUERY_SCALAR = 'queryScalar';
const QUERY_LIMIT = 20;

public function __construct($query, Connection $db = null)
{
Expand All @@ -39,19 +40,33 @@ public function __construct($query, Connection $db = null)
/**
* @author Taiwo Ladipo <[email protected]>
* @param string $function
* @param bool $paginate
* @param null $page
* @return array
* @throws SQLQueryGenerationException
*/
public function generateResult($function = self::QUERY_ALL)
public function generateResult($function = self::QUERY_ALL, $paginate = false, $page = null)
{
try {
$this->openConnection();
$result = $this->db->createCommand($this->query)->{$function}();
$count = 0;
$this->query = rtrim($this->query, ';');
if ($paginate) {
if (!$page) { // first page of pagination
$count = $this->db->createCommand("SELECT COUNT(*) as total FROM ($this->query) a")
->{self::QUERY_SCALAR}();
}
$from = $page ? ($page - 1) * self::QUERY_LIMIT : 0;
$result = $this->db->createCommand("$this->query LIMIT $from, " . self::QUERY_LIMIT)
->{self::QUERY_ALL}();
} else {
$result = $this->db->createCommand($this->query)->{$function}();
}
$this->closeConnection();
} catch (Exception $e) {
throw new SQLQueryGenerationException($e->getMessage());
}
return $result;
return ['data' => $result, 'count' => $count];
}

/**
Expand Down
30 changes: 22 additions & 8 deletions src/generators/sql/SQLQueryBuilderParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace CottaCush\Cricket\Generators\SQL;

use CottaCush\Cricket\Interfaces\CricketQueryableInterface;
use CottaCush\Cricket\Models\Query;
use CottaCush\Cricket\Traits\ValueGetter;
use yii\helpers\ArrayHelper;
use CottaCush\Cricket\Models\Query;

class SQLQueryBuilderParser
{
Expand All @@ -18,25 +19,37 @@ class SQLQueryBuilderParser

/**
* @author Olawale Lawal <[email protected]>
* @author Taiwo Ladipo <[email protected]>
* @param CricketQueryableInterface $report
* @param array $placeholderValues
* @param null $db
* @param string $function
* @param array $paginationExtras
* @return array
* @throws \CottaCush\Cricket\Exceptions\SQLQueryGenerationException
*/
public function parse(
CricketQueryableInterface $report,
$placeholderValues = [],
$db = null,
$function = SQLGenerator::QUERY_ALL
$function = SQLGenerator::QUERY_ALL,
$paginationExtras = []
) {
/** @var Query $queryObj */
$queryObj = $report->query;
$page = ArrayHelper::getValue($paginationExtras, 'page');
$paginate = boolval(ArrayHelper::getValue($paginationExtras, 'paginate'));

$shouldReplacePlaceholders = !empty($placeholderValues); //Should the placeholders be replaced in the query
$this->hasInputPlaceholders = count($queryObj->inputPlaceholders);

$this->query = $queryObj->getQuery();
if (!empty($paginationExtras) && $page) {
$this->query = ArrayHelper::getValue($paginationExtras, 'query_string');
$this->hasPlaceholdersReplaced = true;
} else {
$this->query = $queryObj->getQuery();
}

$data = [];

$dashboardFilterPlaceholders = $queryObj->getDashboardFilterPlaceholders()->all();
Expand All @@ -49,7 +62,7 @@ public function parse(
$data = $generator->generateResult($function);
}

if (!$this->hasInputPlaceholders) { // Query has only session placeholders or none
if (!$this->hasInputPlaceholders && !$paginate) { // Report has only session placeholders or none
$shouldReplacePlaceholders = true;
$placeholders = $queryObj->sessionPlaceholders;
$placeholderValues = [];
Expand All @@ -59,18 +72,19 @@ public function parse(
}
}

if ($shouldReplacePlaceholders) { //If there are placeholders to be injected into the query before execution
//If there are placeholders to be injected into the query before execution and not the first page
if ($shouldReplacePlaceholders && !$page) {
$queryBuilder = new SQLQueryBuilder($queryObj, $placeholderValues);
$this->query = $queryBuilder->buildQuery();

$generator = new SQLGenerator($this->query, $db);
$data = $generator->generateResult($function);
$data = $generator->generateResult($function, $paginate, $page);

$this->hasPlaceholdersReplaced = true;
} else {
if (!$this->hasInputPlaceholders) { //No session placeholders and no submission
if (!$this->hasInputPlaceholders || $page) { //No session placeholders and no submission
$generator = new SQLGenerator($this->query, $db);
$data = $generator->generateResult($function);
$data = $generator->generateResult($function, $paginate, $page);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/generators/sql/SQLQueryFilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private function generateDropdown($value = null)
//Build the query from the report to get data
$queryBuilder = new SQLQueryBuilder($dropdownQuery, $placeholderValues);
$generator = new SQLGenerator($queryBuilder->buildQuery(), $this->database);
$data = $generator->generateResult();
$data = ArrayHelper::getValue($generator->generateResult(), 'data');

if (count($data)) {
$data = ArrayHelper::map($data, 'key', 'value');
Expand Down

0 comments on commit 391e041

Please sign in to comment.