Skip to content

feat: add Query Builder explain()#10238

Open
memleakd wants to merge 3 commits into
codeigniter4:4.8from
memleakd:feat/query-builder-explain
Open

feat: add Query Builder explain()#10238
memleakd wants to merge 3 commits into
codeigniter4:4.8from
memleakd:feat/query-builder-explain

Conversation

@memleakd
Copy link
Copy Markdown
Contributor

Description

This PR adds explain() to Query Builder so applications can inspect the execution plan for the current SELECT query without manually compiling and prefixing SQL.

$query = $builder->where('status', 'pending')->explain();

The helper intentionally keeps the first slice small:

  • MySQLi and Postgre use EXPLAIN.
  • SQLite3 uses EXPLAIN QUERY PLAN.
  • SQLSRV and OCI8 throw a clear DatabaseException because their explain flows need different mechanics.
  • Test mode returns the compiled execution-plan SQL string.
  • DBDebug=false query failures return false, matching normal builder execution behavior.
  • The builder state resets by default, with explain(false) preserving it.

This also adds Model::explain() so model queries keep the expected soft-delete behavior, similar to other terminal builder operations.

Tests cover compiled SQL, SQLite syntax, reset behavior, failure behavior, unsupported drivers, live execution, and Model soft deletes.

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

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 25, 2026
Comment thread user_guide_src/source/database/query_builder.rst Outdated
memleakd added 2 commits May 25, 2026 14:19
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
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.

2 participants