diff --git a/src/Adapter/DbSelect.php b/src/Adapter/DbSelect.php index 154bc5b9..742787f7 100644 --- a/src/Adapter/DbSelect.php +++ b/src/Adapter/DbSelect.php @@ -9,7 +9,6 @@ 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; @@ -19,7 +18,6 @@ class DbSelect implements AdapterInterface { const ROW_COUNT_COLUMN_NAME = 'C'; - const ROW_COUNT_COLUMN_NAME_SMALL = 'c'; /** * @var Sql @@ -124,7 +122,9 @@ public function count() $result = $statement->execute(); $row = $result->current(); - $this->rowCount = (int) $row[$this->getRowCountColumnName()]; + $this->rowCount = (int) isset($row[self::ROW_COUNT_COLUMN_NAME]) + ? $row[self::ROW_COUNT_COLUMN_NAME] + : $row['c']; return $this->rowCount; } @@ -147,7 +147,10 @@ protected function getSelectCount() $countSelect = new Select; - $countSelect->columns([$this->getRowCountColumnName() => new Expression('COUNT(1)')]); + $countSelect->columns([ + self::ROW_COUNT_COLUMN_NAME => new Expression('COUNT(1)'), + 'c' => new Expression('COUNT(1)') + ]); $countSelect->from(['original_select' => $select]); return $countSelect; @@ -167,19 +170,4 @@ public function getArrayCopy() ), ]; } - - private function getRowCountColumnName() - { - $driver = $this->sql->getAdapter()->getDriver(); - if (get_class($driver) !== Pdo::class) { - return self::ROW_COUNT_COLUMN_NAME; - } - - $attrCase = $driver->getConnection() - ->getResource() - ->getAttribute(\PDO::ATTR_CASE); - return $attrCase !== \PDO::CASE_LOWER - ? self::ROW_COUNT_COLUMN_NAME - : self::ROW_COUNT_COLUMN_NAME_SMALL; - } } diff --git a/test/Adapter/DbSelectTest.php b/test/Adapter/DbSelectTest.php index 3ea03f44..142d833b 100644 --- a/test/Adapter/DbSelectTest.php +++ b/test/Adapter/DbSelectTest.php @@ -66,15 +66,6 @@ public function setUp() ->with($this->isInstanceOf('Laminas\Db\Sql\Select')) ->will($this->returnValue($this->mockStatement)); - $this->mockAdapter = $this->getMockBuilder('Laminas\Db\Adapter\AdapterInterface')->getMock(); - $this->mockAdapter->expects($this->any()) - ->method('getDriver') - ->will($this->returnValue($mockDriver)); - $this->mockSql - ->expects($this->any()) - ->method('getAdapter') - ->will($this->returnValue($this->mockAdapter)); - $this->mockSelect = $this->createMock('Laminas\Db\Sql\Select'); $this->mockSelectCount = $this->createMock('Laminas\Db\Sql\Select'); $this->dbSelect = new DbSelect($this->mockSelect, $this->mockSql); @@ -99,6 +90,18 @@ public function testCount() $this->assertEquals(5, $count); } + public function testCountQueryWithLowerColumnNameShouldReturnValidResult() + { + $this->dbSelect = new DbSelect($this->mockSelect, $this->mockSql); + $this->mockResult + ->expects($this->once()) + ->method('current') + ->will($this->returnValue(['c' => 7])); + + $count = $this->dbSelect->count(); + $this->assertEquals(7, $count); + } + public function testCustomCount() { $this->dbSelect = new DbSelect($this->mockSelect, $this->mockSql, null, $this->mockSelectCount);