1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Plasma Core component |
4
|
|
|
* Copyright 2018 PlasmaPHP, All Rights Reserved |
5
|
|
|
* |
6
|
|
|
* Website: https://github.com/PlasmaPHP |
7
|
|
|
* License: https://github.com/PlasmaPHP/core/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Plasma; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Represents a transaction. |
14
|
|
|
*/ |
15
|
|
|
class Transaction implements TransactionInterface { |
16
|
|
|
/** |
17
|
|
|
* @var \Plasma\ClientInterface |
18
|
|
|
*/ |
19
|
|
|
protected $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Plasma\DriverInterface|null |
23
|
|
|
*/ |
24
|
|
|
protected $driver; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $isolation; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Creates a client with the specified factory and options. |
33
|
|
|
* @param \Plasma\ClientInterface $client |
34
|
|
|
* @param \Plasma\DriverInterface $driver |
35
|
|
|
* @param int $isolation |
36
|
|
|
* @throws \Plasma\Exception Thrown if the transaction isolation level is invalid. |
37
|
|
|
*/ |
38
|
21 |
|
function __construct(\Plasma\ClientInterface $client, \Plasma\DriverInterface $driver, int $isolation) { |
39
|
21 |
|
switch($isolation) { |
40
|
|
|
case \Plasma\TransactionInterface::ISOLATION_UNCOMMITTED: |
41
|
|
|
case \Plasma\TransactionInterface::ISOLATION_COMMITTED: |
42
|
|
|
case \Plasma\TransactionInterface::ISOLATION_REPEATABLE: |
43
|
|
|
case \Plasma\TransactionInterface::ISOLATION_SERIALIZABLE: |
44
|
|
|
// Valid isolation level |
45
|
20 |
|
break; |
46
|
|
|
default: |
47
|
1 |
|
throw new \Plasma\Exception('Invalid isolation level given'); |
48
|
|
|
break; |
49
|
|
|
} |
50
|
|
|
|
51
|
20 |
|
$this->client = $client; |
52
|
20 |
|
$this->driver = $driver; |
53
|
20 |
|
$this->isolation = $isolation; |
54
|
20 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Destructor. Implicit rollback and automatically checks the connection back into the client on deallocation. |
58
|
|
|
*/ |
59
|
20 |
|
function __destruct() { |
60
|
20 |
|
if($this->driver !== null && $this->driver->getConnectionState() === \Plasma\DriverInterface::CONNECTION_OK) { |
61
|
|
|
$this->rollback()->then(null, function () { |
62
|
1 |
|
if($this->driver !== null) { |
63
|
|
|
// Error during implicit rollback, close the session |
64
|
1 |
|
$this->driver->close(); |
65
|
|
|
} |
66
|
2 |
|
}); |
67
|
|
|
} |
68
|
20 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the isolation level for this transaction. |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
1 |
|
function getIsolationLevel(): int { |
75
|
1 |
|
return $this->isolation; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Whether the transaction is still active, or has been committed/rolled back. |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
2 |
|
function isActive(): bool { |
83
|
2 |
|
return ($this->driver !== null); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Executes a plain query. Resolves with a `QueryResult` instance. |
88
|
|
|
* @param string $query |
89
|
|
|
* @return \React\Promise\PromiseInterface |
90
|
|
|
* @throws \Plasma\TransactionException Thrown if the transaction has been committed or rolled back. |
91
|
|
|
* @see \Plasma\QueryResultInterface |
92
|
|
|
*/ |
93
|
12 |
|
function query(string $query): \React\Promise\PromiseInterface { |
94
|
12 |
|
if($this->driver === null) { |
95
|
1 |
|
throw new \Plasma\TransactionException('Transaction has been committed or rolled back'); |
96
|
|
|
} |
97
|
|
|
|
98
|
12 |
|
return $this->driver->query($this->client, $query); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Prepares a query. Resolves with a `StatementInterface` instance. |
103
|
|
|
* @param string $query |
104
|
|
|
* @return \React\Promise\PromiseInterface |
105
|
|
|
* @throws \Plasma\TransactionException Thrown if the transaction has been committed or rolled back. |
106
|
|
|
* @see \Plasma\StatementInterface |
107
|
|
|
*/ |
108
|
2 |
|
function prepare(string $query): \React\Promise\PromiseInterface { |
109
|
2 |
|
if($this->driver === null) { |
110
|
1 |
|
throw new \Plasma\TransactionException('Transaction has been committed or rolled back'); |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
return $this->driver->prepare($this->client, $query); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Prepares and executes a query. Resolves with a `QueryResultInterface` instance. |
118
|
|
|
* This is equivalent to prepare -> execute -> close. |
119
|
|
|
* If you need to execute a query multiple times, prepare the query manually for performance reasons. |
120
|
|
|
* @param string $query |
121
|
|
|
* @param array $params |
122
|
|
|
* @return \React\Promise\PromiseInterface |
123
|
|
|
* @throws \Plasma\TransactionException Thrown if the transaction has been committed or rolled back. |
124
|
|
|
* @throws \Plasma\Exception |
125
|
|
|
* @see \Plasma\StatementInterface |
126
|
|
|
*/ |
127
|
|
|
function execute(string $query, array $params = array()): \React\Promise\PromiseInterface { |
128
|
|
|
if($this->driver === null) { |
129
|
|
|
throw new \Plasma\TransactionException('Transaction has been committed or rolled back'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->driver->execute($this->client, $query, $params); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Quotes the string for use in the query. |
137
|
|
|
* @param string $str |
138
|
|
|
* @return string |
139
|
|
|
* @throws \LogicException Thrown if the driver does not support quoting. |
140
|
|
|
* @throws \Plasma\TransactionException Thrown if the transaction has been committed or rolled back. |
141
|
|
|
*/ |
142
|
5 |
|
function quote(string $str): string { |
143
|
5 |
|
if($this->driver === null) { |
144
|
1 |
|
throw new \Plasma\TransactionException('Transaction has been committed or rolled back'); |
145
|
|
|
} |
146
|
|
|
|
147
|
4 |
|
return $this->driver->quote($str); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Commits the changes. |
152
|
|
|
* @return \React\Promise\PromiseInterface |
153
|
|
|
* @throws \Plasma\TransactionException Thrown if the transaction has been committed or rolled back. |
154
|
|
|
*/ |
155
|
4 |
|
function commit(): \React\Promise\PromiseInterface { |
156
|
|
|
return $this->query('COMMIT')->then(function () { |
157
|
4 |
|
$this->driver->endTransaction(); |
158
|
4 |
|
$this->client->checkinConnection($this->driver); |
159
|
4 |
|
$this->driver = null; |
160
|
4 |
|
}); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Rolls back the changes. |
165
|
|
|
* @return \React\Promise\PromiseInterface |
166
|
|
|
* @throws \Plasma\TransactionException Thrown if the transaction has been committed or rolled back. |
167
|
|
|
*/ |
168
|
4 |
|
function rollback(): \React\Promise\PromiseInterface { |
169
|
|
|
return $this->query('ROLLBACK')->then(function () { |
170
|
3 |
|
$this->driver->endTransaction(); |
171
|
3 |
|
$this->client->checkinConnection($this->driver); |
172
|
3 |
|
$this->driver = null; |
173
|
4 |
|
}); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Creates a savepoint with the given identifier. |
178
|
|
|
* @param string $identifier |
179
|
|
|
* @return \React\Promise\PromiseInterface |
180
|
|
|
* @throws \Plasma\TransactionException Thrown if the transaction has been committed or rolled back. |
181
|
|
|
*/ |
182
|
1 |
|
function createSavepoint(string $identifier): \React\Promise\PromiseInterface { |
183
|
1 |
|
return $this->query('SAVEPOINT '.$this->quote($identifier)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Rolls back to the savepoint with the given identifier. |
188
|
|
|
* @param string $identifier |
189
|
|
|
* @return \React\Promise\PromiseInterface |
190
|
|
|
* @throws \Plasma\TransactionException Thrown if the transaction has been committed or rolled back. |
191
|
|
|
*/ |
192
|
1 |
|
function rollbackTo(string $identifier): \React\Promise\PromiseInterface { |
193
|
1 |
|
return $this->query('ROLLBACK TO '.$this->quote($identifier)); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Releases the savepoint with the given identifier. |
198
|
|
|
* @param string $identifier |
199
|
|
|
* @return \React\Promise\PromiseInterface |
200
|
|
|
* @throws \Plasma\TransactionException Thrown if the transaction has been committed or rolled back. |
201
|
|
|
*/ |
202
|
1 |
|
function releaseSavepoint(string $identifier): \React\Promise\PromiseInterface { |
203
|
1 |
|
return $this->query('RELEASE SAVEPOINT '.$this->quote($identifier)); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|