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