Repository navigation
🎉 feat: add subquery support, aggregate methods and array conditions to query builder - #3252
harunollyo wants to merge 14 commits into
Conversation
shewa12
left a comment
There was a problem hiding this comment.
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
- SQL injection via public
resolve_subquery(). It usesto_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 usebuild_select_query(). - 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. where( Closure )grouping is silently dropped, e.g.->where( fn… )->delete()producesDELETE FROM table.or_where( array )isn't grouped. It producesa OR b OR cinstead ofa OR (b AND c), which can leak rows outside the owner scope.- Table names are never validated in
table()/from()/join()(pre-existing, but the new publicfrom()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.
shewa12
left a comment
There was a problem hiding this comment.
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 usesbuild_select_query(). A%sinside 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 … ), andor_where( array )producesOR ( a AND b ). - ✅ Table and join names are validated; derived tables require an alias and block insert/update/delete;
FOR UPDATEis stripped andLIMITrejected for IN subqueries;select( [ 'alias' => 'col' ] )keeps the alias;where( 'col' )with one argument throws.
Still requesting changes for:
- 🔴 WPCS is failing on lines added in this push (exception messages with interpolated values; missing
@throwstags). - 🔴 Sites with a table prefix that starts with a digit break completely: every
DB::table()call throws. - 🟠 JOINs with invalid columns are still silently dropped, which removes the INNER JOIN filter.
- 🟠
add_array_where()corrupts the builder's existing WHERE conditions when it throws.
Details are inline.
…unsafe updates or deletes in DB class
…d improve aggregate methods
Summary
Enhance the query builder with subquery support and array-based
WHEREconditions.Changes
where()for multiple conditions.sum,min,max, andavgTest Result