|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\db; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\cache\database\DbCache; |
|
6
|
|
|
use Ubiquity\exceptions\CacheException; |
|
7
|
|
|
use Ubiquity\log\Logger; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* PDO Database class |
|
11
|
|
|
* |
|
12
|
|
|
* @author jc |
|
13
|
|
|
* @version 1.0.0.3 |
|
14
|
|
|
* @package db |
|
15
|
|
|
* |
|
16
|
|
|
*/ |
|
17
|
|
|
class Database { |
|
18
|
|
|
private $dbType; |
|
19
|
|
|
private $serverName; |
|
20
|
|
|
private $port; |
|
21
|
|
|
private $dbName; |
|
22
|
|
|
private $user; |
|
23
|
|
|
private $password; |
|
24
|
|
|
private $pdoObject; |
|
25
|
|
|
private $statements = [ ]; |
|
26
|
|
|
private $cache; |
|
27
|
|
|
private $options; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Constructor |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $dbName |
|
33
|
|
|
* @param string $serverName |
|
34
|
|
|
* @param string $port |
|
35
|
|
|
* @param string $user |
|
36
|
|
|
* @param string $password |
|
37
|
|
|
* @param array $options |
|
38
|
|
|
* @param boolean|string $cache |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($dbType, $dbName, $serverName = "localhost", $port = "3306", $user = "root", $password = "", $options = [], $cache = false) { |
|
41
|
|
|
$this->dbType = $dbType; |
|
42
|
|
|
$this->dbName = $dbName; |
|
43
|
|
|
$this->serverName = $serverName; |
|
44
|
|
|
$this->port = $port; |
|
45
|
|
|
$this->user = $user; |
|
46
|
|
|
$this->password = $password; |
|
47
|
|
|
if (isset ( $options ["quote"] )) |
|
48
|
|
|
SqlUtils::$quote = $options ["quote"]; |
|
49
|
|
|
$this->options = $options; |
|
50
|
|
|
if ($cache !== false) { |
|
51
|
|
|
if (\is_callable ( $cache )) { |
|
52
|
|
|
$this->cache = $cache (); |
|
53
|
|
|
} else { |
|
54
|
|
|
if (\class_exists ( $cache )) { |
|
55
|
|
|
$this->cache = new $cache (); |
|
56
|
|
|
} else { |
|
57
|
|
|
throw new CacheException ( $cache . " is not a valid value for database cache" ); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Creates the PDO instance and realize a safe connection |
|
65
|
|
|
* @return boolean true if connection is established |
|
66
|
|
|
*/ |
|
67
|
|
|
public function connect() { |
|
68
|
|
|
try{ |
|
69
|
|
|
$this->_connect(); |
|
70
|
|
|
return true; |
|
71
|
|
|
}catch (\PDOException $e){ |
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function _connect(){ |
|
77
|
|
|
$this->options[\PDO::ATTR_ERRMODE]=\PDO::ERRMODE_EXCEPTION; |
|
78
|
|
|
$this->pdoObject = new \PDO ( $this->getDSN(), $this->user, $this->password, $this->options ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getDSN(){ |
|
82
|
|
|
return $this->dbType . ':dbname=' . $this->dbName . ';host=' . $this->serverName . ';charset=UTF8;port=' . $this->port; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Executes an SQL statement, returning a result set as a PDOStatement object |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $sql |
|
89
|
|
|
* @return \PDOStatement |
|
90
|
|
|
*/ |
|
91
|
|
|
public function query($sql) { |
|
92
|
|
|
return $this->pdoObject->query ( $sql ); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $tableName |
|
98
|
|
|
* @param string $condition |
|
99
|
|
|
* @param array|string $fields |
|
100
|
|
|
* @param array $parameters |
|
101
|
|
|
* @param boolean|null $useCache |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
public function prepareAndExecute($tableName, $condition, $fields, $parameters=null,$useCache = NULL) { |
|
105
|
|
|
$cache = (DbCache::$active && $useCache !== false) || (! DbCache::$active && $useCache === true); |
|
106
|
|
|
$result = false; |
|
107
|
|
|
if ($cache) { |
|
108
|
|
|
$result = $this->cache->fetch ( $tableName, $condition ); |
|
109
|
|
|
} |
|
110
|
|
|
if ($result === false) { |
|
111
|
|
|
$fields = SqlUtils::getFieldList ( $fields, $tableName ); |
|
|
|
|
|
|
112
|
|
|
$result=$this->prepareAndFetchAll("SELECT {$fields} FROM " . $tableName . $condition,$parameters); |
|
113
|
|
|
if ($cache) { |
|
114
|
|
|
$this->cache->store ( $tableName, $condition, $result ); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
return $result; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function prepareAndFetchAll($sql,$parameters=null){ |
|
121
|
|
|
$result=false; |
|
122
|
|
|
$statement=$this->getStatement($sql); |
|
123
|
|
|
if($statement->execute ($parameters)){ |
|
124
|
|
|
Logger::info("Database", $sql,"prepareAndFetchAll"); |
|
125
|
|
|
$result = $statement->fetchAll (); |
|
126
|
|
|
} |
|
127
|
|
|
$statement->closeCursor (); |
|
128
|
|
|
return $result; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $sql |
|
134
|
|
|
* @return \PDOStatement |
|
135
|
|
|
*/ |
|
136
|
|
|
private function getStatement($sql) { |
|
137
|
|
|
if (! isset ( $this->statements [$sql] )) { |
|
138
|
|
|
$this->statements [$sql] = $this->pdoObject->prepare ( $sql ); |
|
139
|
|
|
$this->statements [$sql]->setFetchMode ( \PDO::FETCH_ASSOC ); |
|
140
|
|
|
} |
|
141
|
|
|
return $this->statements [$sql]; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE) |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $sql |
|
148
|
|
|
* @return int the number of rows that were modified or deleted by the SQL statement you issued |
|
149
|
|
|
*/ |
|
150
|
|
|
public function execute($sql) { |
|
151
|
|
|
return $this->pdoObject->exec ( $sql ); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function getServerName() { |
|
155
|
|
|
return $this->serverName; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function setServerName($serverName) { |
|
159
|
|
|
$this->serverName = $serverName; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Prepares a statement for execution and returns a statement object |
|
164
|
|
|
* |
|
165
|
|
|
* @param String $sql |
|
166
|
|
|
* @return \PDOStatement |
|
167
|
|
|
*/ |
|
168
|
|
|
public function prepareStatement($sql) { |
|
169
|
|
|
return $this->pdoObject->prepare ( $sql ); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Sets $value to $parameter |
|
174
|
|
|
* |
|
175
|
|
|
* @param \PDOStatement $statement |
|
176
|
|
|
* @param String $parameter |
|
177
|
|
|
* @param mixed $value |
|
178
|
|
|
* @return boolean |
|
179
|
|
|
*/ |
|
180
|
|
|
public function bindValueFromStatement(\PDOStatement $statement, $parameter, $value) { |
|
181
|
|
|
return $statement->bindValue ( ":" . $parameter, $value ); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Returns the last insert id |
|
186
|
|
|
* |
|
187
|
|
|
* @return integer |
|
188
|
|
|
*/ |
|
189
|
|
|
public function lastInserId() { |
|
190
|
|
|
return $this->pdoObject->lastInsertId (); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function getTablesName() { |
|
194
|
|
|
$sql = 'SHOW TABLES'; |
|
195
|
|
|
$query = $this->pdoObject->query ( $sql ); |
|
196
|
|
|
return $query->fetchAll ( \PDO::FETCH_COLUMN ); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Returns the number of records in $tableName that respects the condition passed as a parameter |
|
201
|
|
|
* |
|
202
|
|
|
* @param string $tableName |
|
203
|
|
|
* @param string $condition |
|
204
|
|
|
* Partie suivant le WHERE d'une instruction SQL |
|
205
|
|
|
*/ |
|
206
|
|
|
public function count($tableName, $condition = '') { |
|
207
|
|
|
if ($condition != '') |
|
208
|
|
|
$condition = " WHERE " . $condition; |
|
209
|
|
|
return $this->query ( "SELECT COUNT(*) FROM " . $tableName . $condition )->fetchColumn (); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public function queryColumn($query){ |
|
213
|
|
|
return $this->query ( $query )->fetchColumn (); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function fetchAll($query){ |
|
217
|
|
|
return $this->query ( $query )->fetchAll ( \PDO::FETCH_COLUMN ); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function isConnected() { |
|
221
|
|
|
return ($this->pdoObject !== null && $this->pdoObject instanceof \PDO && $this->ping()); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function setDbType($dbType) { |
|
225
|
|
|
$this->dbType = $dbType; |
|
226
|
|
|
return $this; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
public function ping() { |
|
230
|
|
|
return (1 === intval($this->pdoObject->query('SELECT 1')->fetchColumn(0))); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public function getPort() { |
|
234
|
|
|
return $this->port; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
public function getDbName() { |
|
238
|
|
|
return $this->dbName; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
public function getUser() { |
|
242
|
|
|
return $this->user; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
public function getPdoObject() { |
|
246
|
|
|
return $this->pdoObject; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
public static function getAvailableDrivers(){ |
|
250
|
|
|
return \PDO::getAvailableDrivers(); |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: