|
88 | 88 | class Database |
89 | 89 | { |
90 | 90 | use ApiHelperTrait; |
91 | | - use TransactionConfigurationTrait; |
92 | 91 | use RequestTrait; |
93 | 92 |
|
94 | 93 | public const CONTEXT_READ = 'r'; |
@@ -128,6 +127,15 @@ class Database |
128 | 127 | private CacheItemPoolInterface $cacheItemPool; |
129 | 128 | private array $info; |
130 | 129 | private int $isolationLevel; |
| 130 | + private TransactionOptionsBuilder $transactionOptionsBuilder; |
| 131 | + |
| 132 | + private const MUTATION_SETTERS = [ |
| 133 | + 'insert' => 'setInsert', |
| 134 | + 'update' => 'setUpdate', |
| 135 | + 'insertOrUpdate' => 'setInsertOrUpdate', |
| 136 | + 'replace' => 'setReplace', |
| 137 | + 'delete' => 'setDelete' |
| 138 | + ]; |
131 | 139 |
|
132 | 140 | /** |
133 | 141 | * Create an object representing a Database. |
@@ -185,6 +193,7 @@ public function __construct( |
185 | 193 | ); |
186 | 194 |
|
187 | 195 | $this->optionsValidator = new OptionsValidator($serializer); |
| 196 | + $this->transactionOptionsBuilder = new TransactionOptionsBuilder(); |
188 | 197 | $this->directedReadOptions = $instance->directedReadOptions(); |
189 | 198 | } |
190 | 199 |
|
@@ -747,25 +756,14 @@ public function snapshot(array $options = []): TransactionalReadInterface |
747 | 756 | throw new BadMethodCallException('Nested transactions are not supported by this client.'); |
748 | 757 | } |
749 | 758 |
|
750 | | - $options += [ |
751 | | - 'singleUse' => false |
| 759 | + $snapshotOptions = [ |
| 760 | + 'singleUse' => $options['singleUse'] ?? false |
752 | 761 | ]; |
753 | 762 |
|
754 | | - $options['transactionOptions'] = $this->configureReadOnlyTransactionOptions($options); |
755 | | - |
756 | | - // For backwards compatibility - remove all PBReadOnly fields |
757 | | - // This was previously being done in configureReadOnlyTransactionOptions |
758 | | - // @TODO: clean this up |
759 | | - unset( |
760 | | - $options['returnReadTimestamp'], |
761 | | - $options['strong'], |
762 | | - $options['readTimestamp'], |
763 | | - $options['exactStaleness'], |
764 | | - $options['minReadTimestamp'], |
765 | | - $options['maxStaleness'], |
766 | | - ); |
| 763 | + $snapshotOptions['transactionOptions'] = $this->transactionOptionsBuilder |
| 764 | + ->configureReadOnlyTransactionOptions($options); |
767 | 765 |
|
768 | | - return $this->operation->snapshot($this->session, $options); |
| 766 | + return $this->operation->snapshot($this->session, $snapshotOptions); |
769 | 767 | } |
770 | 768 |
|
771 | 769 | /** |
@@ -814,7 +812,7 @@ public function transaction(array $options = []): Transaction |
814 | 812 | } |
815 | 813 |
|
816 | 814 | // Configure readWrite options here. Any nested options for readWrite should be added to this call |
817 | | - $options['transactionOptions'] = $this->configureReadWriteTransactionOptions( |
| 815 | + $options['transactionOptions'] = $this->transactionOptionsBuilder->configureReadWriteTransactionOptions( |
818 | 816 | ($options['transactionOptions'] ?? []) + ['isolationLevel' => $this->isolationLevel] |
819 | 817 | ); |
820 | 818 |
|
@@ -921,7 +919,7 @@ public function runTransaction(callable $operation, array $options = []): mixed |
921 | 919 |
|
922 | 920 | // Configure necessary readWrite nested and base options |
923 | 921 | $transactionOptions = $options['transactionOptions'] ?? []; |
924 | | - $options['transactionOptions'] = $this->configureReadWriteTransactionOptions( |
| 922 | + $options['transactionOptions'] = $this->transactionOptionsBuilder->configureReadWriteTransactionOptions( |
925 | 923 | $transactionOptions + ['isolationLevel' => $this->isolationLevel] |
926 | 924 | ); |
927 | 925 |
|
@@ -1666,28 +1664,27 @@ public function delete(string $table, KeySet $keySet, array $options = []): Time |
1666 | 1664 | */ |
1667 | 1665 | public function execute($sql, array $options = []): Result |
1668 | 1666 | { |
1669 | | - unset($options['requestOptions']['transactionTag']); |
1670 | | - $session = $this->pluck('session', $options, false) |
1671 | | - ?: $this->session; |
1672 | | - |
1673 | | - list( |
1674 | | - $options['transaction'], |
1675 | | - $options['transactionContext'] |
1676 | | - ) = $this->transactionSelector($options); |
| 1667 | + $executeOptions = $this->pluckArray( |
| 1668 | + ['parameters', 'types'], |
| 1669 | + $options |
| 1670 | + ); |
1677 | 1671 |
|
1678 | 1672 | if (isset($options['transaction']['readWrite'])) { |
1679 | 1673 | $options['transaction']['begin']['isolationLevel'] ??= $this->isolationLevel; |
1680 | 1674 | } |
1681 | 1675 |
|
1682 | | - $options['directedReadOptions'] = $this->configureDirectedReadOptions( |
1683 | | - $options, |
| 1676 | + [$transactionOptions, $transactionContext] = $this->transactionOptionsBuilder->transactionSelector($options); |
| 1677 | + $directedReadOptions = $this->transactionOptionsBuilder->configureDirectedReadOptions( |
| 1678 | + ['transaction' => $transactionOptions] + $options, |
1684 | 1679 | $this->directedReadOptions |
1685 | 1680 | ); |
1686 | 1681 |
|
1687 | | - // Unset the internal flag. |
1688 | | - unset($options['singleUse']); |
1689 | | - return $this->operation->execute($session, $sql, $options + [ |
1690 | | - 'route-to-leader' => $options['transactionContext'] === Database::CONTEXT_READWRITE |
| 1682 | + $session = $options['session'] ?? $this->session; |
| 1683 | + return $this->operation->execute($session, $sql, $executeOptions + [ |
| 1684 | + 'transaction' => $transactionOptions, |
| 1685 | + 'transactionContext' => $transactionContext, |
| 1686 | + 'directedReadOptions' => $directedReadOptions, |
| 1687 | + 'route-to-leader' => $transactionContext === Database::CONTEXT_READWRITE |
1691 | 1688 | ]); |
1692 | 1689 | } |
1693 | 1690 |
|
@@ -2053,20 +2050,20 @@ public function executePartitionedUpdate($statement, array $options = []): int |
2053 | 2050 | */ |
2054 | 2051 | public function read($table, KeySet $keySet, array $columns, array $options = []): Result |
2055 | 2052 | { |
2056 | | - unset($options['requestOptions']['transactionTag']); |
2057 | | - |
2058 | | - list($transactionOptions, $context) = $this->transactionSelector($options); |
2059 | | - $options['transaction'] = $transactionOptions; |
2060 | | - $options['transactionContext'] = $context; |
| 2053 | + [$transactionOptions, $context] = $this->transactionOptionsBuilder->transactionSelector($options); |
2061 | 2054 |
|
2062 | | - $options['directedReadOptions'] = $this->configureDirectedReadOptions( |
2063 | | - $options, |
| 2055 | + $readOptions = $this->pluckArray( |
| 2056 | + ['index', 'limit', 'orderBy', 'lockHint', 'directedReadOptions'], |
| 2057 | + $options |
| 2058 | + ); |
| 2059 | + $readOptions['transactionContext'] = $context; |
| 2060 | + $readOptions['directedReadOptions'] = $this->transactionOptionsBuilder->configureDirectedReadOptions( |
| 2061 | + ['transaction' => $transactionOptions] + $readOptions, |
2064 | 2062 | $this->directedReadOptions |
2065 | 2063 | ); |
| 2064 | + $readOptions['transaction'] = $transactionOptions; |
2066 | 2065 |
|
2067 | | - // Unset the internal flag. |
2068 | | - unset($options['singleUse']); |
2069 | | - return $this->operation->read($this->session, $table, $keySet, $columns, $options + [ |
| 2066 | + return $this->operation->read($this->session, $table, $keySet, $columns, $readOptions + [ |
2070 | 2067 | 'route-to-leader' => $context === Database::CONTEXT_READ |
2071 | 2068 | ]); |
2072 | 2069 | } |
|
0 commit comments