Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #10 : Fix incorrect DbSelect::count() when setted \PDO::ATTR_CASE => \PDO::CASE_LOWER in driver options #11

Merged
merged 10 commits into from
Sep 10, 2020
17 changes: 16 additions & 1 deletion src/Adapter/DbSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Laminas\Db\Sql\Expression;
use Laminas\Db\Sql\Select;
use Laminas\Db\Sql\Sql;
use LogicException;

class DbSelect implements AdapterInterface
{
Expand Down Expand Up @@ -122,7 +123,7 @@ public function count()
$result = $statement->execute();
$row = $result->current();

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

return $this->rowCount;
}
Expand Down Expand Up @@ -165,4 +166,18 @@ public function getArrayCopy()
),
];
}

private function locateRowCount(array $row)
{
if (array_key_exists(self::ROW_COUNT_COLUMN_NAME, $row)) {
return (int) $row[self::ROW_COUNT_COLUMN_NAME];
}

$lowerCaseColumnName = strtolower(self::ROW_COUNT_COLUMN_NAME);
if (array_key_exists($lowerCaseColumnName, $row)) {
return (int) $row[$lowerCaseColumnName];
}

throw new LogicException('Unable to determine row count; missing row count column in result');
}
}
12 changes: 12 additions & 0 deletions test/Adapter/DbSelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,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([strtolower(DbSelect::ROW_COUNT_COLUMN_NAME) => 7]));

$count = $this->dbSelect->count();
$this->assertEquals(7, $count);
}

public function testCustomCount()
{
$this->dbSelect = new DbSelect($this->mockSelect, $this->mockSql, null, $this->mockSelectCount);
Expand Down