Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Add support for LIMIT OFFSET for db2 #275

Merged
merged 5 commits into from
Nov 23, 2017

Conversation

akrabat
Copy link
Contributor

@akrabat akrabat commented Oct 13, 2017

Since 2016, DB2 has supported the LIMIT x OFFSET y construct. This PR supports the new construct in a BC manner via a feature flag. To use with TableGateway:

$tableGateway = new TableGateway($table, $adapter);
$decorator = $tableGateway->getSql()->getSqlPlatform()->getDecorators()['Zend\Db\Sql\Select'];
$decorator->setSupportsLimitOffset(true);

array_push($sqls, sprintf("LIMIT %s", $limit));
}
}
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think deep if can be simplified with return early:

if (! $this->limit) {
    return;
}

if ($this->offset) {
    array_push($sqls, sprintf("LIMIT %s OFFSET %s", $limit, $offset));
    return; 
}

array_push($sqls, sprintf("LIMIT %s", $limit));
return;

// Note: db2_prepare/db2_execute fails with positional parameters, for LIMIT & OFFSET
$limit = (int)$this->limit;
$offset = (int)$this->offset;
if ($this->limit) {
Copy link
Contributor

Choose a reason for hiding this comment

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

$this->limit is not casted to (int) one, I think should use $limit instead

$limit = (int)$this->limit;
$offset = (int)$this->offset;
if ($this->limit) {
if ($this->offset) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think should use if ($offset) instead as it is the casted with (int) one

@akrabat
Copy link
Contributor Author

akrabat commented Oct 16, 2017

@samsonasik Addressed.

if ($this->supportsLimitOffset) {
// Note: db2_prepare/db2_execute fails with positional parameters, for LIMIT & OFFSET
$limit = (int)$this->limit;
$offset = (int)$this->offset;
Copy link
Contributor

Choose a reason for hiding this comment

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

$offset never used if ! $limit, can be define later (after line 114)

Copy link
Contributor

@mwillbanks mwillbanks left a comment

Choose a reason for hiding this comment

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

Just formatting, other than the formatting, everything here looks great.

@@ -82,6 +104,23 @@ protected function processLimitOffset(PlatformInterface $platform, DriverInterfa
return;
}

if ($this->supportsLimitOffset) {
// Note: db2_prepare/db2_execute fails with positional parameters, for LIMIT & OFFSET
$limit = (int)$this->limit;
Copy link
Contributor

Choose a reason for hiding this comment

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

space after the cast.

if ($this->supportsLimitOffset) {
// Note: db2_prepare/db2_execute fails with positional parameters, for LIMIT & OFFSET
$limit = (int)$this->limit;
if (! $limit) {
Copy link
Contributor

Choose a reason for hiding this comment

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

no space after the !

Copy link
Contributor

Choose a reason for hiding this comment

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

most of zf code has space after !, I think already part of zf cs /cc @webimpress

Copy link
Member

Choose a reason for hiding this comment

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

@mwillbanks
@samsonasik is right. See zendframework/zend-coding-standard#1 - Generic.Formatting.SpaceAfterNot

Copy link
Member

Choose a reason for hiding this comment

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

@mwillbanks
Yes, and this is even part of current version of zend-coding-standard (1.0.0): https://github.com/zendframework/zend-coding-standard/blob/master/ruleset.xml#L12

return;
}

$offset = (int)$this->offset;
Copy link
Contributor

Choose a reason for hiding this comment

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

space after the cast.

@guidofaecke
Copy link

Just took a quick look at the code.
I assume "ORDER BY" is still functional, since "ROW_NUMBER() OVER()" is now "missing"?

@akrabat
Copy link
Contributor Author

akrabat commented Nov 2, 2017

Just took a quick look at the code.
I assume "ORDER BY" is still functional, since "ROW_NUMBER() OVER()" is now "missing"?

Correct - ORDER BY is supported as you'd expect if you've used Zend\Db with MySQL for instance.

@akrabat
Copy link
Contributor Author

akrabat commented Nov 2, 2017

@mwillbanks Requested changes are done.

@ezimuel ezimuel merged commit 547215b into zendframework:develop Nov 23, 2017
@ezimuel
Copy link
Contributor

ezimuel commented Nov 23, 2017

@akrabat thanks for your contribution!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants