1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ahmetertem; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Query builder. |
7
|
|
|
*/ |
8
|
|
|
class qb |
9
|
|
|
{ |
10
|
|
|
const DRIVER_MYSQL = 0; |
11
|
|
|
const INNER_JOIN = 0; |
12
|
|
|
const LEFT_JOIN = 1; |
13
|
|
|
const RIGHT_JOIN = 2; |
14
|
|
|
const FULL_JOIN = 3; |
15
|
|
|
|
16
|
|
|
private $_conditions = array(); |
17
|
|
|
private $_table_names = array(); |
18
|
|
|
private $_read_fields = array(); |
19
|
|
|
private $_write_fields = array(); |
20
|
|
|
private $_write_values = array(); |
21
|
|
|
private $_write_field_types = array(); |
22
|
|
|
private $_order_fields = array(); |
23
|
|
|
private $_group_fields = array(); |
24
|
|
|
private $_joins = array(); |
25
|
|
|
|
26
|
|
|
public $limit = 100; |
27
|
|
|
public $limit_offset = -1; |
28
|
|
|
|
29
|
|
|
public static $engine = qb::MYSQL; |
30
|
|
|
public static $default_limit = 100; |
31
|
|
|
|
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
$this->limit = self::$default_limit; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function resetTable() |
38
|
|
|
{ |
39
|
|
|
$this->_table_names = array(); |
40
|
|
|
|
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function setTable($table_name) |
45
|
|
|
{ |
46
|
|
|
$this->resetTable(); |
47
|
|
|
$this->table($table_name); |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function table($table_name) |
53
|
|
|
{ |
54
|
|
|
if ($table_name == null) { |
55
|
|
|
return $this->_table_names[0]; |
56
|
|
|
} |
57
|
|
|
$this->_table_names[] = $table_name; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function resetSet() |
63
|
|
|
{ |
64
|
|
|
$this->_write_fields = array(); |
65
|
|
|
$this->_write_values = array(); |
66
|
|
|
$this->_write_field_types = array(); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* $type : |
73
|
|
|
* - 0 = string |
74
|
|
|
* - 1 = integer |
75
|
|
|
* - 2 = raw. |
76
|
|
|
*/ |
77
|
|
|
public function set($field, $value, $type = 0) |
78
|
|
|
{ |
79
|
|
|
$this->_write_fields[] = $field; |
80
|
|
|
$this->_write_values[] = $value; |
81
|
|
|
$this->_write_field_types[] = $type; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function resetWhere() |
87
|
|
|
{ |
88
|
|
|
$this->_conditions = array(); |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function where($field, $value = null, $operator = '=') |
94
|
|
|
{ |
95
|
|
|
$this->_conditions[] = $this->c($field, $value, $operator); |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Adds "<strong><em>or</em></strong>" condition to current and condition |
102
|
|
|
* array. |
103
|
|
|
*/ |
104
|
|
|
public function whereOr() |
105
|
|
|
{ |
106
|
|
|
$this->_conditions[] = '('.implode(' or ', func_get_args()).')'; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public static function c($field, $value = null, $operator = '=') |
112
|
|
|
{ |
113
|
|
|
if (is_null($value)) { |
114
|
|
|
return $field; |
115
|
|
|
} else { |
116
|
|
|
return "{$field} {$operator} {$value}"; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function resetSelect() |
121
|
|
|
{ |
122
|
|
|
$this->_read_fields = array(); |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function select($field) |
128
|
|
|
{ |
129
|
|
|
$this->_read_fields[] = $field; |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function groupBy($field) |
135
|
|
|
{ |
136
|
|
|
$this->_group_fields[] = $field; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function orderBy($field, $asc = true) |
142
|
|
|
{ |
143
|
|
|
$this->_order_fields[$field] = array($field, $asc); |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function setLimit($limit, $limit_offset = -1) |
149
|
|
|
{ |
150
|
|
|
$this->limit = $limit; |
151
|
|
|
if ($limit_offset != -1) { |
152
|
|
|
$this->limit_offset = $limit_offset; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getSelect() |
159
|
|
|
{ |
160
|
|
|
if (count($this->_table_names) == 0) { |
161
|
|
|
throw new \Exception('Specify at least 1 table name'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// |
165
|
|
|
// selects |
166
|
|
|
// |
167
|
|
|
$_read_fields = $this->_read_fields; |
168
|
|
|
if (count($_read_fields) == 0) { |
169
|
|
|
$_read_fields[] = '*'; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// |
173
|
|
|
// joins |
174
|
|
|
// |
175
|
|
|
$joins = ''; |
176
|
|
|
if (count($this->_joins) > 0) { |
177
|
|
|
$joins = implode(' ', $this->_joins); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// |
181
|
|
|
// limits |
182
|
|
|
// |
183
|
|
|
$limit = null; |
184
|
|
View Code Duplication |
if ($this->limit > 0) { |
|
|
|
|
185
|
|
|
$limit = ' limit '.($this->limit_offset != -1 ? $this->limit_offset.', ' : null).$this->limit; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// |
189
|
|
|
// group bys |
190
|
|
|
// |
191
|
|
|
$group = null; |
192
|
|
|
if (count($this->_group_fields) > 0) { |
193
|
|
|
$group = ' group by '.implode(', ', $this->_group_fields).' '; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// |
197
|
|
|
// order bys |
198
|
|
|
// |
199
|
|
|
$order = null; |
200
|
|
|
if (count($this->_order_fields) > 0) { |
201
|
|
|
$order = ' order by '; |
202
|
|
|
$i = 0; |
203
|
|
|
foreach ($this->_order_fields as $of) { |
204
|
|
|
$order .= ($i > 0 ? ', ' : null); |
205
|
|
|
if (!is_null($of[1])) { |
206
|
|
|
$pos = strpos($of[0], '.'); |
207
|
|
|
if ($pos !== false) { |
208
|
|
|
$t = explode('.', $of[0]); |
209
|
|
|
$t[1] = '`'.$t[1].'`'; |
210
|
|
|
$of[0] = implode('.', $t); |
211
|
|
|
$order .= $of[0]; |
212
|
|
|
} else { |
213
|
|
|
$order .= '`'.$of[0].'`'; |
214
|
|
|
} |
215
|
|
|
$order .= ' '.($of[1] ? 'asc' : 'desc'); |
216
|
|
|
} else { |
217
|
|
|
$order .= $of[0]; |
218
|
|
|
} |
219
|
|
|
++$i; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
$string = sprintf('select %1$s from %2$s%7$s%3$s%6$s%4$s%5$s', implode(', ', $_read_fields), implode(', ', $this->_table_names), count($this->_conditions) > 0 ? (' where '.implode(' and ', $this->_conditions)) : null, $order, $limit, $group, $joins); |
223
|
|
|
|
224
|
|
|
return $string; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function getUpdate() |
228
|
|
|
{ |
229
|
|
|
if (count($this->_table_names) == 0) { |
230
|
|
|
throw new \Exception('Specify at least 1 table name'); |
231
|
|
|
} |
232
|
|
|
if (count($this->_write_fields) == 0) { |
233
|
|
|
throw new \Exception('Specify at least 1 write field (set function)'); |
234
|
|
|
} |
235
|
|
|
$updates = array(); |
236
|
|
|
for ($d = 0, $m = count($this->_write_fields); $d < $m; ++$d) { |
237
|
|
|
$t = '`' . $this->_write_fields[$d].'`='; |
238
|
|
|
switch ($this->_write_field_types[$d]) { |
239
|
|
|
case 0: |
240
|
|
|
$t .= "'".$this->_write_values[$d]."'"; |
241
|
|
|
break; |
242
|
|
|
default: |
243
|
|
|
$t .= $this->_write_values[$d]; |
244
|
|
|
} |
245
|
|
|
$updates[] = $t; |
246
|
|
|
} |
247
|
|
|
$update_fields = implode(', ', $updates); |
248
|
|
|
$limit = null; |
249
|
|
View Code Duplication |
if ($this->limit > 0) { |
|
|
|
|
250
|
|
|
$limit = ' limit '.($this->limit_offset != -1 ? $this->limit_offset.', ' : null).$this->limit; |
251
|
|
|
} |
252
|
|
|
$where = count($this->_conditions) > 0 ? (' where '.implode(' and ', $this->_conditions)) : null; |
253
|
|
|
$string = sprintf('update %1$s set %2$s %3$s %4$s', $this->_table_names[0], $update_fields, $where, $limit); |
254
|
|
|
|
255
|
|
|
return $string; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function getInsert() |
259
|
|
|
{ |
260
|
|
|
if (count($this->_table_names) == 0) { |
261
|
|
|
throw new \Exception('Specify at least 1 table name'); |
262
|
|
|
} |
263
|
|
|
if (count($this->_write_fields) == 0) { |
264
|
|
|
throw new \Exception('Specify at least 1 write field (set function)'); |
265
|
|
|
} |
266
|
|
|
$table = $this->_table_names[0]; |
267
|
|
|
$fields = '`'.implode('`, `', $this->_write_fields).'`'; |
268
|
|
|
$values = array(); |
269
|
|
|
for ($d = 0, $m = count($this->_write_fields); $d < $m; ++$d) { |
270
|
|
|
switch ($this->_write_field_types[$d]) { |
271
|
|
|
case 0: |
272
|
|
|
$values[] = "'".$this->_write_values[$d]."'"; |
273
|
|
|
break; |
274
|
|
|
default: |
275
|
|
|
$values[] = $this->_write_values[$d]; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
$string = sprintf('insert into %1$s (%2$s) values(%3$s)', $table, $fields, implode(', ', $values)); |
279
|
|
|
|
280
|
|
|
return $string; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function getDelete() |
284
|
|
|
{ |
285
|
|
|
if (count($this->_table_names) == 0) { |
286
|
|
|
throw new \Exception('Specify at least 1 table name'); |
287
|
|
|
} |
288
|
|
|
$table = $this->_table_names[0]; |
289
|
|
|
$where = count($this->_conditions) > 0 ? (' where '.implode(' and ', $this->_conditions)) : null; |
290
|
|
|
$limit = null; |
291
|
|
View Code Duplication |
if ($this->limit > 0) { |
|
|
|
|
292
|
|
|
$limit = ' limit '.($this->limit_offset != -1 ? $this->limit_offset.', ' : null).$this->limit; |
293
|
|
|
} |
294
|
|
|
$string = sprintf('delete from %1$s %2$s %3$s', $table, $where, $limit); |
295
|
|
|
|
296
|
|
|
return $string; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function join($string) |
300
|
|
|
{ |
301
|
|
|
$this->_joins[] = $string; |
302
|
|
|
|
303
|
|
|
return $this; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public function getJoin($join_type = qb::INNER_JOIN) |
307
|
|
|
{ |
308
|
|
|
if (count($this->_table_names) == 0) { |
309
|
|
|
throw new \Exception('Specify at least 1 table name'); |
310
|
|
|
} |
311
|
|
|
switch ($join_type) { |
312
|
|
|
case qb::INNER_JOIN: |
313
|
|
|
$join_type_ = 'INNER JOIN'; |
314
|
|
|
break; |
315
|
|
|
case qb::LEFT_JOIN: |
316
|
|
|
$join_type_ = 'LEFT JOIN'; |
317
|
|
|
break; |
318
|
|
|
case qb::RIGHT_JOIN: |
319
|
|
|
$join_type_ = 'RIGHT JOIN'; |
320
|
|
|
break; |
321
|
|
|
case qb::FULL_JOIN: |
322
|
|
|
$join_type_ = 'FULL OUTER JOIN'; |
323
|
|
|
break; |
324
|
|
|
} |
325
|
|
|
$table = $this->_table_names[0]; |
326
|
|
|
$where = count($this->_conditions) > 0 ? (' on ' . implode(' and ', $this->_conditions)) : null; |
327
|
|
|
return sprintf(' %1$s %2$s %3$s ', $join_type_, $table, $where); |
|
|
|
|
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.