Query Builder
Foundation Database wraps common wpdb operations with prepared bindings, quoted identifiers, consistent exceptions, and a small fluent query builder. It remains intentionally close to SQL so developers can inspect exactly what WordPress will execute.
Read rows
Section titled “Read rows”Inject the Database contract and the table object into the class that owns the query. For example, create src/Report/Report_Repository.php:
first() returns one row or null. get() returns a list of associative rows. Qualified identifiers such as r.created_at are quoted as `r`.`created_at` rather than as one identifier.
Use null with equality operators for SQL null checks:
These comparisons compile to IS NULL and IS NOT NULL. Other operators with null are rejected because they do not have useful SQL semantics.
Write rows
Section titled “Write rows”Use the table object for inserts, updates, and deletes so physical table naming stays in one place:
insert() returns the affected row count, while insertGetId() returns the generated integer ID. update(), delete(), and execute() return affected row counts.
Inspect and execute SQL
Section titled “Inspect and execute SQL”Build a query before executing it when logging or diagnostics need the SQL shape and separate bindings:
Prefer toSql() plus bindings() for structured diagnostics. A fully prepared SQL string may contain customer or application data and should not be logged without considering its sensitivity.
The Database contract also exposes prepared low-level operations for queries that do not fit the builder:
Use prepare() when another WordPress API requires the prepared SQL string. Keep values in placeholders instead of concatenating untrusted input.
Handle query failures
Section titled “Handle query failures”Database operations throw QueryException when wpdb reports an error. The exception retains the SQL template, bindings, and database error separately:
Avoid exposing database errors or bindings to end users. They may contain schema details or sensitive values.
Testing
Section titled “Testing”Use wpunit tests for repositories and query behavior. Create the real table, exercise the real WordPress database, and remove the table during cleanup. This catches placeholder, collation, identifier, and MariaDB behavior that a mocked wpdb cannot reproduce.