1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Database\Driver; |
3
|
|
|
|
4
|
|
|
use WebStream\DI\Injector; |
5
|
|
|
use WebStream\Module\Container; |
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* DatabaseDriver |
10
|
|
|
* @author Ryuichi TANAKA. |
11
|
|
|
* @since 2013/12/07 |
12
|
|
|
* @version 0.4 |
13
|
|
|
*/ |
14
|
|
|
abstract class DatabaseDriver |
15
|
|
|
{ |
16
|
|
|
use Injector; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var object DBオブジェクト |
20
|
|
|
*/ |
21
|
|
|
protected $connection; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Container DB接続設定 |
25
|
|
|
*/ |
26
|
|
|
protected $config; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* constructor |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Container $config) |
32
|
|
|
{ |
33
|
|
|
$this->config = $config; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* destructor |
38
|
|
|
*/ |
39
|
|
|
public function __destruct() |
40
|
|
|
{ |
41
|
|
|
// $this->logger->debug("Release driver: " . get_class($this)); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* 接続する |
46
|
|
|
*/ |
47
|
|
|
abstract public function connect(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* 切断する |
51
|
|
|
*/ |
52
|
|
|
public function disconnect() |
53
|
|
|
{ |
54
|
|
|
if ($this->connection !== null) { |
55
|
|
|
$this->logger->debug("Database disconnect."); |
|
|
|
|
56
|
|
|
$this->connection->close(); |
57
|
|
|
$this->connection = null; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* トランザクションを開始する |
63
|
|
|
* @return boolean トランザクション開始結果 |
64
|
|
|
*/ |
65
|
|
|
public function beginTransaction() |
66
|
|
|
{ |
67
|
|
|
$inTransaction = false; |
68
|
|
|
if ($this->connection !== null) { |
69
|
|
|
$this->connection->beginTransaction(); |
70
|
|
|
$inTransaction = $this->inTransaction(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $inTransaction; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* コミットする |
78
|
|
|
*/ |
79
|
|
|
public function commit() |
80
|
|
|
{ |
81
|
|
|
if ($this->connection !== null) { |
82
|
|
|
$this->connection->commit(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* ロールバックする |
88
|
|
|
*/ |
89
|
|
|
public function rollback() |
90
|
|
|
{ |
91
|
|
|
if ($this->inTransaction()) { |
92
|
|
|
$this->connection->rollback(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* DB接続されているか |
98
|
|
|
* @param boolean 接続有無 |
99
|
|
|
*/ |
100
|
|
|
public function isConnected() |
101
|
|
|
{ |
102
|
|
|
return $this->connection !== null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* トランザクション内かどうか |
107
|
|
|
* @return boolean トランザクション内かどうか |
108
|
|
|
*/ |
109
|
|
|
public function inTransaction() |
110
|
|
|
{ |
111
|
|
|
return $this->connection !== null ? $this->connection->isTransactionActive() : false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* SQLをセットしてステートメントを返却する |
116
|
|
|
* @param string SQL |
117
|
|
|
* @return object ステートメント |
118
|
|
|
*/ |
119
|
|
|
public function getStatement($sql) |
120
|
|
|
{ |
121
|
|
|
return $this->connection !== null ? $this->connection->prepare($sql) : null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* トランザクション分離レベルを返却する |
126
|
|
|
* @return int トランザクション分離レベル |
127
|
|
|
*/ |
128
|
|
|
public function getTransactionIsolation() |
129
|
|
|
{ |
130
|
|
|
return $this->connection->getTransactionIsolation(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* トランザクション分離レベルを設定する |
135
|
|
|
* @param int $isolationLevel トランザクション分離レベル |
136
|
|
|
*/ |
137
|
|
|
public function setTransactionIsolation(int $isolationLevel) |
138
|
|
|
{ |
139
|
|
|
$this->connection->setTransactionIsolation($isolationLevel); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.