Skip to content

refactor: Query Builder WHERE/HAVING condition handling#10217

Open
memleakd wants to merge 3 commits into
codeigniter4:4.8from
memleakd:refactor/query-builder-condition-compiler
Open

refactor: Query Builder WHERE/HAVING condition handling#10217
memleakd wants to merge 3 commits into
codeigniter4:4.8from
memleakd:refactor/query-builder-condition-compiler

Conversation

@memleakd
Copy link
Copy Markdown
Contributor

Description

This PR proposes refactoring the internal Query Builder handling for WHERE and HAVING conditions without changing public APIs or expected SQL output.

The existing condition-building paths each handled parts of prefixing, grouping, and compilation themselves. This centralizes those pieces into small private helpers so the behavior is easier to follow and safer to maintain.

It also adds regression coverage for a few important condition cases:

  • LIKE with a bound value
  • = null compiling to IS NULL
  • != null compiling to IS NOT NULL

This is intentionally a behavior-preserving cleanup. No new user-facing API is added, so there is no user guide or changelog entry.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

- Route WHERE/HAVING condition insertion through shared prefix handling
- Extract WHERE/HAVING condition compilation into focused private helpers
- Add regression coverage for operator and null condition handling
- Reduce the BaseBuilder empty() PHPStan baseline count

Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label May 18, 2026
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Comment thread system/Database/BaseBuilder.php Outdated
Comment thread system/Database/BaseBuilder.php Outdated
Comment thread system/Database/BaseBuilder.php
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
@memleakd
Copy link
Copy Markdown
Contributor Author

Thank you @michalsn. Pushed a small update to keep the shared helper focused on the common path.

Copy link
Copy Markdown
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

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

Thanks!

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors CodeIgniter4’s Query Builder internals for building/compiling WHERE and HAVING conditions by centralizing prefix handling and condition compilation into private helpers, aiming to preserve existing SQL output while improving maintainability. It also updates static-analysis baselines and adds regression tests for a few key operator cases.

Changes:

  • Centralized WHERE/HAVING prefixing into new private helpers and streamlined condition compilation in BaseBuilder.
  • Added regression tests covering LIKE with value, and = null/!= null compiling to IS (NOT) NULL.
  • Updated PHPStan baseline counts related to empty() usage.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
utils/phpstan-baseline/empty.notAllowed.neon Updates baseline counts after reducing empty() usage in the builder.
tests/system/Database/Builder/WhereTest.php Adds regression coverage for specific operator/null-handling cases.
system/Database/BaseBuilder.php Refactors internal WHERE/HAVING condition prefixing and compilation via new helpers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +154 to +165
/**
* @param mixed $value
*/
#[DataProvider('provideWhereOperatorRegressionCases')]
public function testWhereOperatorRegressionCases(string $key, $value, string $expectedSQL): void
{
$builder = $this->db->table('jobs job');

$builder->where($key, $value);

$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
}
Comment on lines +1120 to +1129
/**
* @param 'QBHaving'|'QBWhere' $clause
* @param array<string, mixed> $condition
*/
private function addWhereHavingCondition(string $clause, array $condition, string $type): void
{
$condition['condition'] = $this->getWhereHavingPrefix($clause, $type) . $condition['condition'];

$this->{$clause}[] = $condition;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants