1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleORM\Adapter; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Description of QueryBuilder |
7
|
|
|
* |
8
|
|
|
* @author Dmitriy |
9
|
|
|
*/ |
10
|
|
|
class CodeigniterQueryBuilder implements \SimpleORM\QueryBuilderInterface |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
protected $adapter; |
14
|
|
|
|
15
|
|
|
protected $database; |
16
|
|
|
|
17
|
|
|
protected $TableName; |
18
|
|
|
|
19
|
|
|
protected $bind_marker = '?'; |
20
|
|
|
|
21
|
|
|
function __construct(\CI_DB_mysqli_driver $db) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
$this->adapter = $db; |
25
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* "Smart" Escape String |
30
|
|
|
* |
31
|
|
|
* Escapes data based on type |
32
|
|
|
* Sets boolean and null types |
33
|
|
|
* |
34
|
|
|
* @access public |
35
|
|
|
* @param string |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
|
|
protected function escape($str) |
39
|
|
|
{ |
40
|
|
|
if (is_string($str)) { |
41
|
|
|
$str = "'" . $this->escape_str($str) . "'"; |
42
|
|
|
} elseif (is_bool($str)) { |
43
|
|
|
$str = ($str === FALSE) ? 0 : 1; |
44
|
|
|
} elseif (is_null($str)) { |
45
|
|
|
$str = 'NULL'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $str; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function update($table,array $data,$where = []){ |
52
|
|
|
return $this->adapter->update($table,$data,$where); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function insert($table,array $data) |
56
|
|
|
{ |
57
|
|
|
return $this->adapter->insert($table,$data); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function insert_id() |
61
|
|
|
{ |
62
|
|
|
return $this->adapter->insert_id(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function delete($table,$where = []){ |
66
|
|
|
return $this->adapter->delete($table,$where); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function escape_str($str, $like = FALSE) |
70
|
|
|
{ |
71
|
|
|
|
72
|
|
|
if(!$like){ |
73
|
|
|
return $this->adapter->escape_str($str); |
74
|
|
|
} |
75
|
|
|
else{ |
76
|
|
|
return $this->adapter->escape_like_str($str); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
// if (is_array($str)) { |
|
|
|
|
81
|
|
|
// foreach ($str as $key => $val) { |
82
|
|
|
// $str[$key] = $this->escape_str($val, $like); |
83
|
|
|
// } |
84
|
|
|
// |
85
|
|
|
// return $str; |
86
|
|
|
// } |
87
|
|
|
// |
88
|
|
|
// if (function_exists('mysql_real_escape_string') AND is_resource($this->adapter->dbserver->rsLink)) { |
89
|
|
|
// $str = mysql_real_escape_string($str, $this->adapter->dbserver->rsLink); |
90
|
|
|
// } elseif (function_exists('mysql_escape_string')) { |
91
|
|
|
// $str = mysql_escape_string($str); |
92
|
|
|
// } else { |
93
|
|
|
// $str = addslashes($str); |
94
|
|
|
// } |
95
|
|
|
// |
96
|
|
|
// // escape LIKE condition wildcards |
97
|
|
|
// if ($like === TRUE) { |
98
|
|
|
// $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str); |
99
|
|
|
// } |
100
|
|
|
// |
101
|
|
|
// return $str; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Проверка корректности поля |
107
|
|
|
*/ |
108
|
|
|
protected function fieldCheck($field) |
109
|
|
|
{ |
110
|
|
|
if (empty($field)) { |
111
|
|
|
throw new HttpException('You cannot have an empty field name.'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (strpos($field, '.') === false) { |
115
|
|
|
return $this->TableName . '.' . $field; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $field; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* |
123
|
|
|
* @param type $param |
|
|
|
|
124
|
|
|
*/ |
125
|
|
|
public function getResultQuery($table,\SimpleORM\ISpecificationCriteria $Criteria ) { |
126
|
|
|
|
127
|
|
|
$this->setTable($table); |
128
|
|
|
|
129
|
|
|
$res = $this->buildQuery( |
130
|
|
|
$Criteria->getWhere(), |
131
|
|
|
$Criteria->getLimit(), |
132
|
|
|
$Criteria->getOfset(), |
133
|
|
|
$Criteria->getJoins(), |
134
|
|
|
$Criteria->getOrder(), |
135
|
|
|
$Criteria->getManualJoins(), |
136
|
|
|
$Criteria->getGroup(), |
137
|
|
|
$Criteria->getManualWheres(), |
138
|
|
|
$Criteria->getWhereType(), |
139
|
|
|
$Criteria->getManualSelect() |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
return $res; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
protected function setTable($table){ |
146
|
|
|
if(preg_match('~(.*?)\.(.*?)$~is',$table,$m)){ |
147
|
|
|
$this->database = $m[1]; |
148
|
|
|
$this->TableName = $m[2]; |
149
|
|
|
} |
150
|
|
|
else{ |
151
|
|
|
$this->TableName = $table; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Создает селект не только для основной таблицы но и для приджойненых таблиц |
158
|
|
|
* @param type $joins |
159
|
|
|
* @return type |
160
|
|
|
*/ |
161
|
|
|
protected function createSelect(array $joins,$manualSelect) |
162
|
|
|
{ |
163
|
|
|
$s = !empty($manualSelect) ? $manualSelect :"`" . $this->TableName . '`.*'; |
164
|
|
|
|
165
|
|
|
foreach ($joins as $table => $join) { |
166
|
|
|
$table = isset($join['alias']) ? "`{$join['alias']}`": $table; |
167
|
|
|
$s .= ", $table.*"; |
168
|
|
|
} |
169
|
|
|
return $s; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Получение записей по условию |
175
|
|
|
* @param type $where |
176
|
|
|
* @param type $limit |
177
|
|
|
* @param type $offset |
178
|
|
|
* @param type $joins |
179
|
|
|
* @param type $order |
180
|
|
|
* @param type $manualJoins |
181
|
|
|
* @param type $group |
182
|
|
|
* @param type $manualWheres |
183
|
|
|
* @param type $whereType |
184
|
|
|
* @return boolean |
185
|
|
|
* @throws \PDOException |
186
|
|
|
*/ |
187
|
|
|
protected function buildQuery($where = array(), $limit = 25, $offset = 0, $joins = array(), $order = array(), $manualJoins = array(), $group = null, $manualWheres = array(), $whereType = 'AND', $manualSelect = '') |
188
|
|
|
{ |
189
|
|
|
$table = !empty($this->database)? "`{$this->database}`.".$this->TableName : $this->TableName; |
190
|
|
|
$query = 'SELECT ' . $this->createSelect($joins, $manualSelect) . " FROM `".$table."`"; |
191
|
|
|
//$countQuery = "SELECT COUNT(*) AS cnt FROM `{$this->database}`.".$this->getTableName(); |
|
|
|
|
192
|
|
|
|
193
|
|
|
$wheres = array(); |
194
|
|
|
$params = array(); |
195
|
|
|
foreach ($where as $key => $value) { |
196
|
|
|
$key = $this->fieldCheck($key); |
197
|
|
|
|
198
|
|
|
if (!is_array($value)) { |
199
|
|
|
$params[] = $value; |
200
|
|
|
$wheres[] = $key . ' = ?'; |
201
|
|
|
} else { |
202
|
|
|
if (isset($value['operator'])) { |
203
|
|
|
if (is_array($value['value'])) { |
204
|
|
|
if ($value['operator'] == 'between') { |
205
|
|
|
$params[] = $value['value'][0]; |
206
|
|
|
$params[] = $value['value'][1]; |
207
|
|
|
$wheres[] = $key . ' BETWEEN ? AND ?'; |
208
|
|
|
} elseif ($value['operator'] == 'IN') { |
209
|
|
|
$in = array(); |
210
|
|
|
|
211
|
|
|
foreach ($value['value'] as $item) { |
212
|
|
|
$params[] = $item; |
213
|
|
|
$in[] = '?'; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$wheres[] = $key . ' IN (' . implode(', ', $in) . ') '; |
217
|
|
|
} else { |
218
|
|
|
$ors = array(); |
219
|
|
|
foreach ($value['value'] as $item) { |
220
|
|
|
if ($item == 'null') { |
221
|
|
View Code Duplication |
switch ($value['operator']) { |
|
|
|
|
222
|
|
|
case '!=': |
223
|
|
|
$ors[] = $key . ' IS NOT NULL'; |
224
|
|
|
break; |
225
|
|
|
|
226
|
|
|
case '==': |
227
|
|
|
default: |
228
|
|
|
$ors[] = $key . ' IS NULL'; |
229
|
|
|
break; |
230
|
|
|
} |
231
|
|
|
} else { |
232
|
|
|
$params[] = $item; |
233
|
|
|
$ors[] = $this->fieldCheck($key) . ' ' . $value['operator'] . ' ?'; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
$wheres[] = '(' . implode(' OR ', $ors) . ')'; |
237
|
|
|
} |
238
|
|
|
} else { |
239
|
|
|
if ($value['operator'] == 'like') { |
240
|
|
|
$params[] = '%' . $value['value'] . '%'; |
241
|
|
|
$wheres[] = $key . ' ' . $value['operator'] . ' ?'; |
242
|
|
|
} else { |
243
|
|
|
if ($value['value'] === 'null') { |
244
|
|
View Code Duplication |
switch ($value['operator']) { |
|
|
|
|
245
|
|
|
case '!=': |
246
|
|
|
$wheres[] = $key . ' IS NOT NULL'; |
247
|
|
|
break; |
248
|
|
|
|
249
|
|
|
case '==': |
250
|
|
|
default: |
251
|
|
|
$wheres[] = $key . ' IS NULL'; |
252
|
|
|
break; |
253
|
|
|
} |
254
|
|
|
} else { |
255
|
|
|
$params[] = $value['value']; |
256
|
|
|
$wheres[] = $key . ' ' . $value['operator'] . ' ?'; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
} else { |
261
|
|
|
$wheres[] = $key . ' IN (' . implode(', ', array_map(array($this, 'escape'), $value)) . ')'; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
if (count($joins)) { |
267
|
|
|
foreach ($joins as $table => $join) { |
268
|
|
|
$type = isset($join['type'])?$join['type']:'INNER'; |
269
|
|
|
$query .= ' '. $type.' JOIN `' . $table . '` as `' . $join['alias'] . '` ON ' . $join['on'] . ' '; |
270
|
|
|
//$countQuery .= ' '.$type.' JOIN ' . $table . ' ' . $join['alias'] . ' ON ' . $join['on'] . ' '; |
|
|
|
|
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
if (count($manualJoins)) { |
275
|
|
|
foreach ($manualJoins as $join) { |
276
|
|
|
$query .= ' ' . $join . ' '; |
277
|
|
|
//$countQuery .= ' ' . $join . ' '; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$hasWhere = false; |
282
|
|
|
if (count($wheres)) { |
283
|
|
|
$hasWhere = true; |
284
|
|
|
$query .= ' WHERE (' . implode(' ' . $whereType . ' ', $wheres) . ')'; |
285
|
|
|
//$countQuery .= ' WHERE (' . implode(' ' . $whereType . ' ', $wheres) . ')'; |
|
|
|
|
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
if (count($manualWheres)) { |
289
|
|
|
foreach ($manualWheres as $where) { |
290
|
|
|
if (!$hasWhere) { |
291
|
|
|
$hasWhere = true; |
292
|
|
|
$query .= ' WHERE '; |
293
|
|
|
//$countQuery .= ' WHERE '; |
|
|
|
|
294
|
|
|
} else { |
295
|
|
|
$query .= ' ' . $where['type'] . ' '; |
296
|
|
|
//$countQuery .= ' ' . $where['type'] . ' '; |
|
|
|
|
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
$query .= ' ' . $where['query']; |
300
|
|
|
//$countQuery .= ' ' . $where['query']; |
|
|
|
|
301
|
|
|
|
302
|
|
|
if (isset($where['params'])) { |
303
|
|
|
foreach ($where['params'] as $param) { |
304
|
|
|
$params[] = $param; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
if (!is_null($group)) { |
311
|
|
|
$query .= ' GROUP BY ' . $group . ' '; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
if (count($order)) { |
315
|
|
|
$orders = array(); |
316
|
|
|
if (is_string($order) && $order == 'rand') { |
317
|
|
|
$query .= ' ORDER BY RAND() '; |
318
|
|
|
} else { |
319
|
|
|
foreach ($order as $key => $value) { |
|
|
|
|
320
|
|
|
$orders[] = $this->fieldCheck($key) . ' ' . $value; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
$query .= ' ORDER BY ' . implode(', ', $orders); |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
if ($limit) { |
328
|
|
|
$query .= ' LIMIT ' . $limit; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
if ($offset) { |
332
|
|
|
$query .= ' OFFSET ' . $offset; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
|
336
|
|
|
|
337
|
|
|
|
338
|
|
|
// try { |
|
|
|
|
339
|
|
|
// $countQuery = $this->compile_binds($countQuery, $params); |
340
|
|
|
// $res = $this->adapter->getRow($countQuery); |
341
|
|
|
// $count = (int) $res['cnt']; |
342
|
|
|
// } catch(\PDOException $ex) { |
343
|
|
|
// $count = 0; |
344
|
|
|
// } |
345
|
|
|
|
346
|
|
|
|
347
|
|
|
try { |
348
|
|
|
//$query = $this->compile_binds($query, $params); |
|
|
|
|
349
|
|
|
|
350
|
|
|
return $this->adapter->query($query,$params); |
351
|
|
|
//ed( $this->adapter->query($query) ,1); |
|
|
|
|
352
|
|
|
// if ($res = $this->adapter->getRows($query)) { |
353
|
|
|
// $rtn = array(); |
354
|
|
|
// |
355
|
|
|
// foreach ($res as $data) { |
356
|
|
|
// $rtn[] = $this->mapper->buildEntity($data); |
357
|
|
|
// unset($data); |
358
|
|
|
// } |
359
|
|
|
// |
360
|
|
|
// return array('items' => $rtn, 'count' => $count); |
361
|
|
|
// } else { |
362
|
|
|
// return false; |
363
|
|
|
// } |
364
|
|
|
} catch(\PDOException $ex) { |
365
|
|
|
throw $ex; |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* применение значений в стиле PDO |
372
|
|
|
* |
373
|
|
|
* @param type $sql |
374
|
|
|
* @param type $binds |
375
|
|
|
* @return type |
376
|
|
|
*/ |
377
|
|
|
protected function compile_binds($sql, $binds) |
378
|
|
|
{ |
379
|
|
|
if (strpos($sql, $this->bind_marker) === FALSE) { |
380
|
|
|
return $sql; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
if (!is_array($binds)) { |
384
|
|
|
$binds = array($binds); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
// Get the sql segments around the bind markers |
388
|
|
|
$segments = explode($this->bind_marker, $sql); |
389
|
|
|
|
390
|
|
|
// The count of bind should be 1 less then the count of segments |
391
|
|
|
// If there are more bind arguments trim it down |
392
|
|
|
if (count($binds) >= count($segments)) { |
393
|
|
|
$binds = array_slice($binds, 0, count($segments) - 1); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
// Construct the binded query |
397
|
|
|
$result = $segments[0]; |
398
|
|
|
$i = 0; |
399
|
|
|
foreach ($binds as $bind) { |
400
|
|
|
$result .= $this->escape($bind); |
401
|
|
|
$result .= $segments[++$i]; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
return $result; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
public function endTransaction() { |
408
|
|
|
$this->adapter->trans_start(); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
public function startTransaction() { |
412
|
|
|
$this->adapter->trans_complete(); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
|
416
|
|
|
} |
417
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.