1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Database implementation |
5
|
|
|
*/ |
6
|
|
|
namespace Ubiquity\db; |
7
|
|
|
|
8
|
|
|
use Ubiquity\exceptions\CacheException; |
9
|
|
|
use Ubiquity\db\traits\DatabaseOperationsTrait; |
10
|
|
|
use Ubiquity\exceptions\DBException; |
11
|
|
|
use Ubiquity\db\traits\DatabaseTransactionsTrait; |
12
|
|
|
use Ubiquity\controllers\Startup; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Ubiquity PDO database class. |
16
|
|
|
* Ubiquity\db$Database |
17
|
|
|
* This class is part of Ubiquity |
18
|
|
|
* |
19
|
|
|
* @author jcheron <[email protected]> |
20
|
|
|
* @version 1.0.5 |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
class Database { |
24
|
|
|
use DatabaseOperationsTrait,DatabaseTransactionsTrait; |
25
|
|
|
private $dbType; |
26
|
|
|
private $serverName; |
27
|
|
|
private $port; |
28
|
|
|
private $dbName; |
29
|
|
|
private $user; |
30
|
|
|
private $password; |
31
|
|
|
private $cache; |
32
|
|
|
private $options; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* |
36
|
|
|
* @var \PDO |
37
|
|
|
*/ |
38
|
|
|
protected $pdoObject; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor |
42
|
|
|
* |
43
|
|
|
* @param string $dbName |
44
|
|
|
* @param string $serverName |
45
|
|
|
* @param string $port |
46
|
|
|
* @param string $user |
47
|
|
|
* @param string $password |
48
|
|
|
* @param array $options |
49
|
|
|
* @param boolean|string $cache |
50
|
|
|
*/ |
51
|
94 |
|
public function __construct($dbType, $dbName, $serverName = "127.0.0.1", $port = "3306", $user = "root", $password = "", $options = [], $cache = false) { |
52
|
94 |
|
$this->dbType = $dbType; |
53
|
94 |
|
$this->dbName = $dbName; |
54
|
94 |
|
$this->serverName = $serverName; |
55
|
94 |
|
$this->port = $port; |
56
|
94 |
|
$this->user = $user; |
57
|
94 |
|
$this->password = $password; |
58
|
94 |
|
if (isset ( $options ["quote"] )) |
59
|
1 |
|
SqlUtils::$quote = $options ["quote"]; |
60
|
94 |
|
$this->options = $options; |
61
|
94 |
|
if ($cache !== false) { |
62
|
18 |
|
if ($cache instanceof \Closure) { |
63
|
1 |
|
$this->cache = $cache (); |
64
|
|
|
} else { |
65
|
18 |
|
if (\class_exists ( $cache )) { |
66
|
18 |
|
$this->cache = new $cache (); |
67
|
|
|
} else { |
68
|
1 |
|
throw new CacheException ( $cache . " is not a valid value for database cache" ); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
94 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Creates the PDO instance and realize a safe connection. |
76
|
|
|
* |
77
|
|
|
* @throws DBException |
78
|
|
|
* @return boolean |
79
|
|
|
*/ |
80
|
83 |
|
public function connect() { |
81
|
|
|
try { |
82
|
83 |
|
$this->_connect (); |
83
|
83 |
|
return true; |
84
|
1 |
|
} catch ( \PDOException $e ) { |
85
|
1 |
|
throw new DBException ( $e->getMessage (), $e->getCode (), $e->getPrevious () ); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
84 |
|
public function getDSN() { |
90
|
84 |
|
return $this->dbType . ':dbname=' . $this->dbName . ';host=' . $this->serverName . ';charset=UTF8;port=' . $this->port; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
* @codeCoverageIgnore |
97
|
|
|
*/ |
98
|
|
|
public function getServerName() { |
99
|
|
|
return $this->serverName; |
100
|
|
|
} |
101
|
|
|
|
102
|
3 |
|
public function setServerName($serverName) { |
103
|
3 |
|
$this->serverName = $serverName; |
104
|
3 |
|
} |
105
|
|
|
|
106
|
3 |
|
public function setDbType($dbType) { |
107
|
3 |
|
$this->dbType = $dbType; |
108
|
3 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
* @codeCoverageIgnore |
115
|
|
|
*/ |
116
|
|
|
public function getPort() { |
117
|
|
|
return $this->port; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
* @codeCoverageIgnore |
124
|
|
|
*/ |
125
|
|
|
public function getDbName() { |
126
|
|
|
return $this->dbName; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
* @codeCoverageIgnore |
133
|
|
|
*/ |
134
|
|
|
public function getUser() { |
135
|
|
|
return $this->user; |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
public static function getAvailableDrivers() { |
139
|
2 |
|
return \PDO::getAvailableDrivers (); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* |
144
|
|
|
* @return mixed |
145
|
|
|
* @codeCoverageIgnore |
146
|
|
|
*/ |
147
|
|
|
public function getDbType() { |
148
|
|
|
return $this->dbType; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
* @codeCoverageIgnore |
155
|
|
|
*/ |
156
|
|
|
public function getPassword() { |
157
|
|
|
return $this->password; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
* @codeCoverageIgnore |
164
|
|
|
*/ |
165
|
|
|
public function getOptions() { |
166
|
|
|
return $this->options; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* |
171
|
|
|
* @param string $port |
172
|
|
|
*/ |
173
|
2 |
|
public function setPort($port) { |
174
|
2 |
|
$this->port = $port; |
175
|
2 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* |
179
|
|
|
* @param string $dbName |
180
|
|
|
*/ |
181
|
1 |
|
public function setDbName($dbName) { |
182
|
1 |
|
$this->dbName = $dbName; |
183
|
1 |
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* |
187
|
|
|
* @param string $user |
188
|
|
|
*/ |
189
|
2 |
|
public function setUser($user) { |
190
|
2 |
|
$this->user = $user; |
191
|
2 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* |
195
|
|
|
* @param string $password |
196
|
|
|
*/ |
197
|
1 |
|
public function setPassword($password) { |
198
|
1 |
|
$this->password = $password; |
199
|
1 |
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* |
203
|
|
|
* @param array $options |
204
|
|
|
*/ |
205
|
1 |
|
public function setOptions($options) { |
206
|
1 |
|
$this->options = $options; |
207
|
1 |
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Closes the active pdo connection |
211
|
|
|
*/ |
212
|
23 |
|
public function close() { |
213
|
23 |
|
$this->pdoObject = null; |
214
|
23 |
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Starts and returns a database instance corresponding to an offset in config |
218
|
|
|
* |
219
|
|
|
* @param string $offset |
220
|
|
|
* @return \Ubiquity\db\Database|NULL |
221
|
|
|
*/ |
222
|
|
|
public static function start($offset = null) { |
223
|
|
|
$config = Startup::$config; |
224
|
|
|
$db = $offset ? ($config ['database'] [$offset] ?? ($config ['database'] ?? [ ])) : ($config ['database'] ?? [ ]); |
225
|
|
|
if ($db ['dbName'] !== '') { |
226
|
|
|
$database = new Database ( $db ['type'], $db ['dbName'], $db ['serverName'] ?? '127.0.0.1', $db ['port'] ?? 3306, $db ['user'] ?? 'root', $db ['password'] ?? '', $db ['options'] ?? [ ], $db ['cache'] ?? false); |
227
|
|
|
$database->connect (); |
228
|
|
|
return $database; |
229
|
|
|
} |
230
|
|
|
return null; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|