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

Improving limit method using OVER() #392

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions library/Zend/Db/Adapter/Db2.php
Original file line number Diff line number Diff line change
Expand Up @@ -691,14 +691,23 @@ public function limit($sql, $count, $offset = 0)
* Unfortunately because we use the column wildcard "*",
* this puts an extra column into the query result set.
*/
return "SELECT z2.*
FROM (
SELECT ROW_NUMBER() OVER() AS \"ZEND_DB_ROWNUM\", z1.*
FROM (
" . $sql . "
) z1
) z2
WHERE z2.zend_db_rownum BETWEEN " . ($offset+1) . " AND " . ($offset+$count);
// Add this piece and place $order in OVER() clause
$pieces = preg_split("/order by/i", $sql);
$order = "";
if(array_key_exists(1, $pieces)) {
$order = "ORDER BY " . $pieces[1];
}

$limit_sql = "SELECT z2.*
FROM (
SELECT ROW_NUMBER() OVER($order) AS \"ZEND_DB_ROWNUM\", z1.*
FROM (
" . $sql . "
) z1
) z2
WHERE z2.zend_db_rownum BETWEEN " . ($offset+1) . " AND " . ($offset+$count);

return $limit_sql;
}

/**
Expand Down
Loading