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