final class Session

Aggregates ZTD session state and core collaborators.

Type Aliases§

Row = import from StatementInterface

Methods§

public function __construct(
    SqlRewriter $rewriter,
    ShadowStore $shadowStore,
    ResultSelectRunner $resultSelectRunner,
    ZtdConfig $config,
    ConnectionInterface $connection,
    ?ShadowTransactions $transactions = null,
    ?TableDefinitionRegistry $registry = null,
    ?CopySupport $copySupport = null,
    ?ParameterBindingCompiler $parameterBindingCompiler = null,
    ResultColumnTypeResolver $resultColumnTypeResolver = new \ZtdQuery\Platform\MissingResultColumnTypeResolver(),
)

Parameters

$rewriterSqlRewriterRewrite pipeline for SQL.
$shadowStoreShadowStoreTarget shadow store for mutation application.
$resultSelectRunnerResultSelectRunnerExecutes result-select queries.
$configZtdConfigZTD configuration for error handling.
$connectionConnectionInterfaceDatabase connection for query execution.
$transactions?ShadowTransactions
$registry?TableDefinitionRegistry
$copySupport?CopySupport
$parameterBindingCompiler?ParameterBindingCompiler
$resultColumnTypeResolverResultColumnTypeResolver
Calls 6
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 2
public function enable(): void

Enable ZTD behavior for this session.

Test cases 2
public function lastInsertId(): string|false

Answers the identity the last simulated insert would have been given.

Returns

string|false The identity, or false when nothing has been inserted
Test cases 1
Called from 1
public function tableDefinition(string $tableName): TableDefinition|null

Answers what a table was described as, where something described it.

Parameters

$tableNamestringTable to answer for

Returns

TableDefinition|null Its description, or null when nothing has described it
Test cases 2
Calls 1
public function copySupport(): CopySupport|null
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
Test cases 3
Calls 1
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 3
Called from 2
Calls 11
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
Test cases 1
Called from 1
Calls 4
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 1
Called from 1
Calls 6

Private surface 12§

Implementation details, listed for orientation only.

private SqlRewriter $rewriter
private ResultSelectRunner $resultSelectRunner
private ConnectionInterface $connection
private bool $enabled = true
private ShadowTransactions $transactions
private TableDefinitionRegistry $registry
private ?CopySupport $copySupport
private ?ParameterBindingCompiler $parameterBindingCompiler
private ResultColumnTypeResolver $resultColumnTypeResolver
private ShadowApplication $shadowApplication
private RewriteRefusal $refusals
private ?string $lastInsertId = null

Test cases 82§

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

Dedicated tests 28
Other tests reaching this symbol 54

Relations§

Instantiated in 3
Method calls 41
Type declarations 18