1
|
|
|
<?php |
2
|
|
|
defined('ROOT_PATH') || exit('Access denied'); |
3
|
|
|
/** |
4
|
|
|
* TNH Framework |
5
|
|
|
* |
6
|
|
|
* A simple PHP framework using HMVC architecture |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2017 TNH Framework |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
class DatabaseConnection extends BaseClass { |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The PDO instance |
35
|
|
|
* @var object |
36
|
|
|
*/ |
37
|
|
|
private $pdo = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The database driver name to use |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $driver = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The database hostname |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $hostname = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The database port |
53
|
|
|
* @var integer |
54
|
|
|
*/ |
55
|
|
|
private $port = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The database username |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
private $username = null; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The database password |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
private $password = null; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The database name used for the application |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
private $databaseName = null; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* The database charset |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
private $charset = null; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* The database collation |
83
|
|
|
* @var string |
84
|
|
|
*/ |
85
|
|
|
private $collation = null; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* The database tables prefix |
89
|
|
|
* @var string |
90
|
|
|
*/ |
91
|
|
|
private $prefix = null; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* The database configuration |
95
|
|
|
* @var array |
96
|
|
|
*/ |
97
|
|
|
private $config = array(); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Construct new DatabaseConnection |
101
|
|
|
* |
102
|
|
|
* @param array $config the database configuration config |
103
|
|
|
* @param boolean $autoConnect whether to connect to database automatically |
104
|
|
|
*/ |
105
|
|
|
public function __construct(array $config = array(), $autoConnect = false) { |
106
|
|
|
parent::__construct(); |
107
|
|
|
//Note need use the method to set config |
108
|
|
|
$this->setConfig($config); |
109
|
|
|
|
110
|
|
|
//if we need connect or not |
111
|
|
|
if ($autoConnect) { |
112
|
|
|
$this->connect(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* This is method is used to connect to database |
118
|
|
|
* |
119
|
|
|
* @return boolean true in case of successfully connection false if error |
120
|
|
|
*/ |
121
|
|
|
public function connect() { |
122
|
|
|
try { |
123
|
|
|
$options = array( |
124
|
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ |
125
|
|
|
); |
126
|
|
|
$this->pdo = new PDO($this->getDsnValue(), $this->getUsername(), $this->getPassword(), $options); |
127
|
|
|
if($this->getDriver() == 'mysql') { |
128
|
|
|
$this->pdo->exec("SET NAMES '" . $this->getCharset() . "' COLLATE '" . $this->getCollation() . "'"); |
129
|
|
|
$this->pdo->exec("SET CHARACTER SET '" . $this->getCharset() . "'"); |
130
|
|
|
} |
131
|
|
|
return is_object($this->pdo); |
132
|
|
|
} catch (PDOException $e) { |
133
|
|
|
$this->logger->critical($e->getMessage()); |
134
|
|
|
show_error('Cannot connect to Database.'); |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Disconnect from database server |
141
|
|
|
*/ |
142
|
|
|
public function disconnect() { |
143
|
|
|
$this->pdo = null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Escape the data before execute query useful for security. |
148
|
|
|
* @param mixed $data the data to be escaped |
149
|
|
|
* @param boolean $escaped whether we can do escape of not |
150
|
|
|
* @return mixed the data after escaped or the same data if no |
151
|
|
|
* need escaped |
152
|
|
|
*/ |
153
|
|
|
public function escape($data, $escaped = true) { |
154
|
|
|
$data = trim($data); |
155
|
|
|
if ($escaped) { |
156
|
|
|
return $this->pdo->quote($data); |
157
|
|
|
} |
158
|
|
|
return $data; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get the DSN value for the configuration driver |
163
|
|
|
* |
164
|
|
|
* @return string|null the dsn value |
165
|
|
|
*/ |
166
|
|
|
public function getDsnValue() { |
167
|
|
|
$dsn = ''; |
168
|
|
|
$port = $this->getPort(); |
169
|
|
|
$driver = $this->getDriver(); |
170
|
|
|
|
171
|
|
|
$driversDsn = array( |
172
|
|
|
'mysql' => 'mysql:host=' . $this->getHostname() . ';%sdbname=' . $this->getDatabase() . ';charset=' . $this->getCharset(), |
173
|
|
|
'pgsql' => 'pgsql:host=' . $this->getHostname() . ';%sdbname=' . $this->getDatabase() . ';charset=' . $this->getCharset(), |
174
|
|
|
'oracle' => 'oci:dbname=' . $this->getHostname() . '%s/' . $this->getDatabase() . ';charset=' . $this->getCharset(), |
175
|
|
|
'sqlite' => 'sqlite:' . $this->getDatabase() |
176
|
|
|
); |
177
|
|
|
if ($port) { |
178
|
|
|
$driversPort = array( |
179
|
|
|
'mysql' => 'port=' . $port . ';', |
180
|
|
|
'pgsql' => 'port=' . $port . ';', |
181
|
|
|
'oracle' => ':' . $port |
182
|
|
|
); |
183
|
|
|
if (isset($driversPort[$driver])) { |
184
|
|
|
$port = $driversPort[$driver]; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
if (isset($driversDsn[$driver])) { |
188
|
|
|
$dsn = sprintf($driversDsn[$driver], $port); |
189
|
|
|
} |
190
|
|
|
return $dsn; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Return the PDO instance |
195
|
|
|
* @return object |
196
|
|
|
*/ |
197
|
|
|
public function getPdo() { |
198
|
|
|
return $this->pdo; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Set the PDO object |
203
|
|
|
* @param object $pdo the instance of PDO |
204
|
|
|
* |
205
|
|
|
* @return object the current instance |
206
|
|
|
*/ |
207
|
|
|
public function setPdo(PDO $pdo = null) { |
208
|
|
|
$this->pdo = $pdo; |
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Return the driver |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
public function getDriver() { |
218
|
|
|
return $this->driver; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Set the driver |
223
|
|
|
* |
224
|
|
|
* @param string $driver the drive to set like "pgsql", "mysql", etc. |
225
|
|
|
* |
226
|
|
|
* @return object the current instance |
227
|
|
|
*/ |
228
|
|
|
public function setDriver($driver) { |
229
|
|
|
$this->driver = $driver; |
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Return the hostname |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
|
|
public function getHostname() { |
238
|
|
|
return $this->hostname; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Set the hostname |
243
|
|
|
* @param string $hostname the hostname to set |
244
|
|
|
* |
245
|
|
|
* @return object the current instance |
246
|
|
|
*/ |
247
|
|
|
public function setHostname($hostname) { |
248
|
|
|
$this->hostname = $hostname; |
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Return the port |
254
|
|
|
* |
255
|
|
|
* @return int |
256
|
|
|
*/ |
257
|
|
|
public function getPort() { |
258
|
|
|
return $this->port; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Set the port number |
263
|
|
|
* |
264
|
|
|
* @param int $port the port to set |
265
|
|
|
* |
266
|
|
|
* @return object the current instance |
267
|
|
|
*/ |
268
|
|
|
public function setPort($port) { |
269
|
|
|
$this->port = $port; |
270
|
|
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Return the username |
275
|
|
|
* |
276
|
|
|
* @return string |
277
|
|
|
*/ |
278
|
|
|
public function getUsername() { |
279
|
|
|
return $this->username; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Set the username |
284
|
|
|
* |
285
|
|
|
* @param string $username the username to set |
286
|
|
|
* |
287
|
|
|
* @return object the current instance |
288
|
|
|
*/ |
289
|
|
|
public function setUsername($username) { |
290
|
|
|
$this->username = $username; |
291
|
|
|
return $this; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Return the password |
296
|
|
|
* @return string |
297
|
|
|
*/ |
298
|
|
|
public function getPassword() { |
299
|
|
|
return $this->password; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Set the password |
304
|
|
|
* |
305
|
|
|
* @param string $password the password to set |
306
|
|
|
* |
307
|
|
|
* @return object the current instance |
308
|
|
|
*/ |
309
|
|
|
public function setPassword($password) { |
310
|
|
|
$this->password = $password; |
311
|
|
|
return $this; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Return the database name |
316
|
|
|
* @return string |
317
|
|
|
*/ |
318
|
|
|
public function getDatabase() { |
319
|
|
|
return $this->databaseName; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Set the database name |
324
|
|
|
* |
325
|
|
|
* @param string $database the name of the database to set |
326
|
|
|
* |
327
|
|
|
* @return object the current instance |
328
|
|
|
*/ |
329
|
|
|
public function setDatabase($database) { |
330
|
|
|
$this->databaseName = $database; |
331
|
|
|
return $this; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Return the charset |
336
|
|
|
* |
337
|
|
|
* @return string |
338
|
|
|
*/ |
339
|
|
|
public function getCharset() { |
340
|
|
|
return $this->charset; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Set the charset |
345
|
|
|
* |
346
|
|
|
* @param string $charset the charset to set |
347
|
|
|
* |
348
|
|
|
* @return object the current instance |
349
|
|
|
*/ |
350
|
|
|
public function setCharset($charset) { |
351
|
|
|
$this->charset = $charset; |
352
|
|
|
return $this; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Return the collation |
357
|
|
|
* |
358
|
|
|
* @return string |
359
|
|
|
*/ |
360
|
|
|
public function getCollation() { |
361
|
|
|
return $this->collation; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Set the collation |
366
|
|
|
* |
367
|
|
|
* @param string $collation the collation to set |
368
|
|
|
* |
369
|
|
|
* @return object the current instance |
370
|
|
|
*/ |
371
|
|
|
public function setCollation($collation) { |
372
|
|
|
$this->collation = $collation; |
373
|
|
|
return $this; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Return the prefix |
378
|
|
|
* |
379
|
|
|
* @return string |
380
|
|
|
*/ |
381
|
|
|
public function getPrefix() { |
382
|
|
|
return $this->prefix; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Set the tables prefix |
387
|
|
|
* |
388
|
|
|
* @param string $prefix the prefix to set |
389
|
|
|
* |
390
|
|
|
* @return object the current instance |
391
|
|
|
*/ |
392
|
|
|
public function setPrefix($prefix) { |
393
|
|
|
$this->prefix = $prefix; |
394
|
|
|
return $this; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Return the database configuration |
399
|
|
|
* |
400
|
|
|
* @return array |
401
|
|
|
*/ |
402
|
|
|
public function getConfig() { |
403
|
|
|
return $this->config; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Set the database configuration |
408
|
|
|
* |
409
|
|
|
* @param array $config the configuration to set |
410
|
|
|
* |
411
|
|
|
* @return object the current instance |
412
|
|
|
*/ |
413
|
|
|
public function setConfig(array $config) { |
414
|
|
|
$this->config = $config; |
415
|
|
|
//populate the properties |
416
|
|
|
$this->populatePropertiesFromConfig(); |
417
|
|
|
|
418
|
|
|
if (!empty($this->config)) { |
419
|
|
|
//For logging |
420
|
|
|
$configInfo = $this->config; |
421
|
|
|
//Hide password from log |
422
|
|
|
$configInfo['password'] = string_hidden($this->getPassword()); |
423
|
|
|
$this->logger->info('The database configuration are listed below: ' . stringfy_vars($configInfo)); |
424
|
|
|
} |
425
|
|
|
return $this; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Get the database configuration using the configuration file |
430
|
|
|
|
431
|
|
|
* @return array the database configuration from file |
432
|
|
|
*/ |
433
|
|
|
public function getDatabaseConfigFromFile() { |
434
|
|
|
$db = array(); |
435
|
|
|
if (file_exists(CONFIG_PATH . 'database.php')) { |
436
|
|
|
//here don't use require_once because somewhere can |
437
|
|
|
//create database instance directly |
438
|
|
|
require CONFIG_PATH . 'database.php'; |
439
|
|
|
} |
440
|
|
|
return $db; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Update the properties using the current database configuration |
445
|
|
|
* |
446
|
|
|
* @return object the current instance |
447
|
|
|
*/ |
448
|
|
|
protected function populatePropertiesFromConfig() { |
449
|
|
|
foreach ($this->config as $key => $value) { |
450
|
|
|
$setter = 'set' . ucfirst($key); |
451
|
|
|
if (method_exists($this, $setter)) { |
452
|
|
|
$this->{$setter}($value); |
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
//determine the port using the hostname like localhost:34455 |
456
|
|
|
//hostname will be "localhost", and port "34455" |
457
|
|
|
$part = explode(':', $this->hostname); |
458
|
|
|
if (count($part) >= 2) { |
459
|
|
|
$this->config['hostname'] = $part[0]; |
460
|
|
|
$this->config['port'] = $part[1]; |
461
|
|
|
$this->hostname = $part[0]; |
462
|
|
|
$this->port = (int) $part[1]; |
463
|
|
|
} |
464
|
|
|
return $this; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Class desctructor this is used to disconnect to server |
469
|
|
|
* and call $this->disconnect |
470
|
|
|
*/ |
471
|
|
|
public function __destruct() { |
472
|
|
|
$this->disconnect(); |
473
|
|
|
} |
474
|
|
|
} |
475
|
|
|
|