final class QueryExecutor
Public API: explicitly declared with @visibility public.

Rewrites and executes SQL against one session using an injected database platform.

The executor owns execution policy. Session owns mutable virtual state, and Platform supplies database semantics without creating or owning sessions.

Type Aliases§

Row = import from StatementInterface
Accept database semantics through dependency injectiondoctest
$connect = static fn (\ZtdQuery\Connection\ConnectionInterface $connection, \ZtdQuery\Platform $platform): \ZtdQuery\QueryExecutor => new \ZtdQuery\QueryExecutor($connection, $platform);
    $connect instanceof \Closure // => true

Methods§

public function __construct(
    private ConnectionInterface $connection,
    private Platform $platform,
    ZtdConfig $config = new \ZtdQuery\Config\ZtdConfig(),
    ?Session $session = null,
    private ResultSelectRunner $resultSelectRunner = new \ZtdQuery\ResultSelectRunner(),
)

Reflect a fresh catalog unless the caller supplies virtual session state. Reflection failures propagate before the executor can be used.

Parameters

$connectionConnectionInterface
$platformPlatform
$configZtdConfig
$session?Session
$resultSelectRunnerResultSelectRunner
Calls 12
public function session(): Session

Return the virtual state shared by statements executed through this executor.

Returns

Test cases 19
Called from 22
public function applyShadow(
    ShadowMutation $mutation,
    ResultSet $resultSet,
    string $sql,
): MutationImpact

Writes a mutation into the shadow, and remembers the identity it produced.

Parameters

$mutationShadowMutationMutation to write
$resultSetResultSetWhat the rewritten statement read back
$sqlstringStatement being simulated, for the refusal

Returns

MutationImpact What the statement came to

Throws

DatabaseException When the shadow refuses the statement
Test cases 1
Called from 3
Calls 3
public function copyTarget(string $relation, string|null $fields): CopyTarget|null

Answers what a COPY statement is written against, where everything it needs is known.

Parameters

$relationstringRelation as the statement named it
$fieldsstring|nullColumn list as the statement wrote it, or null for every column

Returns

CopyTarget|null The target, or null when the dialect has no COPY or the table is undescribed
Calls 2
public function rewrite(string $sql): RewritePlan

Rewrite SQL using the configured rewriter.

Applies the configured refusal policy to unsupported SQL and unknown schemas.

Parameters

$sqlstring

Throws

DatabaseException When config is Exception mode and rewrite fails.
Test cases 11
Called from 4
Calls 4
public function splitStatements(string $sql): list<string>
public function processExecutedStatement(
    RewritePlan $plan,
    StatementInterface $statement,
): ExecuteResult

Process an already-executed statement based on the rewrite plan.

This method handles post-execution logic like shadow application for write queries. Use this when you need to control statement preparation and execution externally.

Parameters

$planRewritePlanThe rewrite plan from rewrite().
$statementStatementInterfaceThe already-executed statement.

Returns

ExecuteResult The execution result.

Throws

DatabaseException When the shadow refuses the statement
Test cases 1
Called from 2
Calls 12
public function runResultSelectAndApplyShadow(
    RewritePlan $plan,
    callable(string): StatementInterface|false $executor,
): array<int, Row>

Run result-select query and apply shadow mutation.

This method executes a result-select query using the provided executor, then applies the mutation from the rewrite plan to the shadow store.

Parameters

$planRewritePlanThe rewrite plan containing the SQL and mutation.
$executorcallable(string): StatementInterface|falseFunction to execute SQL.

Returns

array<int, Row> The affected rows.

Throws

UnsupportedSqlException When the plan carries no mutation to write.
DatabaseException When the shadow refuses the statement
Called from 1
Calls 5
public function execStatement(string $sql): int|false

Execute an exec-style statement with ZTD rewriting and shadow application.

Parameters

$sqlstringThe original SQL statement.

Returns

int|false The number of affected rows, or false on failure.

Throws

DatabaseException When config is Exception mode and rewrite fails.
Test cases 3
Called from 1
Calls 7

Private surface 7§

Implementation details, listed for orientation only.

private Session $session
private SqlRewriter $rewriter
private ShadowApplication $shadowApplication
private RewriteRefusal $refusals
private ConnectionInterface $connection
private Platform $platform
private ResultSelectRunner $resultSelectRunner = new \ZtdQuery\ResultSelectRunner()

Test cases 55§

Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.

Dedicated tests 3
Other tests reaching this symbol 52

Relations§

Instantiated in 2
Method calls 41
Type declarations 14