1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ez\DbLinker\Driver\Connection; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Driver\Connection; |
6
|
|
|
use Doctrine\DBAL\Driver\PDOConnection; |
7
|
|
|
use Doctrine\DBAL\DriverManager; |
8
|
|
|
use Ez\DbLinker\RetryStrategy; |
9
|
|
|
|
10
|
|
|
class RetryConnection implements Connection |
11
|
|
|
{ |
12
|
|
|
private $wrappedConnectionParams; |
13
|
|
|
private $retryStrategy; |
14
|
|
|
private $transactionLevel = 0; |
15
|
|
|
private $wrappedConnection; |
16
|
|
|
private $wrappedDriver; |
17
|
|
|
|
18
|
|
|
use CallAndRetry; |
19
|
|
|
|
20
|
|
|
public function __construct(Array $wrappedConnectionParams, RetryStrategy $retryStrategy) |
21
|
|
|
{ |
22
|
|
|
$this->wrappedConnectionParams = $wrappedConnectionParams; |
23
|
|
|
$this->retryStrategy = $retryStrategy; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Prepares a statement for execution and returns a Statement object. |
28
|
|
|
* |
29
|
|
|
* @param string $prepareString |
30
|
|
|
* |
31
|
|
|
* @return \Doctrine\DBAL\Driver\Statement |
32
|
|
|
*/ |
33
|
|
|
public function prepare($prepareString) { |
34
|
|
|
return new RetryStatement( |
35
|
|
|
$this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args()), |
36
|
|
|
$this, |
37
|
|
|
$this->retryStrategy |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Executes an SQL statement, returning a result set as a Statement object. |
43
|
|
|
* |
44
|
|
|
* @return \Doctrine\DBAL\Driver\Statement |
45
|
|
|
* @throws \Exception |
46
|
|
|
*/ |
47
|
|
|
public function query() { |
48
|
|
|
return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Quotes a string for use in a query. |
53
|
|
|
* |
54
|
|
|
* @param string $input |
55
|
|
|
* @param integer $type |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function quote($input, $type = \PDO::PARAM_STR) { |
60
|
|
|
return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Executes an SQL statement and return the number of affected rows. |
65
|
|
|
* |
66
|
|
|
* @param string $statement |
67
|
|
|
* |
68
|
|
|
* @return integer |
69
|
|
|
*/ |
70
|
|
|
public function exec($statement) { |
71
|
|
|
return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the ID of the last inserted row or sequence value. |
76
|
|
|
* |
77
|
|
|
* @param string|null $name |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function lastInsertId($name = null) { |
82
|
|
|
return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Initiates a transaction. |
87
|
|
|
* |
88
|
|
|
* @return boolean TRUE on success or FALSE on failure. |
89
|
|
|
*/ |
90
|
|
|
public function beginTransaction() { |
91
|
|
|
$this->transactionLevel++; |
92
|
|
|
return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Commits a transaction. |
97
|
|
|
* |
98
|
|
|
* @return boolean TRUE on success or FALSE on failure. |
99
|
|
|
*/ |
100
|
|
|
public function commit() { |
101
|
|
|
$this->transactionLevel--; |
102
|
|
|
return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Rolls back the current transaction, as initiated by beginTransaction(). |
107
|
|
|
* |
108
|
|
|
* @return boolean TRUE on success or FALSE on failure. |
109
|
|
|
*/ |
110
|
|
|
public function rollBack() { |
111
|
|
|
$this->transactionLevel--; |
112
|
|
|
return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns the error code associated with the last operation on the database handle. |
117
|
|
|
* |
118
|
|
|
* @return string|null The error code, or null if no operation has been run on the database handle. |
119
|
|
|
*/ |
120
|
|
|
public function errorCode() { |
121
|
|
|
return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns extended error information associated with the last operation on the database handle. |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function errorInfo() { |
130
|
|
|
return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function close() |
134
|
|
|
{ |
135
|
|
|
if ($this->wrappedConnection instanceof PDOConnection) { |
136
|
|
|
} elseif (method_exists($this->wrappedConnection, "getWrappedResourceHandle")) { |
137
|
|
|
$this->wrappedConnection->getWrappedResourceHandle()->close(); |
138
|
|
|
} |
139
|
|
|
$this->wrappedConnection = null; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function transactionLevel() |
143
|
|
|
{ |
144
|
|
|
return $this->transactionLevel; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param $method |
149
|
|
|
* @param array $arguments |
150
|
|
|
* @return mixed |
151
|
|
|
* @throws \Exception |
152
|
|
|
*/ |
153
|
|
|
private function callWrappedConnectionAndRetry($method, array $arguments) |
154
|
|
|
{ |
155
|
|
|
return $this->callAndRetry(function () use ($method, $arguments) { |
156
|
|
|
return \call_user_func_array([$this->wrappedConnection(), $method], $arguments); |
157
|
|
|
}, $this->retryStrategy, $this); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
protected function wrap() |
161
|
|
|
{ |
162
|
|
|
$connection = $this->wrappedConnection = DriverManager::getConnection( |
163
|
|
|
$this->wrappedConnectionParams |
164
|
|
|
); |
165
|
|
|
$this->wrappedConnection = $connection->getWrappedConnection(); |
166
|
|
|
$this->wrappedDriver = $connection->getDriver(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @inherit |
171
|
|
|
* @return \Doctrine\DBAL\Driver\PDOConnection |
172
|
|
|
*/ |
173
|
|
|
public function wrappedConnection() |
174
|
|
|
{ |
175
|
|
|
if ($this->wrappedConnection === null) { |
176
|
|
|
$this->wrap(); |
177
|
|
|
} |
178
|
|
|
return $this->wrappedConnection; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function wrappedDriver() |
182
|
|
|
{ |
183
|
|
|
if ($this->wrappedDriver === null) { |
184
|
|
|
$this->wrap(); |
185
|
|
|
} |
186
|
|
|
return $this->wrappedDriver; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
|