Skip to content

Bug: Query Builder limit() problem with OCI8 #9469

@rutgers-master

Description

@rutgers-master

PHP Version

8.3

CodeIgniter4 Version

4.6.0

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

apache

Database

Oracle

What happened?

When using Query Builder (OCI8) on an Oracle database, it seems like the limit() function is not working properly when returning results for anything after the first page. In other words, if I do a limit(2,0), I'll get back the first 2 results correctly, however if I do a limit(2,2), instead of getting back the next page of 2 results, instead I get back 3 results and one of the results is a duplicate from the previous page of results.

Steps to Reproduce

Using a Model that is for an Oracle table, simply do:

$results = $builder->select("field")->limit(2,0)->get()->getResult();
// Print the results
print_r($results);
// Then do it again to get the next 2 results:
$results = $builder->select("field")->limit(2,2)->get()->getResult();
// Print the results
print_r($results);
// You'll get back 3 results instead of 2 results.

Expected Output

The expected output would be to get back just 2 results when doing limit(2,2), and this can easily be fixed by modifying the Codeigniter4/system/Database/OCI8/Builder.php file and changing this line:
$limitTemplateQuery = 'SELECT * FROM (SELECT INNER_QUERY.*, ROWNUM RNUM FROM (%s) INNER_QUERY WHERE ROWNUM < %d)' . ($offset !== 0 ? ' WHERE RNUM >= %d' : '');
To this line:
$limitTemplateQuery = 'SELECT * FROM (SELECT INNER_QUERY.*, ROWNUM RNUM FROM (%s) INNER_QUERY WHERE ROWNUM < %d)' . ($offset !== 0 ? ' WHERE RNUM > %d' : '');

Note: All I did was change "RNUM >= %d" to "RNUM > %d".

Anything else?

If you need more details, please let me know. Unfortunately I don't know the specific version of the Oracle server I am accessing, I don't manage the Oracle server itself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions