Skip to content

Commit

Permalink
Improving limit method using LIMIT,OFFSET
Browse files Browse the repository at this point in the history
  • Loading branch information
ushiday committed Nov 24, 2023
1 parent f8018da commit 87c1ae2
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions library/Zend/Db/Adapter/Db2.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,25 +680,16 @@ public function limit($sql, $count, $offset = 0)
require_once 'Zend/Db/Adapter/Db2/Exception.php';
throw new Zend_Db_Adapter_Db2_Exception("LIMIT argument offset=$offset is not valid");
}

if ($offset === 0) {
return $sql . " FETCH FIRST $count ROWS ONLY";
}


/**
* DB2 does not implement the LIMIT clause as some RDBMS do.
* We have to simulate it with subqueries and ROWNUM.
* Unfortunately because we use the column wildcard "*",
* this puts an extra column into the query result set.
* DB2Modern versions already have `LIMIT,OFFSET` available.
*/
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);
$sql .= " LIMIT $count";
if ($offset > 0) {
$sql .= " OFFSET $offset";
}

return $sql;
}

/**
Expand Down

0 comments on commit 87c1ae2

Please sign in to comment.