1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Database; |
3
|
|
|
|
4
|
|
|
use WebStream\DI\Injector; |
5
|
|
|
use WebStream\Module\Container; |
6
|
|
|
use WebStream\Exception\Extend\DatabaseException; |
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* DatabaseManager |
11
|
|
|
* @author Ryuichi TANAKA. |
12
|
|
|
* @since 2013/12/07 |
13
|
|
|
* @version 0.4 |
14
|
|
|
*/ |
15
|
|
|
class DatabaseManager |
16
|
|
|
{ |
17
|
|
|
use Injector; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ConnectionManager コネクションマネージャ |
21
|
|
|
*/ |
22
|
|
|
private $connectionManager; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var DatabaseDriver データベースコネクション |
26
|
|
|
*/ |
27
|
|
|
private $connection; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Query クエリオブジェクト |
31
|
|
|
*/ |
32
|
|
|
private $query; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Logger ロガー |
36
|
|
|
*/ |
37
|
|
|
private $logger; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* constructor |
41
|
|
|
* @param Container 依存コンテナ |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Container $container) |
44
|
|
|
{ |
45
|
|
|
$this->connectionManager = new ConnectionManager($container); |
46
|
|
|
$this->logger = $container->logger; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* destructor |
51
|
|
|
*/ |
52
|
|
|
public function __destruct() |
53
|
|
|
{ |
54
|
|
|
$this->disconnect(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* データベース接続する |
59
|
|
|
* すでに接続中であれば再接続はしない |
60
|
|
|
*/ |
61
|
|
|
public function connect() |
62
|
|
|
{ |
63
|
|
|
try { |
64
|
|
|
$this->connection->connect(); |
65
|
|
|
$this->query = new Query($this->connection); |
66
|
|
|
$this->query->inject('logger', $this->logger); |
67
|
|
|
} catch (\PDOException $e) { |
68
|
|
|
throw new DatabaseException($e); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* データベース切断する |
74
|
|
|
*/ |
75
|
|
|
public function disconnect() |
76
|
|
|
{ |
77
|
|
|
if ($this->connection === null) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($this->inTransaction()) { |
82
|
|
|
// トランザクションが明示的に開始された状態でcommit/rollbackが行われていない場合 |
83
|
|
|
// ログに警告を書き込み、強制的にロールバックする |
84
|
|
|
$this->connection->rollback(); |
85
|
|
|
$this->logger->warn("Not has been executed commit or rollback after the transaction started."); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->connection->disconnect(); |
89
|
|
|
$this->connection = null; |
90
|
|
|
$this->query = null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* トランザクションを開始する |
95
|
|
|
* @param int $isolationLevel トランザクション分離レベル |
96
|
|
|
*/ |
97
|
|
|
public function beginTransaction(int $isolationLevel) |
98
|
|
|
{ |
99
|
|
|
// 既にトランザクションが開始されている場合、継続しているトランザクションを有効のままにする |
100
|
|
|
// トランザクションを破棄して再度開始する場合は明示的に破棄してから再呼び出しする |
101
|
|
|
if ($this->inTransaction()) { |
102
|
|
|
$this->logger->debug("Transaction already started."); |
103
|
|
|
|
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (!$this->connection->beginTransaction()) { |
108
|
|
|
throw new DatabaseException("Failed to start transaction."); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($isolationLevel === Connection::TRANSACTION_READ_UNCOMMITTED || |
112
|
|
|
$isolationLevel === Connection::TRANSACTION_READ_COMMITTED || |
113
|
|
|
$isolationLevel === Connection::TRANSACTION_REPEATABLE_READ || |
114
|
|
|
$isolationLevel === Connection::TRANSACTION_SERIALIZABLE) { |
115
|
|
|
$this->connection->setTransactionIsolation($isolationLevel); |
116
|
|
|
} else { |
117
|
|
|
throw new DatabaseException("Invalid transaction isolation level: " . $isolationLevel); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->logger->debug("Transaction start."); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* コミットする |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function commit() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
try { |
129
|
|
|
if ($this->connection !== null) { |
130
|
|
|
if ($this->inTransaction()) { |
131
|
|
|
$this->connection->commit(); |
132
|
|
|
$this->logger->debug("Execute commit."); |
133
|
|
|
} else { |
134
|
|
|
$this->logger->warn("Not executed commit because the transaction is not started."); |
135
|
|
|
} |
136
|
|
|
} else { |
137
|
|
|
throw new DatabaseException("Can't execute commit."); |
138
|
|
|
} |
139
|
|
|
} catch (\PDOException $e) { |
140
|
|
|
$this->query = null; |
141
|
|
|
throw new DatabaseException($e); |
142
|
|
|
} finally { |
143
|
|
|
$this->disconnect(); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* ロールバックする |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
public function rollback() |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
try { |
153
|
|
|
if ($this->connection !== null) { |
154
|
|
|
if ($this->inTransaction()) { |
155
|
|
|
$this->connection->rollback(); |
156
|
|
|
$this->logger->debug("Execute rollback."); |
157
|
|
|
} else { |
158
|
|
|
$this->logger->warn("Not executed rollback because the transaction is not started."); |
159
|
|
|
} |
160
|
|
|
} else { |
161
|
|
|
throw new DatabaseException("Can't execute rollback."); |
162
|
|
|
} |
163
|
|
|
} catch (\PDOException $e) { |
164
|
|
|
$this->query = null; |
165
|
|
|
throw new DatabaseException($e); |
166
|
|
|
} finally { |
167
|
|
|
$this->disconnect(); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* ロールバックが発生したかどうか |
173
|
|
|
* @return boolean ロールバックが発生したかどうか |
174
|
|
|
*/ |
175
|
|
|
public function isRollback() |
176
|
|
|
{ |
177
|
|
|
return $this->isRollback; |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* トランザクション内かどうか |
182
|
|
|
* @return boolean トランザクション内かどうか |
183
|
|
|
*/ |
184
|
|
|
public function inTransaction() |
185
|
|
|
{ |
186
|
|
|
return $this->connection->inTransaction(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* DB接続されているか |
191
|
|
|
* @param boolean 接続有無 |
192
|
|
|
*/ |
193
|
|
|
public function isConnected() |
194
|
|
|
{ |
195
|
|
|
return $this->connection->isConnected(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* トランザクション分離レベルを返却する |
200
|
|
|
* @return int トランザクション分離レベル |
201
|
|
|
*/ |
202
|
|
|
public function getTransactionIsolation() |
203
|
|
|
{ |
204
|
|
|
return $this->connection->getTransactionIsolation(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* データベース接続が可能かどうか |
209
|
|
|
* @param string Modelファイルパス |
210
|
|
|
* @return boolean 接続可否 |
211
|
|
|
*/ |
212
|
|
|
public function loadConnection($filepath) |
213
|
|
|
{ |
214
|
|
|
$connection = $this->connectionManager->getConnection($filepath); |
215
|
|
|
if ($connection !== null) { |
216
|
|
|
$this->connection = $connection; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return $this->connection !== null; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* クエリを設定する |
224
|
|
|
* @param string SQL |
225
|
|
|
* @param array<string> パラメータ |
226
|
|
|
*/ |
227
|
|
|
public function query($sql, array $bind = []) |
228
|
|
|
{ |
229
|
|
|
if ($this->query === null) { |
230
|
|
|
throw new DatabaseException("Query does not set because database connection failed."); |
231
|
|
|
} |
232
|
|
|
$this->query->setSql($sql); |
233
|
|
|
$this->query->setBind($bind); |
234
|
|
|
|
235
|
|
|
return $this->query; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.