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

Conversation

samsonasik
Copy link
Member

Signed-off-by: Abdul Malik Ikhsan samsonasik@gmail.com

Q A
Bugfix yes

Description

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

…ATTR_CASE => \PDO::CASE_LOWER in driver options

Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
@samsonasik
Copy link
Member Author

need to update mock driver in test

…tance

Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
@samsonasik samsonasik changed the title Fixes #10 : Fix incorrect DbSelect::count() when setted \PDO::ATTR_CASE => \PDO::CASE_LOWER in driver options [wip] Fixes #10 : Fix incorrect DbSelect::count() when setted \PDO::ATTR_CASE => \PDO::CASE_LOWER in driver options Aug 5, 2020
@samsonasik
Copy link
Member Author

The easiest way probably create a separate class that uses small 'c' as column, or just change the ROW_COUNT_COLUMN_NAME constant value to small 'c'

Copy link
Member

@froschdesign froschdesign left a comment

Choose a reason for hiding this comment

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

I think the solution is too complicated because the select query for counting contains only one column.

Some problems:

  • PDO attribute is checked two times
  • a new class constant is added to the class
  • a concrete database adapter is added to the class

@froschdesign
Copy link
Member

A possible unit test:

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);
}

Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
@samsonasik
Copy link
Member Author

I simplified check with add "c" column in columns list, so if no isset "C" column, we can fallback to "c" column. @froschdesign I applied your test.

@samsonasik
Copy link
Member Author

travis build green 🎉

@samsonasik samsonasik changed the title [wip] Fixes #10 : Fix incorrect DbSelect::count() when setted \PDO::ATTR_CASE => \PDO::CASE_LOWER in driver options Fixes #10 : Fix incorrect DbSelect::count() when setted \PDO::ATTR_CASE => \PDO::CASE_LOWER in driver options Aug 6, 2020
Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
@samsonasik samsonasik added the Bug Something isn't working label Aug 7, 2020
@samsonasik samsonasik added this to the 2.8.4 milestone Aug 7, 2020
Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
@samsonasik
Copy link
Member Author

travis build green 🎉

Copy link
Member

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

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

I'm worried about how portable this is: is it going to work across all drivers PDO supports? what about non-PDO drivers?

I'm wondering if we either need (a) a flag to enable this behavior, with documentation about when you'd enable that flag, and/or (b) a separate adapter that does this. Right now, since we mock the connections, we may be getting a false sense of security that this approach works, when it may only work in select cases.

src/Adapter/DbSelect.php Outdated Show resolved Hide resolved
src/Adapter/DbSelect.php Outdated Show resolved Hide resolved
…ge count()

Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
@samsonasik
Copy link
Member Author

@froschdesign @weierophinney merge-able now ? Thank you.

$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)];
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

Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
@weierophinney weierophinney changed the base branch from master to 2.8.x September 10, 2020 14:19
weierophinney added a commit that referenced this pull request Sep 10, 2020
Signed-off-by: Matthew Weier O'Phinney <matthew@weierophinney.net>
@weierophinney weierophinney merged commit 56280e5 into laminas:2.8.x Sep 10, 2020
@samsonasik samsonasik deleted the fix-10 branch September 10, 2020 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

some DbAdapter driver_options can make DbSelect::count() incorrect
3 participants