Skip to content

Commit

Permalink
Fixes #10 : Fix incorrect DbSelect::count() when setted \PDO::ATTR_CA…
Browse files Browse the repository at this point in the history
…SE => \PDO::CASE_LOWER in driver options

Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
samsonasik committed Aug 5, 2020
1 parent ac3332b commit 4f34a05
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Adapter/DbSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Laminas\Paginator\Adapter;

use Laminas\Db\Adapter\Adapter;
use Laminas\Db\Adapter\Driver\Pdo\Pdo;
use Laminas\Db\ResultSet\ResultSet;
use Laminas\Db\ResultSet\ResultSetInterface;
use Laminas\Db\Sql\Expression;
Expand All @@ -18,6 +19,7 @@
class DbSelect implements AdapterInterface
{
const ROW_COUNT_COLUMN_NAME = 'C';
const ROW_COUNT_COLUMN_NAME_SMALL = 'c';

/**
* @var Sql
Expand Down Expand Up @@ -122,7 +124,7 @@ public function count()
$result = $statement->execute();
$row = $result->current();

$this->rowCount = (int) $row[self::ROW_COUNT_COLUMN_NAME];
$this->rowCount = (int) $row[$this->getRowCountColumnName()];

return $this->rowCount;
}
Expand All @@ -145,7 +147,7 @@ protected function getSelectCount()

$countSelect = new Select;

$countSelect->columns([self::ROW_COUNT_COLUMN_NAME => new Expression('COUNT(1)')]);
$countSelect->columns([$this->getRowCountColumnName() => new Expression('COUNT(1)')]);
$countSelect->from(['original_select' => $select]);

return $countSelect;
Expand All @@ -165,4 +167,14 @@ public function getArrayCopy()
),
];
}

private function getRowCountColumnName()
{
$driver = $this->sql->getAdapter()->getDriver();
if (get_class($driver) === Pdo::class && $driver->getConnection()->getResource()->getAttribute(\PDO::ATTR_CASE) !== \PDO::CASE_LOWER) {
return self::ROW_COUNT_COLUMN_NAME;
}

return self::ROW_COUNT_COLUMN_NAME_SMALL;
}
}

0 comments on commit 4f34a05

Please sign in to comment.