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
Called from 3
Calls 2
public function shouldExecute(RewritePlan $plan): bool

Whether the plan's SQL should be executed against the database.

Parameters

Returns

bool
Calls 2
public function needsPostProcessing(RewritePlan $plan): bool

Whether the plan requires post-execution processing via processExecutedStatement().

Parameters

Returns

bool
Calls 3
public function createEmptyWriteResult(): ExecuteResult

Create an empty write-simulated result for skipped writes.

Calls 2
public function enable(): void

Enable ZTD behavior for this session.

Test cases 1
public function disable(): void

Disable ZTD behavior for this session.

Test cases 1
public function isEnabled(): bool

Check whether ZTD mode is enabled.

Returns

bool
Test cases 1
public function transactions(): TransactionTarget

Provides the target used to apply transaction statements to shadow state.

Test cases 1
public function beginTransaction(): void

Starts a transaction over the shadow state.

Calls 1
public function commitTransaction(): void

Keeps the current shadow state and discards transactional snapshots.

Calls 1
public function rollBackTransaction(): void

Restores the shadow state captured when the transaction began.

Calls 1
public function applyTransactionStatement(TransactionStatement $statement): void

Applies a parsed transaction statement to this session's shadow state.

Parameters

Calls 1
public function transactionStatement(string $sql): TransactionStatement|null

Answers the transaction statement a statement is, if it is one.

Parameters

$sqlstringStatement as it was written

Returns

TransactionStatement|null What it does to the transaction, or null when it is not one
Calls 1
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
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

Answers how this dialect writes COPY, where it writes it at all.

Returns

CopySupport|null What the dialect supports, or null where it has no COPY
Test cases 2
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 2
Calls 1
public function parameterBindingCompiler(): ParameterBindingCompiler|null

Answers how this dialect writes bound parameters, where it needs to be told.

Returns

ParameterBindingCompiler|null The compiler, or null where the driver binds them itself
Test cases 2
public function rewrite(string $sql): RewritePlan

Rewrite SQL using the configured rewriter.

Catches exceptions from the rewriter and handles them based on config. For ignore/notice modes, returns a passthrough plan (READ with original SQL).

Parameters

$sqlstring

Throws

DatabaseException When config is Exception mode and rewrite fails.
Called from 2
Calls 4
public function splitStatements(string $sql): list<string>

Parameters

$sqlstring

Returns

list<string>
Test cases 1
Calls 1
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 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
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.
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 14§

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

Dedicated tests 7
Other tests reaching this symbol 7

Relations§

Method calls 2
Type declarations 3