1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ahmetertem; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Query builder. |
7
|
|
|
*/ |
8
|
|
|
class qb |
9
|
|
|
{ |
10
|
|
|
private $_conditions = array(); |
11
|
|
|
private $_table_names = array(); |
12
|
|
|
private $_read_fields = array(); |
13
|
|
|
private $_write_fields = array(); |
14
|
|
|
private $_write_values = array(); |
15
|
|
|
private $_write_field_types = array(); |
16
|
|
|
private $_order_fields = array(); |
17
|
|
|
private $_group_fields = array(); |
18
|
|
|
public $limit = 100; |
19
|
|
|
public $limit_offset = -1; |
20
|
|
|
public static $default_limit = 100; |
21
|
|
|
|
22
|
|
|
public function __construct($table_name = null) |
23
|
|
|
{ |
24
|
|
|
$this->limit = self::$default_limit; |
25
|
|
|
if (!is_null($table_name)) { |
26
|
|
|
$this->table($table_name); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function resetTables() |
31
|
|
|
{ |
32
|
|
|
$this->_table_names = array(); |
33
|
|
|
|
34
|
|
|
return true; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function setTable($table_name) |
38
|
|
|
{ |
39
|
|
|
$this->resetTables(); |
40
|
|
|
$this->table($table_name); |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function table($table_name) |
46
|
|
|
{ |
47
|
|
|
$this->_table_names[] = $table_name; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function resetWrite() |
53
|
|
|
{ |
54
|
|
|
$this->_write_fields = array(); |
55
|
|
|
$this->_write_values = array(); |
56
|
|
|
$this->_write_field_types = array(); |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* $type : |
63
|
|
|
* - 0 = string |
64
|
|
|
* - 1 = integer |
65
|
|
|
* - 2 = raw. |
66
|
|
|
*/ |
67
|
|
|
public function set($field, $value, $type = 0) |
68
|
|
|
{ |
69
|
|
|
$this->_write_fields[] = $field; |
70
|
|
|
$this->_write_values[] = $value; |
71
|
|
|
$this->_write_field_types[] = $type; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function resetConditions() |
77
|
|
|
{ |
78
|
|
|
$this->_conditions = array(); |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function and($field, $value = null, $operator = '=') |
84
|
|
|
{ |
85
|
|
|
$this->_conditions[] = $this->c($field, $value, $operator); |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Adds "<strong><em>or</em></strong>" condition to current and condition |
92
|
|
|
* array. |
93
|
|
|
*/ |
94
|
|
|
public function or() |
95
|
|
|
{ |
96
|
|
|
$this->_conditions[] = '('.implode(' or ', func_get_args()).')'; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public static function c($field, $value = null, $operator = '=') |
102
|
|
|
{ |
103
|
|
|
if (is_null($value)) { |
104
|
|
|
return $field; |
105
|
|
|
} else { |
106
|
|
|
return "{$field} {$operator} {$value}"; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function resetReadFields() |
111
|
|
|
{ |
112
|
|
|
$this->_read_fields = array(); |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function select($field) |
118
|
|
|
{ |
119
|
|
|
$this->_read_fields[] = $field; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function groupBy($field) |
125
|
|
|
{ |
126
|
|
|
$this->_group_fields[] = $field; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function orderBy($field, $asc = true) |
132
|
|
|
{ |
133
|
|
|
$this->_order_fields[$field] = array($field, $asc); |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getSelect() |
139
|
|
|
{ |
140
|
|
|
$_read_fields = $this->_read_fields; |
141
|
|
|
if (count($_read_fields) == 0) { |
142
|
|
|
$_read_fields[] = '*'; |
143
|
|
|
} |
144
|
|
|
$limit = null; |
145
|
|
View Code Duplication |
if ($this->limit > 0) { |
|
|
|
|
146
|
|
|
$limit = ' limit '.($this->limit_offset != -1 ? $this->limit_offset.', ' : null).$this->limit; |
147
|
|
|
} |
148
|
|
|
$group = null; |
149
|
|
|
if (count($this->_group_fields) > 0) { |
150
|
|
|
$group = ' group by '.implode(', ', $this->_group_fields).' '; |
151
|
|
|
} |
152
|
|
|
$order = null; |
153
|
|
|
if (count($this->_order_fields) > 0) { |
154
|
|
|
$order = ' order by '; |
155
|
|
|
$i = 0; |
156
|
|
|
foreach ($this->_order_fields as $of) { |
157
|
|
|
$order .= ($i > 0 ? ', ' : null); |
158
|
|
|
if (!is_null($of[1])) { |
159
|
|
|
$pos = strpos($of[0], '.'); |
160
|
|
|
if ($pos !== false) { |
161
|
|
|
$t = explode('.', $of[0]); |
162
|
|
|
$t[1] = '`'.$t[1].'`'; |
163
|
|
|
$of[0] = implode('.', $t); |
164
|
|
|
$order .= $of[0]; |
165
|
|
|
} else { |
166
|
|
|
$order .= '`'.$of[0].'`'; |
167
|
|
|
} |
168
|
|
|
$order .= ' '.($of[1] ? 'asc' : 'desc'); |
169
|
|
|
} else { |
170
|
|
|
$order .= $of[0]; |
171
|
|
|
} |
172
|
|
|
++$i; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
$string = sprintf('select %1$s from %2$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); |
176
|
|
|
|
177
|
|
|
return $string; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function getUpdate() |
181
|
|
|
{ |
182
|
|
|
$updates = array(); |
183
|
|
|
for ($d = 0, $m = count($this->_write_fields); $d < $m; ++$d) { |
184
|
|
|
$t = $this->_write_fields[$d].'='; |
185
|
|
|
switch ($this->_write_field_types[$d]) { |
186
|
|
|
case 0: |
187
|
|
|
$t .= "'".$this->_write_values[$d]."'"; |
188
|
|
|
break; |
189
|
|
|
default: |
190
|
|
|
$t .= $this->_write_values[$d]; |
191
|
|
|
} |
192
|
|
|
$updates[] = $t; |
193
|
|
|
} |
194
|
|
|
$update_fields = implode(', ', $updates); |
195
|
|
|
$limit = null; |
196
|
|
View Code Duplication |
if ($this->limit > 0) { |
|
|
|
|
197
|
|
|
$limit = ' limit '.($this->limit_offset != -1 ? $this->limit_offset.', ' : null).$this->limit; |
198
|
|
|
} |
199
|
|
|
$where = count($this->_conditions) > 0 ? (' where '.implode(' and ', $this->_conditions)) : null; |
200
|
|
|
$string = sprintf('update %1$s set %2$s %3$s %4$s', $this->_table_names[0], $update_fields, $where, $limit); |
201
|
|
|
|
202
|
|
|
return $string; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function getInsert() |
206
|
|
|
{ |
207
|
|
|
$table = $this->_table_names[0]; |
208
|
|
|
$fields = '`'.implode('`, `', $this->_write_fields).'`'; |
209
|
|
|
$values = array(); |
210
|
|
|
for ($d = 0, $m = count($this->_write_fields); $d < $m; ++$d) { |
211
|
|
|
switch ($this->_write_field_types[$d]) { |
212
|
|
|
case 0: |
213
|
|
|
$values[] = "'".$this->_write_values[$d]."'"; |
214
|
|
|
break; |
215
|
|
|
default: |
216
|
|
|
$values[] = $this->_write_values[$d]; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
$string = sprintf('insert into %1$s (%2$s) values(%3$s)', $table, $fields, implode(', ', $values)); |
220
|
|
|
|
221
|
|
|
return $string; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function getDelete() |
225
|
|
|
{ |
226
|
|
|
$table = $this->_table_names[0]; |
227
|
|
|
$where = count($this->_conditions) > 0 ? (' where '.implode(' and ', $this->_conditions)) : null; |
228
|
|
|
$limit = null; |
229
|
|
View Code Duplication |
if ($this->limit > 0) { |
|
|
|
|
230
|
|
|
$limit = ' limit '.($this->limit_offset != -1 ? $this->limit_offset.', ' : null).$this->limit; |
231
|
|
|
} |
232
|
|
|
$string = sprintf('delete from %1$s %2$s %3$s', $table, $where, $limit); |
233
|
|
|
|
234
|
|
|
return $string; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
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.