forked from googleapis/google-cloud-php-datastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransactionTrait.php
176 lines (165 loc) · 5.18 KB
/
TransactionTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Datastore;
use Google\Cloud\Datastore\Query\QueryInterface;
/**
* Common operations for datastore transactions.
*/
trait TransactionTrait
{
/**
* @var Operation
*/
private $operation;
/**
* @var string
*/
private $projectId;
/**
* @var string
*/
private $transactionId;
/**
* Create a Transaction
*
* @param Operation $operation Class that handles shared API interaction.
* @param string $projectId The Google Cloud Platform project ID.
* @param string $transactionId The transaction to run mutations in.
*/
public function __construct(
Operation $operation,
$projectId,
$transactionId
) {
$this->operation = $operation;
$this->projectId = $projectId;
$this->transactionId = $transactionId;
}
/**
* Retrieve an entity from the datastore inside a transaction
*
* Example:
* ```
* $key = $datastore->key('Person', 'Bob');
*
* $entity = $transaction->lookup($key);
* if (!is_null($entity)) {
* echo $entity['firstName']; // 'Bob'
* }
* ```
*
* @param Key $key $key The identifier to use to locate a desired entity.
* @param array $options [optional] {
* Configuration Options
*
* @type string $className The name of the class to return results as.
* Must be a subclass of {@see Google\Cloud\Datastore\Entity}.
* If not set, {@see Google\Cloud\Datastore\Entity} will be used.
* }
* @return Entity|null
*/
public function lookup(Key $key, array $options = [])
{
$res = $this->lookupBatch([$key], $options);
return (isset($res['found'][0]))
? $res['found'][0]
: null;
}
/**
* Get multiple entities inside a transaction
*
* Example:
* ```
* $keys = [
* $datastore->key('Person', 'Bob'),
* $datastore->key('Person', 'John')
* ];
*
* $entities = $transaction->lookupBatch($keys);
*
* foreach ($entities['found'] as $entity) {
* echo $entity['firstName'] . PHP_EOL;
* }
* ```
*
* @param Key[] $key The identifiers to look up.
* @param array $options [optional] {
* Configuration Options
*
* @type string|array $className If a string, the name of the class to return results as.
* Must be a subclass of {@see Google\Cloud\Datastore\Entity}.
* If not set, {@see Google\Cloud\Datastore\Entity} will be used.
* If an array is given, it must be an associative array, where
* the key is a Kind and the value is the name of a subclass of
* {@see Google\Cloud\Datastore\Entity}.
* @type bool $sort If set to true, results in each set will be sorted
* to match the order given in $keys. **Defaults to** `false`.
* }
* @return array Returns an array with keys [`found`, `missing`, and `deferred`].
* Members of `found` will be instance of
* {@see Google\Cloud\Datastore\Entity}. Members of `missing` and
* `deferred` will be instance of {@see Google\Cloud\Datastore\Key}.
*/
public function lookupBatch(array $keys, array $options = [])
{
return $this->operation->lookup($keys, $options + [
'transaction' => $this->transactionId
]);
}
/**
* Run a query and return entities inside a Transaction
*
* Example:
* ```
* $result = $transaction->runQuery($query);
*
* foreach ($result as $entity) {
* echo $entity['firstName'];
* }
* ```
*
* @param QueryInterface $query The query object.
* @param array $options [optional] {
* Configuration Options
*
* @type string $className The name of the class to return results as.
* Must be a subclass of {@see Google\Cloud\Datastore\Entity}.
* If not set, {@see Google\Cloud\Datastore\Entity} will be used.
* }
* @return EntityIterator<Google\Cloud\Datastore\Entity>
*/
public function runQuery(QueryInterface $query, array $options = [])
{
return $this->operation->runQuery($query, $options + [
'transaction' => $this->transactionId
]);
}
/**
* Roll back a Transaction
*
* Example:
* ```
* $transaction->rollback();
* ```
*
* @return void
*/
public function rollback()
{
return $this->operation->rollback($this->transactionId);
}
}