classSession
final class Session
Aggregates ZTD session state and core collaborators.
Type Aliases§
Row = import from StatementInterfaceMethods§
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
$rewriter | SqlRewriter | Rewrite pipeline for SQL. |
$shadowStore | ShadowStore | Target shadow store for mutation application. |
$resultSelectRunner | ResultSelectRunner | Executes result-select queries. |
$config | ZtdConfig | ZTD configuration for error handling. |
$connection | ConnectionInterface | Database connection for query execution. |
$transactions | ?ShadowTransactions | |
$registry | ?TableDefinitionRegistry | |
$copySupport | ?CopySupport | |
$parameterBindingCompiler | ?ParameterBindingCompiler | |
$resultColumnTypeResolver | ResultColumnTypeResolver |
public function applyShadow(
ShadowMutation $mutation,
ResultSet $resultSet,
string $sql,
): MutationImpactWrites a mutation into the shadow, and remembers the identity it produced.
Parameters
$mutation | ShadowMutation | Mutation to write |
$resultSet | ResultSet | What the rewritten statement read back |
$sql | string | Statement being simulated, for the refusal |
Returns
MutationImpact What the statement came toThrows
DatabaseException When the shadow refuses the statementCalled from 3
Calls 2
- method-call ShadowApplication::apply() line 126
- method-call ShadowApplication::lastInsertIdOf() line 127
public function shouldExecute(RewritePlan $plan): boolWhether the plan's SQL should be executed against the database.
Parameters
$plan | RewritePlan |
Returns
boolCalls 2
- method-call RewritePlan::kind() line 137
- class-const QueryKind::SKIPPED() line 137
public function needsPostProcessing(RewritePlan $plan): boolWhether the plan requires post-execution processing via processExecutedStatement().
Parameters
$plan | RewritePlan |
Returns
boolCalls 3
- method-call RewritePlan::kind() line 145
- class-const QueryKind::WRITE_SIMULATED() line 145
- class-const QueryKind::DDL_SIMULATED() line 146
public function createEmptyWriteResult(): ExecuteResultCreate an empty write-simulated result for skipped writes.
Returns
Calls 2
- static-call GenericExecuteResult::fromBufferedRows() line 154
- class-const QueryKind::WRITE_SIMULATED() line 154
public function enable(): voidEnable ZTD behavior for this session.
Test cases 1
public function disable(): voidDisable ZTD behavior for this session.
Test cases 1
public function isEnabled(): boolCheck whether ZTD mode is enabled.
Returns
boolTest cases 1
public function transactions(): TransactionTargetProvides the target used to apply transaction statements to shadow state.
Returns
public function beginTransaction(): voidStarts a transaction over the shadow state.
Calls 1
- method-call ShadowTransactions::begin() line 194
public function commitTransaction(): voidKeeps the current shadow state and discards transactional snapshots.
Calls 1
- method-call ShadowTransactions::commit() line 202
public function rollBackTransaction(): voidRestores the shadow state captured when the transaction began.
Calls 1
- method-call ShadowTransactions::rollBack() line 210
public function applyTransactionStatement(TransactionStatement $statement): voidApplies a parsed transaction statement to this session's shadow state.
Parameters
$statement | TransactionStatement |
Calls 1
- method-call TransactionStatement::apply() line 218
public function transactionStatement(string $sql): TransactionStatement|nullAnswers the transaction statement a statement is, if it is one.
Parameters
$sql | string | Statement as it was written |
Returns
TransactionStatement|null What it does to the transaction, or null when it is not oneCalls 1
- method-call SqlRewriter::transactionStatement() line 230
public function lastInsertId(): string|falseAnswers the identity the last simulated insert would have been given.
Returns
string|false The identity, or false when nothing has been insertedpublic function tableDefinition(string $tableName): TableDefinition|nullAnswers what a table was described as, where something described it.
Parameters
$tableName | string | Table to answer for |
Returns
TableDefinition|null Its description, or null when nothing has described itTest cases 2
Calls 1
- method-call TableDefinitionRegistry::get() line 252
public function copySupport(): CopySupport|nullAnswers how this dialect writes COPY, where it writes it at all.
Returns
CopySupport|null What the dialect supports, or null where it has no COPYpublic function copyTarget(string $relation, string|null $fields): CopyTarget|nullAnswers what a COPY statement is written against, where everything it needs is known.
Parameters
$relation | string | Relation as the statement named it |
$fields | string|null | Column 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 undescribedTest cases 2
Calls 1
- method-call TableDefinitionRegistry::get() line 278
public function parameterBindingCompiler(): ParameterBindingCompiler|nullAnswers how this dialect writes bound parameters, where it needs to be told.
Returns
ParameterBindingCompiler|null The compiler, or null where the driver binds them itselfpublic function resultColumnTypeResolver(): ResultColumnTypeResolverAnswers how this dialect reads the column types a driver reports.
Returns
ResultColumnTypeResolver The resolverpublic function rewrite(string $sql): RewritePlanRewrite 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
$sql | string |
Returns
Throws
DatabaseException When config is Exception mode and rewrite fails.Called from 2
Calls 4
- method-call SqlRewriter::rewrite() line 317
- method-call RewriteRefusal::forUnsupported() line 319
- method-call RewriteRefusal::forUnknownSchema() line 321
- method-call SqlRewriter::emptyResultSelect() line 321
public function splitStatements(string $sql): list<string>Parameters
$sql | string |
Returns
list<string>Test cases 1
Calls 1
- method-call SqlRewriter::splitStatements() line 330
public function processExecutedStatement(
RewritePlan $plan,
StatementInterface $statement,
): ExecuteResultProcess 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
$plan | RewritePlan | The rewrite plan from rewrite(). |
$statement | StatementInterface | The already-executed statement. |
Returns
ExecuteResult The execution result.Throws
DatabaseException When the shadow refuses the statementTest cases 2
Calls 11
- method-call RewritePlan::kind() line 347
- class-const QueryKind::READ() line 347
- static-call GenericExecuteResult::fromStatement() line 348
- method-call ResultSelectRunner::readResultSet() line 351
- method-call RewritePlan::requireMutation() line 354
- method-call Session::applyShadow() line 355
- method-call RewritePlan::sql() line 355
- method-call RewritePlan::returningProjection() line 356
- static-call GenericExecuteResult::fromBufferedRows() line 361
- class-const QueryKind::WRITE_SIMULATED() line 363
- method-call RewritePlan::affectedRowsMode() line 364
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
$plan | RewritePlan | The rewrite plan containing the SQL and mutation. |
$executor | callable(string): StatementInterface|false | Function 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 statementCalled from 1
Calls 4
- method-call RewritePlan::requireMutation() line 384
- method-call ResultSelectRunner::runResultSet() line 386
- method-call RewritePlan::sql() line 387
- method-call Session::applyShadow() line 391
public function execStatement(string $sql): int|falseExecute an exec-style statement with ZTD rewriting and shadow application.
Parameters
$sql | string | The 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
- method-call Session::rewrite() line 405
- class-const QueryKind::SKIPPED() line 407
- class-const QueryKind::READ() line 411
- method-call ConnectionInterface::query() line 412
- method-call ResultSelectRunner::runResultSet() line 421
- method-call Session::applyShadow() line 426
Private surface 12§
Implementation details, listed for orientation only.
private SqlRewriter $rewriterprivate ResultSelectRunner $resultSelectRunnerprivate ConnectionInterface $connectionprivate bool $enabled = trueprivate ShadowTransactions $transactionsprivate TableDefinitionRegistry $registryprivate ?CopySupport $copySupportprivate ?ParameterBindingCompiler $parameterBindingCompilerprivate ResultColumnTypeResolver $resultColumnTypeResolverprivate ShadowApplication $shadowApplicationprivate RewriteRefusal $refusalsprivate ?string $lastInsertId = nullTest cases 14§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 7
SessionTest::testBeginTransactionRollBackTransactionBeginTransactionUsesProvidedTransactionManagerForSchemaRollbackcallsSessionTest::testDisableEnableDisableEnableAndDisablecallsSessionTest::testForeignKeyFailureRestoresShadowStatecallsSessionTest::testMutationFailureIsConvertedToDatabaseExceptioncallsSessionTest::testParameterBindingCompilerResultColumnTypeResolverParameterBindingCompilerDelegatesCopyTargetsToTheConfiguredPlatformSupportcallsSessionTest::testSplitStatementsUsesPlatformRewritercallsSessionTest::testTableDefinitionReturnsRegisteredSchemaOrNullcalls
Other tests reaching this symbol 7
FakeSessionFactory::createcallsSessionUnderTest::overcallsStatementSimulatorTest::testReadStatementReturnsFalseWhenExecutorFailscallsStatementSimulatorTest::testReadStatementReturnsRowCountcallsStatementSimulatorTest::testSimulateRefusesAWriteThePlanCarriesNoMutationForcallsStatementSimulatorTest::testUnsupportedSqlThrowscallsStatementSimulatorTest::testWriteStatementUpdatesShadowStorecalls
Relations§
Method calls 2
Type declarations 3
- type ZtdQuery\Platform\SessionFactory packages/ztd-query-core/src/Platform/SessionFactory.php:35
- type ZtdQuery\Simulator\StatementSimulator packages/ztd-query-core/src/Simulator/StatementSimulator.php:24
- type ZtdQuery\Simulator\StatementSimulator packages/ztd-query-core/src/Simulator/StatementSimulator.php:29