Skip to content

Commit

Permalink
Rework getFiltered, getTotal in PdoSource
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Nov 17, 2024
1 parent d074f06 commit 564e646
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 123 deletions.
84 changes: 0 additions & 84 deletions README.bak

This file was deleted.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ use Rougin\Datatables\Table;
interface SourceInterface
{
/**
* Returns the total items after filter.
* Returns the total items after filter. If no filters
* are defined, the value should be same with getTotal.
*
* @return integer
*/
Expand Down
72 changes: 35 additions & 37 deletions src/Source/PdoSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,14 @@ public function __construct(\PDO $pdo)
}

/**
* TODO: If no filters, the value should be same with getTotal.
*
* Returns the total items after filter.
* Returns the total items after filter. If no filters
* are defined, the value should be same with getTotal.
*
* @return integer
*/
public function getFiltered()
{
// Reset values prior creating query ---
$this->values = array();
// -------------------------------------

$table = $this->table->getName() . ' ';

$query = 'SELECT COUNT(*) FROM ' . $table;

$query .= $this->setWhereQuery() . ' ';

/** @var \PDOStatement */
$stmt = $this->pdo->prepare($query);

$stmt->execute($this->values);

/** @var integer */
$total = $stmt->fetch(\PDO::FETCH_COLUMN);

return (int) $total;
return $this->getTotalItems(true);
}

/**
Expand Down Expand Up @@ -137,27 +118,13 @@ public function getItems()
}

/**
* TODO: Merge logic with getFiltered.
*
* Returns the total items from the source.
*
* @return integer
*/
public function getTotal()
{
$table = $this->table->getName();

$query = 'SELECT COUNT(*) FROM ' . $table;

/** @var \PDOStatement */
$stmt = $this->pdo->prepare($query);

$stmt->execute();

/** @var integer */
$total = $stmt->fetch(\PDO::FETCH_COLUMN);

return (int) $total;
return $this->getTotalItems();
}

/**
Expand Down Expand Up @@ -188,6 +155,37 @@ public function setTable(Table $table)
return $this;
}

/**
* @param boolean $filter
*
* @return integer
*/
protected function getTotalItems($filter = false)
{
// Reset values prior creating query ---
$this->values = array();
// -------------------------------------

$table = $this->table->getName() . ' ';

$query = 'SELECT COUNT(*) FROM ' . $table;

if ($filter)
{
$query .= $this->setWhereQuery() . ' ';
}

/** @var \PDOStatement */
$stmt = $this->pdo->prepare(trim($query));

$stmt->execute($this->values);

/** @var integer */
$total = $stmt->fetch(\PDO::FETCH_COLUMN);

return (int) $total;
}

/**
* @return string
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Source/SourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
interface SourceInterface
{
/**
* Returns the total items after filter.
* Returns the total items after filter. If no filters
* are defined, the value should be same with getTotal.
*
* @return integer
*/
Expand Down

0 comments on commit 564e646

Please sign in to comment.