-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleMySQLi.php
More file actions
384 lines (324 loc) · 10.8 KB
/
simpleMySQLi.php
File metadata and controls
384 lines (324 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
/**
* @author andy.bezbozhny <andy.bezbozhny@gmail.com>
*/
class simpleMySQLi
{
/**
* @var string $str строка запроса
* @var boolean $log логировать запросы?
*/
public $str;
public $log = false;
/**
* @var mixed $db конфиг подключения к БД
* @var string $err сообщение об ошибке
* @var int $id последнее добавленное значение auto_increment
* @var string $path каталог для логов
* @var int $rows кол-во строк при запросах select/update/delete
* @var resource $res результат выполнения запроса
*/
private $db;
private $err;
private $id;
private $path;
private $rows;
private $res;
private $sql;
/**
* $db => [
* 'host' => default_host
* 'username' => default_user
* 'passwd' => default_pw
* 'dbname' => default_dbase
* 'port' => default_port
* 'socket' => default_socket
* ]
*/
public function __construct($db, $logs = null)
{
$default = [
'host' => null,
'username' => null,
'passwd' => null,
'dbname' => null,
'port' => null,
'socket' => null
];
$this->db = array_merge($default, $db);
$this->path = $logs;
return $this->_connect();
}
public function __destruct()
{
if ($this->sql) {
$this->sql->close();
}
}
public function __get($key)
{
return in_array($key, ['err', 'id', 'path', 'rows']) ? $this->$key : null;
}
public function __set($key, $value)
{
if ($key == 'err' and empty($value)) $this->err = null;
}
public function __debugInfo()
{
return [
'str' => $this->str,
'err' => $this->err,
'id' => $this->id,
'rows' => $this->rows
];
}
/**
* выборка всех строк после select
* @param boolean $num флаг гладкого или ассоциированного массива
* @return array массив данных
*/
public function all($num = false)
{
$data = $this->res->fetch_all($num ? MYSQLI_NUM : MYSQLI_ASSOC);
$data = array_map(['self', '_strip'], $data);
return $data;
}
/**
* результат запроса - в ассоциированный массив
* @return array массив данных
*/
public function assoc()
{
$data = $this->res->fetch_assoc();
return self::_strip($data);
}
/**
* экранирование строки
* @param string $str эранируемая строка
* @return string экранированная строка
*/
public function escape($str)
{
return $this->sql->real_escape_string($str);
}
/**
* выполнение запроса
* @param boolean $log логировать данный запрос?
* @return boolean результат выполнения запроса
*/
public function execute($log = false)
{
$this->err = null;
$this->rows = $this->id = 0;
$this->str = is_array($this->str) ? implode(' ', $this->str) : $this->str;
$this->str = trim($this->str);
$this->res = $this->sql->query($this->str);
if ($this->sql->errno) {
$this->err = $this->sql->errno . ': ' . $this->sql->error;
} else if (($pos = mb_stripos($this->str, 'DELETE')) === 0) {
$this->rows = $this->rows();
} else if (($pos = mb_stripos($this->str, 'INSERT')) === 0) {
$this->id = $this->last();
$this->rows = $this->rows();
} else if (($pos = mb_stripos($this->str, 'REPLACE')) === 0) {
$this->rows = $this->rows();
} else if (($pos = mb_stripos($this->str, 'SELECT')) === 0) {
$this->rows = $this->res->num_rows;
} else if (($pos = mb_stripos($this->str, 'UPDATE')) === 0) {
$this->id = $this->last();
$this->rows = $this->rows();
}
if ($this->log or $log or $this->err) {
$this->_write2log();
}
return empty($this->err);
}
/**
* результат запроса в гладкий массив
* @return array массив данных
*/
public function fetch()
{
$data = $this->res->fetch_array(MYSQLI_NUM);
return self::_strip($data);
}
/**
* высвобождение результата запроса
*/
public function free()
{
$this->res->close();
}
/**
* кол-во изменённных строк (после DELETE/INSERT/REPLACE/UPDATE)
* @return int кол-во строк
*/
public function rows()
{
return $this->sql->affected_rows;
}
/**
* добавление в таблицу
* @param string $table название таблицы
* @param array $data массив данных, где ключ - названия поля
* @return boolean результат выполнения операции
*/
public function insert($table = '', $data = [])
{
if (!$data or !$table) return false;
$this->str = [];
$this->str[] = 'INSERT INTO ' . $table;
$this->str[] = '(' . implode(', ', array_keys($data)) . ')';
$this->str[] = 'VALUES (' . implode(', ', $data) . ')';
return $this->execute() ? ($this->id ? $this->id : true) : false;
}
/**
* последние значение auto_increment
* @return int значение insert_id
*/
public function last()
{
return $this->sql->insert_id;
}
/**
* проверка соединения
*/
public function ping()
{
return ($this->sql and $this->sql->ping()) ? true : $this->_connect();
}
/**
* добавление с заменой в таблицу
* @param string $table название таблицы
* @param array $data массив данных, где ключ - названия поля
* @return boolean результат выполнения операции
*/
public function replace($table = '', $data = [])
{
if (!$data or !$table) return false;
$this->str = [];
$this->str[] = 'REPLACE INTO ' . $table;
$this->str[] = '(' . implode(', ', array_keys($data)) . ')';
$this->str[] = 'VALUES (' . implode(', ', $data) . ')';
return $this->execute() ? $this->rows : false;
}
/**
* возврат первого элемента массива результата
* @return mixed результат
*/
public function single()
{
$data = $this->fetch();
return array_shift($data);
}
/**
* обновление строки
* @param string $table название таблицы
* @param array $data массив данных, где ключ - названия поля
* @param array $where массив условий
* @param string $sep оператор условия
* @return int|boolean кол-во изменённых строк либо false в случае ошибки
*/
public function update($table = '', $data = [], $where = [], $sep = 'AND')
{
if (!$data or !$table) return false;
$this->str = 'UPDATE ' . $table . ' SET ' . implode(', ', self::_assoc2plain($data));
$this->str .= ($where) ? (' WHERE ' . implode(' ' . $sep . ' ', $where)) : '';
return $this->execute() ? $this->rows : false;
}
/**
* заключение строки в двойные кавычки
* @param string $str исходная строка
* @return string результат
*/
public function varchar($str = '')
{
return '"' . $this->escape($str) . '"';
}
/**
* подключение к БД
*/
private function _connect()
{
$this->str = null;
$this->sql = new mysqli(
$this->db['host'],
$this->db['username'],
$this->db['passwd'],
$this->db['dbname'],
$this->db['port'],
$this->db['socket']
);
if ($this->sql->connect_error) {
$this->err = $this->sql->connect_errno . ': ' . $this->sql->connect_error;
$this->_write2log();
return false;
} else {
$this->sql->set_charset('utf8');
return true;
}
}
/**
* запись в лог
*/
private function _write2log()
{
if ($this->path and file_exists($this->path)) {
$log = [PHP_EOL];
$log[] = date('H:i:s');
if ($this->str) $log[] = $this->str;
if ($this->err) $log[] = $this->err;
error_log(implode(PHP_EOL, $log), 3, $logfile = $this->path . '/mysql.' . date('Y.m.d') . '.log');
chmod($logfile, 0644);
}
}
/**
* преобразование ассоциированного массива в гладкий
*/
static function _assoc2plain($u = [])
{
return array_map(function($key, $val) { return $key . ' = ' . $val; }, array_keys($u), $u);
}
static function _and($u = [])
{
return implode(' AND ', $u);
}
static function _int($r = [])
{
$int = ['id'];
foreach ($r as $key => $val) {
$r[$key] = in_array($key, $int) ? (int)$val : $val;
}
return $r;
}
/**
* строковое представление даты/времени
* @param boolean $full флаг полноты
* @return string результат
*/
static function _now($full = true)
{
return $full ? date('Y-m-d H:i:s') : date('Y-m-d');
}
static function _or($u = [])
{
return implode(' OR ', $u);
}
/**
* очистка массива/объекта от слешей
* @param mixed $obj исходный массив/объект
* @return mixed результат
*/
static function _strip($data)
{
if (is_array($data)) {
return array_map(function($val) { return is_string($val) ? stripcslashes($val) : $val; }, $data);
} else if (is_object($data)) {
return (object)array_map(function($val) { return is_string($val) ? stripcslashes($val) : $val; }, (array)$data);
} else if (is_string($data)) {
return stripcslashes($data);
} else {
return $data;
}
}
}