1
|
|
|
<?php /** MicroDataBaseConnection */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Db; |
4
|
|
|
|
5
|
|
|
use Micro\Base\Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Connection class file. |
9
|
|
|
* |
10
|
|
|
* @author Oleg Lunegov <[email protected]> |
11
|
|
|
* @link https://github.com/lugnsk/micro |
12
|
|
|
* @copyright Copyright © 2013 Oleg Lunegov |
13
|
|
|
* @license /LICENSE |
14
|
|
|
* @package Micro |
15
|
|
|
* @subpackage Db |
16
|
|
|
* @version 1.0 |
17
|
|
|
* @since 1.0 |
18
|
|
|
*/ |
19
|
|
|
class DbConnection extends Connection |
20
|
|
|
{ |
21
|
|
|
/** @var \PDO|null $conn Connection to DB */ |
22
|
|
|
protected $conn; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Construct for this class |
27
|
|
|
* |
28
|
|
|
* @access public |
29
|
|
|
* |
30
|
|
|
* @param array $config configuration array |
31
|
|
|
* |
32
|
|
|
* @result void |
33
|
|
|
* @throws Exception |
34
|
|
|
*/ |
35
|
|
|
public function __construct(array $config = []) |
36
|
|
|
{ |
37
|
|
|
parent::__construct($config); |
38
|
|
|
|
39
|
|
|
try { |
40
|
|
|
if (empty($config['options'])) { |
41
|
|
|
$config['options'] = null; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->conn = new \PDO( |
45
|
|
|
$config['connectionString'], |
46
|
|
|
$config['username'], |
47
|
|
|
$config['password'], |
48
|
|
|
$config['options'] |
49
|
|
|
); |
50
|
|
|
} catch (\PDOException $e) { |
51
|
|
|
if (!array_key_exists('ignoreFail', $config) || !$config['ignoreFail']) { |
52
|
|
|
throw new Exception('Connect to DB failed: ' . $e->getMessage()); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Destructor for this class |
59
|
|
|
* |
60
|
|
|
* @access public |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function __destruct() |
64
|
|
|
{ |
65
|
|
|
$this->conn = null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
71
|
|
|
public function rawQuery($query = '', array $params = [], $fetchType = \PDO::FETCH_ASSOC, $fetchClass = 'Model') |
72
|
|
|
{ |
73
|
|
|
$sth = $this->conn->prepare($query); |
74
|
|
|
|
75
|
|
|
if ($fetchType === \PDO::FETCH_CLASS) { |
76
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
77
|
|
|
$sth->setFetchMode($fetchType, ucfirst($fetchClass), ['container' => $this->container, 'new' => false]); |
78
|
|
|
} else { |
79
|
|
|
$sth->setFetchMode($fetchType); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
foreach ($params AS $name => $value) { |
83
|
|
|
$sth->bindValue($name, $value); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$sth->execute(); |
87
|
|
|
|
88
|
|
|
return $sth->fetchAll(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
|
|
public function listDatabases() |
95
|
|
|
{ |
96
|
|
|
$sth = $this->conn->query('SHOW DATABASES;'); |
97
|
|
|
|
98
|
|
|
$result = []; |
99
|
|
|
foreach ($sth->fetchAll() AS $row) { |
100
|
|
|
$result[] = $row[0]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $result; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritdoc |
108
|
|
|
*/ |
109
|
|
|
public function infoDatabase($dbName) |
110
|
|
|
{ |
111
|
|
|
$sth = $this->conn->query("SHOW TABLE STATUS FROM {$dbName};"); |
112
|
|
|
|
113
|
|
|
$result = []; |
114
|
|
|
foreach ($sth->fetchAll() AS $row) { |
115
|
|
|
$result[] = [ |
116
|
|
|
'name' => $row['Name'], |
117
|
|
|
'engine' => $row['Engine'], |
118
|
|
|
'rows' => $row['Rows'], |
119
|
|
|
'length' => $row['Avg_row_length'], |
120
|
|
|
'increment' => $row['Auto_increment'], |
121
|
|
|
'collation' => $row['Collation'] |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $result; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @inheritdoc |
130
|
|
|
*/ |
131
|
|
|
public function tableExists($table) |
132
|
|
|
{ |
133
|
|
|
return in_array($table, $this->listTables(), false); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @inheritdoc |
138
|
|
|
*/ |
139
|
|
|
public function listTables() |
140
|
|
|
{ |
141
|
|
|
return $this->conn->query('SHOW TABLES;')->fetchAll(\PDO::FETCH_COLUMN, 0); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @inheritdoc |
146
|
|
|
*/ |
147
|
|
|
public function createTable($name, array $elements = [], $params = '') |
148
|
|
|
{ |
149
|
|
|
return $this->conn->exec( |
150
|
|
|
"CREATE TABLE IF NOT EXISTS `{$name}` (`".implode('`,`', $elements)."`) {$params};" |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritdoc |
156
|
|
|
*/ |
157
|
|
|
public function clearTable($name) |
158
|
|
|
{ |
159
|
|
|
return $this->conn->exec("TRUNCATE {$name};"); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritdoc |
164
|
|
|
*/ |
165
|
|
|
public function removeTable($name) |
166
|
|
|
{ |
167
|
|
|
return $this->conn->exec("DROP TABLE {$name};"); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @inheritdoc |
172
|
|
|
*/ |
173
|
|
|
public function fieldExists($field, $table) |
174
|
|
|
{ |
175
|
|
|
foreach ($this->listFields($table) AS $tbl) { |
176
|
|
|
if ($tbl['field'] === $field) { |
177
|
|
|
return true; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return false; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @inheritdoc |
186
|
|
|
*/ |
187
|
|
|
public function listFields($table) |
188
|
|
|
{ |
189
|
|
|
$sth = $this->conn->query("SHOW COLUMNS FROM {$table};"); |
190
|
|
|
|
191
|
|
|
$result = []; |
192
|
|
|
foreach ($sth->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
193
|
|
|
$result[] = [ |
194
|
|
|
'field' => $row['Field'], |
195
|
|
|
'type' => $row['Type'], |
196
|
|
|
'null' => $row['Null'], |
197
|
|
|
'key' => $row['Key'], |
198
|
|
|
'default' => $row['Default'], |
199
|
|
|
'extra' => $row['Extra'] |
200
|
|
|
]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $result; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @inheritdoc |
208
|
|
|
*/ |
209
|
|
|
public function fieldInfo($field, $table) |
210
|
|
|
{ |
211
|
|
|
$sth = $this->conn->query("SELECT {$field} FROM {$table} LIMIT 1;"); |
212
|
|
|
|
213
|
|
|
return $sth->getColumnMeta(0); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @inheritdoc |
218
|
|
|
*/ |
219
|
|
|
public function switchDatabase($dbName) |
220
|
|
|
{ |
221
|
|
|
if ($this->conn->exec("USE {$dbName};") !== false) { |
222
|
|
|
return true; |
223
|
|
|
} else { |
224
|
|
|
return false; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @inheritdoc |
230
|
|
|
*/ |
231
|
|
|
public function insert($table, array $line = [], $multi = false) |
232
|
|
|
{ |
233
|
|
|
$fields = '`' . implode('`, `', array_keys($multi ? $line[0] : $line)) . '`'; |
234
|
|
|
$values = ':' . implode(', :', array_keys($multi ? $line[0] : $line)); |
235
|
|
|
|
236
|
|
|
$id = null; |
|
|
|
|
237
|
|
|
$dbh = null; |
238
|
|
|
if ($multi) { |
239
|
|
|
$this->conn->beginTransaction(); |
240
|
|
|
foreach ($line AS $l) { |
241
|
|
|
$dbh = $this->conn->prepare( |
242
|
|
|
"INSERT INTO {$table} ({$fields}) VALUES ({$values});" |
243
|
|
|
)->execute($l); |
244
|
|
|
} |
245
|
|
|
$id = $dbh ? $this->conn->lastInsertId() : false; |
246
|
|
|
$this->conn->commit(); |
247
|
|
|
} else { |
248
|
|
|
$dbh = $this->conn->prepare( |
249
|
|
|
"INSERT INTO {$table} ({$fields}) VALUES ({$values});" |
250
|
|
|
)->execute($line); |
251
|
|
|
$id = $dbh ? $this->conn->lastInsertId() : false; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return $id ?: false; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @inheritdoc |
259
|
|
|
*/ |
260
|
|
|
public function update($table, array $elements = [], $conditions = '') |
261
|
|
|
{ |
262
|
|
|
$keys = array_keys($elements); |
263
|
|
|
if (0 === count($keys)) { |
264
|
|
|
return false; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
$valStr = []; |
268
|
|
|
foreach ($keys as $key) { |
269
|
|
|
$valStr[] = '`' . $key . '` = :' . $key; |
270
|
|
|
} |
271
|
|
|
$valStr = implode(',', $valStr); |
272
|
|
|
|
273
|
|
|
if ($conditions) { |
274
|
|
|
$conditions = 'WHERE ' . $conditions; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return $this->conn->prepare( |
278
|
|
|
"UPDATE {$table} SET {$valStr} {$conditions};" |
279
|
|
|
)->execute($elements); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @inheritdoc |
284
|
|
|
*/ |
285
|
|
|
public function delete($table, $conditions, array $ph = []) |
286
|
|
|
{ |
287
|
|
|
return $this->conn->prepare( |
288
|
|
|
"DELETE FROM {$table} WHERE {$conditions};" |
289
|
|
|
)->execute($ph); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @inheritdoc |
294
|
|
|
*/ |
295
|
|
|
public function exists($table, array $params = []) |
296
|
|
|
{ |
297
|
|
|
$keys = []; |
298
|
|
|
foreach ($params AS $key => $val) { |
299
|
|
|
$keys[] = '`' . $table . '`.`' . $key . '`="' . $val . '"'; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$sth = $this->conn->prepare( |
303
|
|
|
'SELECT * FROM `' . $table . '` WHERE ' . implode(' AND ', $keys) . ' LIMIT 1;' |
304
|
|
|
); |
305
|
|
|
$sth->execute(); |
306
|
|
|
|
307
|
|
|
return (bool)$sth->rowCount(); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @inheritdoc |
312
|
|
|
*/ |
313
|
|
|
public function count($subQuery = '', $table = '') |
314
|
|
|
{ |
315
|
|
|
if ($subQuery) { |
316
|
|
|
$sth = $this->conn->prepare("SELECT COUNT(*) FROM ({$subQuery}) AS m;"); |
317
|
|
|
} elseif ($table) { |
318
|
|
|
$sth = $this->conn->prepare("SELECT COUNT(*) FROM {$table} AS m;"); |
319
|
|
|
} else { |
320
|
|
|
return false; |
321
|
|
|
} |
322
|
|
|
if ($sth->execute()) { |
323
|
|
|
return $sth->fetchColumn(); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
return false; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.