Skip to content

🎉 Added chainable fluent DB helper and unit test - #3052

Open
harunollyo wants to merge 3 commits into
devfrom
query-builder
Open

harunollyo wants to merge 3 commits into
devfrom
query-builder

Conversation

@harunollyo

@harunollyo harunollyo commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Added a chainable, fluent DB for building and executing database queries with improved security and SQL injection prevention. The builder also provides better extensibility, maintainability, and debugging support through the to_sql() method

Key Benefits

  • Improved security — centralizes query construction and parameter binding to help prevent SQL injection.
  • Extensibility — provides a structured foundation for adding new query features without expanding a collection of independent helper methods.
  • Better readability — uses a fluent, chainable API for clearer and more maintainable queries.
  • Debugging support — provides a to_sql() method to inspect the generated SQL query.
  • Consistent database handling — keeps query preparation and execution aligned with WordPress $wpdb.
  • Easier maintenance — centralizes query-building logic in QueryBuilder.php.

Example Usage

$users = DB::table( 'users' )
    ->select( [ 'ID', 'user_login', 'user_email' ] )
    ->where( 'user_status', 0 )
    ->order_by( 'ID', 'DESC' )
    ->limit( 10 )
    ->get();
$course = DB::table( 'posts' )
    ->where( 'post_id', $course_id )
    ->first();
$count = DB::table( 'options' )
    ->where( 'option_id', $id )
    ->count();

For debugging, the generated SQL can be inspected without executing the query:

$sql = DB::table( 'comments' )
    ->where( 'comment_id', $id )
    ->to_sql();

The new builder provides a safer, more maintainable, extensible, and debuggable foundation for database queries while allowing existing query logic to be migrated incrementally.

Testing

image

@harunollyo harunollyo self-assigned this Oct 1, 2026
@harunollyo harunollyo changed the title Feat: added chainable fluent QueryBuilder helper and unit test Feat: added chainable fluent DB helper and unit test Oct 1, 2026
@harunollyo
harunollyo marked this pull request as ready for review October 1, 2026 10:12
@harunollyo
harunollyo requested a review from shewa12 October 1, 2026 10:12
@harunollyo harunollyo changed the title Feat: added chainable fluent DB helper and unit test 🎉 Added chainable fluent DB helper and unit test Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant