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