-
Notifications
You must be signed in to change notification settings - Fork 1
/
MysqlAdapter.php
412 lines (351 loc) · 9.67 KB
/
MysqlAdapter.php
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
namespace Dreamcoil;
function refValues($arr){
if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+
{
$refs = array();
foreach($arr as $key => $value)
$refs[$key] = &$arr[$key];
return $refs;
}
return $arr;
}
class MysqlAdapter
{
private $host;
private $user;
private $password;
private $database;
private $port;
public $unitTest;
/**
* @var \mysqli
*/
private $interface=null;
private $table;
private $opts;
private $collectData;
/**
* Creates a Mysql Connection
*
* @param $host
* @param $user
* @param $password
* @param $database
* @param int $port
* @param array $opts
*/
public function __construct($host, $user, $password, $database, $port = 3306, array $opts = array())
{
$this->opts['collect'] = FALSE;
$this->opts['webEscape'] = TRUE;
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->database = $database;
$this->port = $port;
$this->opts = array_merge($this->opts, $opts);
$this->unitTest = FALSE;
}
/**
* @param string $option
* @throws \Exception
*/
private function existsOption($option)
{
if(!isset($this->opts[$option])) {
throw new \Exception("MysqlAdapter option '".$option."' doesn't exists");
}
}
/**
* @param string $option
* @param mixed $value
*/
public function setOption($option, $value)
{
$this->existsOption($option);
$this->opts[$option] = $value;
}
/**
* Creates the connection
*/
public function connect()
{
$this->interface = new \mysqli($this->host, $this->user, $this->password, $this->database, $this->port);
if($this->checkConnection()) {
$this->interface->set_charset("utf8");
}
}
/**
* Checks if the connection is still alive
*
* @throws \Exception
*/
public function checkConnection()
{
if(is_bool($this->getInterface()) || is_null($this->getInterface())) {
return false;
}
if (null !== $this->getInterface()->connect_error) {
throw new \Dreamcoil\Exception\MysqlAdapter($this->getInterface()->connect_errno." (".$this->getInterface()->connect_errno.")");
}
return true;
}
/**
* @TODO: Build a better destructer
*/
public function __destruct()
{
if ($this->opts['collect']) {
$this->query($this->collectData);
}
if(!is_null($this->interface)) {
$this->interface->close();
$this->interface = null;
}
}
/**
* Runs a query and returns the result
*
* @param $query
* @throws \Exception
* @return $result
*/
public function query($query)
{
global $dreamcoilMysqliQueries;
if(!isset($dreamcoilMysqliQueries)) {
$dreamcoilMysqliQueries = [];
}
$dreamcoilMysqliQueries[] = $query;
$this->checkConnection();
$result = $this->getInterface()->query($query);
return $result;
}
/**
* Runs multiple query and returns the result
*
* @param $querys
* @throws \Exception
* @return $result
*/
public function multi_query($querys)
{
$this->getInterface()->multi_query($querys);
$this->checkConnection();
return $querys;
}
/**
* @param bool $tableName
* @return string
*/
public function table($tableName = FALSE)
{
if($tableName) {
$this->table = $tableName;
}
return $this->table;
}
/**
* Fetches the data from a query and returns it in an array
*
* @param $result
* @return array
*/
public function fetch_array(\mysqli_result $result)
{
$i = 0;
$return = [];
while($data = $this->fetch($result))
{
$return[$i] = $data;
$i++;
}
return $return;
}
/**
* Fetches the data from a query
*
* @param \mysqli_result $result
* @return array
*/
public function fetch(\mysqli_result $result)
{
return $result->fetch_assoc();
}
/**
* @param array $data
* @param bool $table
* @return string
*/
public function insert(array $data, $table = FALSE)
{
if ($table !== FALSE) {
$this->table = $table;
}
if ($this->opts['collect']) {
echo NULL;
}
$i = 0;
$rows = $values = $questionMarks = $vars = [];
foreach ($data as $row => $content) {
$rows[$i] = "`" . $row . "`";
$questionMarks[$i] = "?";
if (is_null($content)) {
$values[$i] = "NULL";
$vars[$i] = null;
} else {
if($this->opts['webEscape']) {
$content = $this->webEscape($content);
}
$vars[$i] = $content;
$values[$i] = "'" . $content . "'";
}
$i++;
}
$query = "INSERT INTO `" . $this->database . "`.`" . $this->table . "` ";
$query .= "(" . implode(', ', $rows) . ") ";
$preparedQuery = $query;
$query .= "VALUES (" . implode(', ', $values) . ");";
$preparedQuery .= "VALUES (" . implode(', ', $questionMarks) . ");";
if($this->unitTest) {
return $query;
}
if (!$this->opts['collect']) {
$statement = $this->getInterface()->prepare($preparedQuery);
if($statement === false) {
throw new \Exception("Can't prepare Mysql Query: ".$preparedQuery);
}
$var_types = '';
foreach ($vars as $var) {
$var_type = gettype($var);
if($var_type == 'integer') {
$var_type = 'i';
} else if($var_type == 'double') {
$var_type = 'd';
} else if($var_type == 'string' || is_null($var)) {
$var_type = 's';
} else {
throw new \Exception("MysqlAdapter cannot bind ".var_export($var, true).":".$var_type);
}
$var_types .= $var_type;
}
$vars = array_merge([-1 => $var_types], $vars);
call_user_func_array(array($statement, "bind_param"), refValues($vars));
$statement->execute();
return $statement->id;
}
else {
$this->collectData .= "\n" . $query;
}
return NULL;
}
/**
* @param $fields
* @param $table
* @param null $where
* @return null|string
*/
public function update(array $fields, $table = FALSE, $where = NULL)
{
if ($table !== FALSE) {
$this->table = $table;
}
// There are no fields to update
if(count($fields) == 0) {
return NULL;
}
$sets = [];
foreach ($fields as $field => $value) {
if($this->opts['webEscape'] && !is_null($value)) {
$value = $this->webEscape($value);
}
if(is_null($value)) {
$sets[] = "`".$field."` = NULL";
} else {
$sets[] = "`".$field."` = '" . $value . "'";
}
}
$query = "UPDATE `" . $this->database . "`.`" . $this->table . "` ";
$query .= "SET ".implode(", ", $sets);
if(!is_null($where)) {
$query .= " WHERE ".$where;
}
if($this->unitTest) {
return $query;
}
if (!$this->opts['collect']) {
$this->query($query);
}
else {
$this->collectData .= "\n" . $query;
}
return NULL;
}
/**
* Get stats about the Mysql Server
*
* @return string
* @throws \Exception
*/
public function stat()
{
if($this->checkConnection())
{
return $this->getInterface()->stat();
}
else throw new \Dreamcoil\Exception\MysqlAdapter("Mysql Connection failed");
}
/**
* Escapes an data for a query
*
* @param string $data
* @param bool $reverse
* @return string
*/
public function webEscape($data, $reverse = false)
{
$data = str_replace($this->webEscapeCharacters($reverse)[0], $this->webEscapeCharacters($reverse)[1], $data);
return $data;
}
/**
* @param bool $reverse
* @return array
*/
private function webEscapeCharacters($reverse = false) {
$array1 = ["<", ">", "'" , "Ä" , "ä" , "Ö" , "ö" , "Ü" , "ü" ];
$array2 = ["<", ">", "'", "Ä", "ä", "Ö", "ö", "Ü", "ü"];
if($reverse) {
return [$array2, $array1];
}
return [$array1, $array2];
}
public static function getExecutedQuerys()
{
global $dreamcoilMysqliQueries;
if(isset($dreamcoilMysqliQueries)) {
return $dreamcoilMysqliQueries;
}
return [];
}
/**
* @return array
*/
public function getCredentials()
{
$cred = [
"host" => $this->host,
"user" => $this->user,
"password" => $this->password,
"database" => $this->database
];
return $cred;
}
/**
* @return \mysqli
*/
public function getInterface()
{
return $this->interface;
}
}