Total Complexity | 130 |
Total Lines | 464 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Query often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Query extends \Inji\InjiObject implements \Inji\Db\DriverQuery { |
||
15 | |||
16 | public $curInstance = null; |
||
17 | public $where = []; |
||
18 | public $whereString = ''; |
||
19 | public $having = []; |
||
20 | public $havingString = ''; |
||
21 | public $cols = []; |
||
22 | public $order = null; |
||
23 | public $join = []; |
||
24 | public $group = []; |
||
25 | public $limit = 0; |
||
26 | public $start = 0; |
||
27 | public $error = ''; |
||
28 | public $query = ''; |
||
29 | public $table = ''; |
||
30 | public $operation = ''; |
||
31 | public $indexes = []; |
||
32 | public $params = []; |
||
33 | public $distinct = false; |
||
34 | public $dbOptions = []; |
||
35 | public $colPrefix = ''; |
||
36 | |||
37 | /** |
||
38 | * @param $instance |
||
39 | */ |
||
40 | public function __construct($instance = null) { |
||
41 | if (!$instance) { |
||
42 | $this->curInstance = \App::$cur->db->connection; |
||
43 | } else { |
||
44 | $this->curInstance = $instance; |
||
45 | } |
||
46 | } |
||
47 | |||
48 | public function colPrefix($colPrefix) { |
||
49 | $this->colPrefix = $colPrefix; |
||
50 | } |
||
51 | |||
52 | public function setDbOption($name, $value) { |
||
53 | $this->dbOptions[$name] = $value; |
||
54 | } |
||
55 | |||
56 | public function setTable($tableName) { |
||
57 | $this->table = $tableName; |
||
58 | } |
||
59 | |||
60 | public function insert($table, $data) { |
||
61 | $this->operation = 'INSERT'; |
||
62 | $this->table = $table; |
||
63 | $this->cols = $data; |
||
64 | $this->query(); |
||
65 | return $this->curInstance->pdo->lastInsertId(); |
||
66 | } |
||
67 | |||
68 | public function select($table = null) { |
||
69 | $this->operation = 'SELECT'; |
||
70 | if ($table !== null) { |
||
71 | $this->table = $table; |
||
72 | } |
||
73 | return $this->query(); |
||
74 | } |
||
75 | |||
76 | public function update($table, $data) { |
||
77 | $this->operation = 'UPDATE'; |
||
78 | $this->table = $table; |
||
79 | $this->cols = $data; |
||
80 | $result = $this->query(); |
||
81 | return $result->pdoResult->rowCount(); |
||
82 | } |
||
83 | |||
84 | public function delete($table) { |
||
85 | $this->operation = 'DELETE'; |
||
86 | $this->table = $table; |
||
87 | $result = $this->query(); |
||
88 | return $result->pdoResult->rowCount(); |
||
89 | } |
||
90 | |||
91 | public function createTable($table_name, $cols, $indexes = []) { |
||
92 | $this->operation = 'CREATE TABLE'; |
||
93 | $this->table = $table_name; |
||
94 | $this->cols = $cols; |
||
95 | $this->indexes = $indexes; |
||
96 | return $this->query(); |
||
97 | } |
||
98 | |||
99 | public function cols($cols) { |
||
100 | if (is_array($cols)) { |
||
101 | $this->cols = array_merge($this->cols, array_values($cols)); |
||
102 | } else { |
||
103 | $this->cols[] = $cols; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | public function join($table, $where = false, $type = 'LEFT', $alias = '') { |
||
108 | if (is_array($table)) { |
||
109 | foreach ($table as $item) { |
||
110 | if (!is_array($item)) { |
||
111 | call_user_func_array(array($this, 'join'), $table); |
||
112 | break; |
||
113 | } else { |
||
114 | $this->join($item); |
||
115 | } |
||
116 | } |
||
117 | } else { |
||
118 | $this->join[] = [$table, $where, $type, $alias]; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | public function where($where = '', $value = '', $operation = false, $concatenation = 'AND') { |
||
123 | if (!is_array($where)) { |
||
124 | $this->where[] = [$where, $value, $operation, $concatenation]; |
||
125 | } else { |
||
126 | $this->where[] = $where; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | public function having($where = '', $value = '', $operation = false, $concatenation = 'AND') { |
||
135 | } |
||
136 | } |
||
137 | |||
138 | public function group($colname) { |
||
139 | $this->group[] = $colname; |
||
140 | } |
||
141 | |||
142 | public function start($start) { |
||
143 | $this->start = $start; |
||
144 | } |
||
145 | |||
146 | public function order($order, $type = 'ASC') { |
||
147 | if (!is_array($order)) { |
||
148 | $this->order[] = "{$this->colPrefix}{$order} {$type}"; |
||
149 | } else { |
||
150 | foreach ($order as $item) { |
||
151 | if (!is_array($item)) { |
||
152 | call_user_func_array(array($this, 'order'), $order); |
||
153 | break; |
||
154 | } else { |
||
155 | $this->order($item); |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | public function limit($limit = 0, $len = 0) { |
||
162 | $this->limit = $limit; |
||
163 | } |
||
164 | |||
165 | public function buildLimit() { |
||
166 | $start = intval($this->start); |
||
167 | $len = intval($this->limit); |
||
168 | $str = "LIMIT {$start}"; |
||
169 | if ($len !== 0) { |
||
170 | $str .= ",{$len}"; |
||
171 | } |
||
172 | return $str; |
||
173 | } |
||
174 | |||
175 | public function buildJoin($table, $where = false, $type = 'LEFT', $alias = '') { |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Build where string |
||
196 | * |
||
197 | * @param string|array $where |
||
198 | * @param mixed $value |
||
199 | * @param string $operation |
||
200 | * @param string $concatenation |
||
201 | */ |
||
202 | public function buildWhere($where = '', $value = '', $operation = '=', $concatenation = 'AND') { |
||
203 | if (!is_array($where)) { |
||
204 | if (empty($operation)) { |
||
205 | $operation = '='; |
||
206 | } |
||
207 | |||
208 | if ($concatenation === false) { |
||
209 | $concatenation = 'AND'; |
||
210 | } elseif ($concatenation === true) { |
||
211 | $concatenation = ''; |
||
212 | } |
||
213 | |||
214 | if ($this->whereString == '') { |
||
215 | $this->whereString = ' WHERE '; |
||
216 | } |
||
217 | |||
218 | if (stristr($operation, 'IN') || stristr($operation, 'NOT IN')) { |
||
219 | if (is_array($value)) { |
||
220 | $newValue = ''; |
||
221 | foreach ($value as $item) { |
||
222 | if ($newValue) { |
||
223 | $newValue .= ','; |
||
224 | } |
||
225 | if (is_string($item)) { |
||
226 | $newValue .= '"' . $item . '"'; |
||
227 | } else { |
||
228 | $newValue .= $item; |
||
229 | } |
||
230 | } |
||
231 | $value = '(' . $newValue . ')'; |
||
232 | } elseif (!preg_match('!\(!', $value) && !preg_match('![^0-9,\.\(\) ]!', $value)) { |
||
233 | $value = "({$value})"; |
||
234 | } elseif (preg_match('!\(!', $value) && preg_match('![^0-9,\.\(\) ]!', $value)) { |
||
235 | $value = "\"{$value}\""; |
||
236 | } |
||
237 | } elseif (!in_array($value, array('CURRENT_TIMESTAMP', 'NULL'))) { |
||
238 | $this->params[] = $value; |
||
239 | $value = "?"; |
||
240 | } |
||
241 | |||
242 | if (substr($this->whereString, -1, 1) == '(' || substr($this->whereString, -2, 2) == 'E ') { |
||
243 | $this->whereString .= " {$this->colPrefix}{$where} {$operation} {$value} "; |
||
244 | } else { |
||
245 | $this->whereString .= "{$concatenation} {$this->colPrefix}{$where} {$operation} {$value} "; |
||
246 | } |
||
247 | } else { |
||
248 | $i = -1; |
||
249 | while (isset($where[++$i])) { |
||
250 | $item = $where[$i]; |
||
251 | if (isset($where[$i + 1]) && !isset($where[$i - 1]) && is_array($where[$i])) { |
||
252 | if ($this->whereString != null && substr($this->whereString, -1, 1) != '(' && $this->whereString != 'WHERE ') { |
||
253 | if (!isset($item[3])) { |
||
254 | $concatenation = 'AND'; |
||
255 | } else { |
||
256 | $concatenation = $item[3]; |
||
257 | } |
||
258 | |||
259 | $this->whereString .= "{$concatenation} "; |
||
260 | } |
||
261 | |||
262 | if ($this->whereString != '') { |
||
263 | $this->whereString .= '('; |
||
264 | } else { |
||
265 | $this->whereString = 'WHERE ('; |
||
266 | } |
||
267 | } |
||
268 | |||
269 | if (!is_array($item)) { |
||
270 | call_user_func_array(array($this, 'buildWhere'), $where); |
||
|
|||
271 | break; |
||
272 | } else { |
||
273 | $this->buildWhere($item); |
||
274 | } |
||
275 | if (!isset($where[$i + 1]) && isset($where[$i - 1])) { |
||
276 | $this->whereString .= ') '; |
||
277 | } |
||
278 | } |
||
279 | } |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Build having string |
||
284 | * |
||
285 | * @param string|array $where |
||
286 | * @param mixed $value |
||
287 | * @param string $operation |
||
288 | * @param string $concatenation |
||
289 | */ |
||
290 | public function buildHaving($where = '', $value = '', $operation = '=', $concatenation = 'AND') { |
||
291 | if (!is_array($where)) { |
||
292 | if (empty($operation)) { |
||
293 | $operation = '='; |
||
294 | } |
||
295 | if ($concatenation === false) { |
||
296 | $concatenation = 'AND'; |
||
297 | } elseif ($concatenation === true) { |
||
298 | $concatenation = ''; |
||
299 | } |
||
300 | |||
301 | if ($this->havingString == '') { |
||
302 | $this->havingString = ' HAVING '; |
||
303 | } |
||
304 | |||
305 | if (stristr($operation, 'IN') || stristr($operation, 'NOT IN')) { |
||
306 | if (is_array($value)) { |
||
307 | $newValue = ''; |
||
308 | foreach ($value as $item) { |
||
309 | if ($newValue) { |
||
310 | $newValue .= ','; |
||
311 | } |
||
312 | if (is_string($item)) { |
||
313 | $newValue .= '"' . $item . '"'; |
||
314 | } else { |
||
315 | $newValue .= $item; |
||
316 | } |
||
317 | } |
||
318 | $value = '(' . $newValue . ')'; |
||
319 | } elseif (!preg_match('!\(!', $value) && !preg_match('![^0-9,\.\(\) ]!', $value)) { |
||
320 | $value = "({$value})"; |
||
321 | } elseif (preg_match('!\(!', $value) && preg_match('![^0-9,\.\(\) ]!', $value)) { |
||
322 | $value = "\"{$value}\""; |
||
323 | } |
||
324 | } elseif (!in_array($value, array('CURRENT_TIMESTAMP', 'NULL'))) { |
||
325 | $this->params[] = $value; |
||
326 | $value = "?"; |
||
327 | } |
||
328 | if (substr($this->havingString, -1, 1) == '(' || substr($this->havingString, -2, 2) == 'G ') { |
||
329 | $this->havingString .= " {$this->colPrefix}{$where} {$operation} {$value} "; |
||
330 | } else { |
||
331 | $this->havingString .= "{$concatenation} {$this->colPrefix}{$where} {$operation} {$value} "; |
||
332 | } |
||
333 | } else { |
||
334 | $i = -1; |
||
335 | while (isset($where[++$i])) { |
||
336 | $item = $where[$i]; |
||
337 | if (isset($where[$i + 1]) && !isset($where[$i - 1]) && is_array($where[$i])) { |
||
338 | if ($this->havingString != null && substr($this->havingString, -1, 1) != '(' && $this->havingString != 'HAVING ') { |
||
339 | if (!isset($item[3])) { |
||
340 | $concatenation = 'AND'; |
||
341 | } else { |
||
342 | $concatenation = $item[3]; |
||
343 | } |
||
344 | |||
345 | $this->havingString .= "{$concatenation} "; |
||
346 | } |
||
347 | |||
348 | if ($this->havingString != '') { |
||
349 | $this->havingString .= '('; |
||
350 | } else { |
||
351 | $this->havingString = 'HAVING ('; |
||
352 | } |
||
353 | } |
||
354 | |||
355 | if (!is_array($item)) { |
||
356 | call_user_func_array(array($this, 'buildHaving'), $where); |
||
357 | break; |
||
358 | } else { |
||
359 | $this->buildHaving($item); |
||
360 | } |
||
361 | if (!isset($where[$i + 1]) && isset($where[$i - 1])) { |
||
362 | $this->havingString .= ') '; |
||
363 | } |
||
364 | } |
||
365 | } |
||
366 | } |
||
367 | |||
368 | public function buildQuery() { |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Execute query |
||
450 | * |
||
451 | * @param string|array $query |
||
452 | * @return Result |
||
453 | */ |
||
454 | public function query($query = []) { |
||
478 | } |
||
479 | } |