Skip to content

🎉 feat: add subquery support, aggregate methods and array conditions to query builder - #3252

Open
harunollyo wants to merge 14 commits into
4.3.0from
query-builder-feature
Open

harunollyo wants to merge 14 commits into
4.3.0from
query-builder-feature

Conversation

@harunollyo

@harunollyo harunollyo commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Enhance the query builder with subquery support and array-based WHERE conditions.

Changes

  • Add subquery support to query builder methods.
  • Add associative array support to where() for multiple conditions.
  • Add comprehensive unit tests.
  • Update DB query builder documentation.
  • Aggregate Methods sum, min, max, and avg

Test Result

composer test:unit:filter DBTest
image

@harunollyo
harunollyo requested a review from shewa12 November 25, 2026 07:12
@harunollyo harunollyo self-assigned this Nov 25, 2026
@harunollyo harunollyo added the 4.3.0 Tutor v4.3.0 label Nov 25, 2026

@shewa12 shewa12 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review: request changes

Thanks for this — subqueries and array conditions are a useful addition. I tested core/DB.php from this branch against a stub wpdb and verified each issue below by inspecting the generated SQL. Details are in the inline comments.

🔴 Must fix before merge

  1. SQL injection via public resolve_subquery(). It uses to_sql(), which strips wpdb placeholder escapes, so user % values become live placeholders when the output is used in *_raw() with bindings. Make it private and use build_select_query().
  2. Fail-open on invalid input. Empty or invalid subqueries, malformed array tuples, bad operators and bad join types are silently dropped. That broadens queries, and with delete()/update() it affects every row. Throw instead.
  3. where( Closure ) grouping is silently dropped, e.g. ->where( fn… )->delete() produces DELETE FROM table.
  4. or_where( array ) isn't grouped. It produces a OR b OR c instead of a OR (b AND c), which can leak rows outside the owner scope.
  5. Table names are never validated in table()/from()/join() (pre-existing, but the new public from() widens it).

🟠 Bugs

A derived table without an alias produces invalid SQL. Writes are allowed on derived tables. where( 'col' ) now means IS NULL. Subqueries are snapshotted when added. FOR UPDATE/LIMIT inside subqueries pass through. select( array( 'alias' => 'col' ) ) drops the alias.

🟡 Refactor / standards

Inconsistent operator whitelists, wrong @since tags, docs missing the subquery API, and tests that assert the fail-open behaviour.

Comment thread core/DB.php Resolved
Comment thread core/DB.php Resolved
Comment thread core/DB.php Resolved
Comment thread core/DB.php Resolved
Comment thread core/DB.php Resolved
Comment thread core/DB.php Resolved
Comment thread core/DB.php Resolved
Comment thread docs/developer/core/DB.php.md Resolved
Comment thread core/DB.php Resolved
Comment thread tests/phpunit/DBTest.php Resolved
@harunollyo
harunollyo requested a review from shewa12 November 26, 2026 11:44

@shewa12 shewa12 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the quick turnaround. Most of the previous findings are fixed, and I re-verified them against the generated SQL:

  • ✅ resolve_subquery() is now private and uses build_select_query(). A %s inside a subquery value no longer turns into a live placeholder.
  • ✅ Invalid or empty subqueries, bad aliases and operators, bad join types and malformed array tuples now throw instead of silently broadening the query.
  • ✅ where( Closure ) produces a grouped ( … OR … ), and or_where( array ) produces OR ( a AND b ).
  • ✅ Table and join names are validated; derived tables require an alias and block insert/update/delete; FOR UPDATE is stripped and LIMIT rejected for IN subqueries; select( [ 'alias' => 'col' ] ) keeps the alias; where( 'col' ) with one argument throws.

Still requesting changes for:

  1. 🔴 WPCS is failing on lines added in this push (exception messages with interpolated values; missing @throws tags).
  2. 🔴 Sites with a table prefix that starts with a digit break completely: every DB::table() call throws.
  3. 🟠 JOINs with invalid columns are still silently dropped, which removes the INNER JOIN filter.
  4. 🟠 add_array_where() corrupts the builder's existing WHERE conditions when it throws.

Details are inline.

Comment thread core/DB.php Resolved
Comment thread core/DB.php Resolved
Comment thread core/DB.php Resolved
Comment thread core/DB.php Resolved
Comment thread core/DB.php Resolved
Comment thread core/DB.php Resolved
Comment thread core/DB.php Resolved
@harunollyo harunollyo changed the title 🎉 feat: add subquery support and array conditions to query builder 🎉 feat: add subquery support, aggregate methods and array conditions to query builder Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.3.0 Tutor v4.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants