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
9 changes: 7 additions & 2 deletions src/Adapter/DbSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ public function count()
$result = $statement->execute();
$row = $result->current();

$this->rowCount = (int) $row[self::ROW_COUNT_COLUMN_NAME];
$this->rowCount = isset($row[self::ROW_COUNT_COLUMN_NAME])
? (int) $row[self::ROW_COUNT_COLUMN_NAME]
: (int) $row[strtolower(self::ROW_COUNT_COLUMN_NAME)];
samsonasik marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when the lowercase version is not present? This is likely to raise a warning.

I'd argue this should be done as follows:

$this->rowCount = $this->locateRowCount($row);

with the following helper method:

/* Not sure if `array` is correct here, or if this could be an `ArrayAccess` instance */
private function locateRowCount(array $row): int
{
    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');
}

This will satisfy #10, while also making the code less brittle.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weierophinney implemented


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

$countSelect = new Select;

$countSelect->columns([self::ROW_COUNT_COLUMN_NAME => new Expression('COUNT(1)')]);
$countSelect->columns([
self::ROW_COUNT_COLUMN_NAME => new Expression('COUNT(1)'),
strtolower(self::ROW_COUNT_COLUMN_NAME) => new Expression('COUNT(1)')
samsonasik marked this conversation as resolved.
Show resolved Hide resolved
]);
$countSelect->from(['original_select' => $select]);

return $countSelect;
Expand Down
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