ZF-12459: Sqlsrv driver limit/offset handling assumes the machine can add numbers above int_max

Description

For some reason Zend_Db_Select tries to be clever and set the limit to PHP_INT_MAX if only offset is supplied. This, for one, means you can't offset into a table with more than 2^31 rows, but also causes strange differences compared to using the freetds driver on linux. This is because it actually tries to add the $offset onto the $count, even though $count is already at int max!

Basically, in the SQLsrv adapter, line 633 or so, this:

            $end = $offset + $count;

            $sql = "WITH outer_tbl AS ($sql) SELECT * FROM outer_tbl WHERE \"ZEND_DB_ROWNUM\" BETWEEN $start AND $end";

Needs to be changed to:

            if ($count == PHP_INT_MAX) {
                $sql = "WITH outer_tbl AS ($sql) SELECT * FROM outer_tbl WHERE \"ZEND_DB_ROWNUM\" >= $start";
            }
            else {
                $end = $offset + $count;

                $sql = "WITH outer_tbl AS ($sql) SELECT * FROM outer_tbl WHERE \"ZEND_DB_ROWNUM\" BETWEEN $start AND $end";
            }

Comments

Seems similar

Does this solve itself with a 64-bit version of PHP?

Resolved on trunk (25076) and release-1.12 (25077)