Completed
Push — master ( cd04f7...3c6e87 )
by Ahmet
02:56
created

qb::orderBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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
    private $_joins = array();
19
    public $limit = 100;
20
    public $limit_offset = -1;
21
    public static $default_limit = 100;
22
23
    public function __construct()
24
    {
25
        $this->limit = self::$default_limit;
26
    }
27
28
    public function resetTable()
29
    {
30
        $this->_table_names = array();
31
32
        return true;
33
    }
34
35
    public function setTable($table_name)
36
    {
37
        $this->resetTable();
38
        $this->table($table_name);
39
40
        return $this;
41
    }
42
43
    public function table($table_name)
44
    {
45
        $this->_table_names[] = $table_name;
46
47
        return $this;
48
    }
49
50
    public function resetSet()
51
    {
52
        $this->_write_fields = array();
53
        $this->_write_values = array();
54
        $this->_write_field_types = array();
55
56
        return $this;
57
    }
58
59
    /**
60
     * $type :
61
     *        - 0 = string
62
     *        - 1 = integer
63
     *        - 2 = raw.
64
     */
65
    public function set($field, $value, $type = 0)
66
    {
67
        $this->_write_fields[] = $field;
68
        $this->_write_values[] = $value;
69
        $this->_write_field_types[] = $type;
70
71
        return $this;
72
    }
73
74
    public function resetWhere()
75
    {
76
        $this->_conditions = array();
77
78
        return $this;
79
    }
80
81
    public function where($field, $value = null, $operator = '=')
82
    {
83
        $this->_conditions[] = $this->c($field, $value, $operator);
84
85
        return $this;
86
    }
87
88
    /**
89
     * Adds "<strong><em>or</em></strong>" condition to current and condition
90
     * array.
91
     */
92
    public function whereOr()
93
    {
94
        $this->_conditions[] = '('.implode(' or ', func_get_args()).')';
95
96
        return $this;
97
    }
98
99
    public static function c($field, $value = null, $operator = '=')
100
    {
101
        if (is_null($value)) {
102
            return $field;
103
        } else {
104
            return "{$field} {$operator} {$value}";
105
        }
106
    }
107
108
    public function resetSelect()
109
    {
110
        $this->_read_fields = array();
111
112
        return $this;
113
    }
114
115
    public function select($field)
116
    {
117
        $this->_read_fields[] = $field;
118
119
        return $this;
120
    }
121
122
    public function groupBy($field)
123
    {
124
        $this->_group_fields[] = $field;
125
126
        return $this;
127
    }
128
129
    public function orderBy($field, $asc = true)
130
    {
131
        $this->_order_fields[$field] = array($field, $asc);
132
133
        return $this;
134
    }
135
136
    public function setLimit($limit, $limit_offset = -1)
137
    {
138
        $this->limit = $limit;
139
        if ($limit_offset != -1) {
140
            $this->limit_offset = $limit_offset;
141
        }
142
143
        return $this;
144
    }
145
146
    public function join($table_name, $on, $join_type = null)
147
    {
148
        $this->_joins[] = array('table' => $table_name, 'on' => $on, 'type' => $join_type);
149
150
        return $this;
151
    }
152
153
    public function getSelect()
154
    {
155
        if (count($this->_table_names) == 0) {
156
            throw new \Exception('Specify at least 1 table name');
157
        }
158
159
        //
160
        // selects
161
        //
162
        $_read_fields = $this->_read_fields;
163
        if (count($_read_fields) == 0) {
164
            $_read_fields[] = '*';
165
        }
166
167
        //
168
        // joins
169
        //
170
        $joins = null;
171
        foreach ($this->_joins as $join) {
172
            $joins .= $join['type'].' JOIN '.$join['table'].' on '.$join['on'].' ';
173
        }
174
175
        //
176
        // limits
177
        //
178
        $limit = null;
179 View Code Duplication
        if ($this->limit > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
180
            $limit = ' limit '.($this->limit_offset != -1 ? $this->limit_offset.', ' : null).$this->limit;
181
        }
182
183
        //
184
        // group bys
185
        //
186
        $group = null;
187
        if (count($this->_group_fields) > 0) {
188
            $group = ' group by '.implode(', ', $this->_group_fields).' ';
189
        }
190
191
        //
192
        // order bys
193
        //
194
        $order = null;
195
        if (count($this->_order_fields) > 0) {
196
            $order = ' order by ';
197
            $i = 0;
198
            foreach ($this->_order_fields as $of) {
199
                $order .= ($i > 0 ? ', ' : null);
200
                if (!is_null($of[1])) {
201
                    $pos = strpos($of[0], '.');
202
                    if ($pos !== false) {
203
                        $t = explode('.', $of[0]);
204
                        $t[1] = '`'.$t[1].'`';
205
                        $of[0] = implode('.', $t);
206
                        $order .= $of[0];
207
                    } else {
208
                        $order .= '`'.$of[0].'`';
209
                    }
210
                    $order .= ' '.($of[1] ? 'asc' : 'desc');
211
                } else {
212
                    $order .= $of[0];
213
                }
214
                ++$i;
215
            }
216
        }
217
        $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);
218
219
        return $string;
220
    }
221
222
    public function getUpdate()
223
    {
224
        if (count($this->_table_names) == 0) {
225
            throw new \Exception('Specify at least 1 table name');
226
        }
227
        if (count($this->_write_fields) == 0) {
228
            throw new \Exception('Specify at least 1 write field (set function)');
229
        }
230
        $updates = array();
231
        for ($d = 0, $m = count($this->_write_fields); $d < $m; ++$d) {
232
            $t = $this->_write_fields[$d].'=';
233
            switch ($this->_write_field_types[$d]) {
234
            case 0:
235
              $t .= "'".$this->_write_values[$d]."'";
236
              break;
237
          default:
238
              $t .= $this->_write_values[$d];
239
          }
240
            $updates[] = $t;
241
        }
242
        $update_fields = implode(', ', $updates);
243
        $limit = null;
244 View Code Duplication
        if ($this->limit > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
245
            $limit = ' limit '.($this->limit_offset != -1 ? $this->limit_offset.', ' : null).$this->limit;
246
        }
247
        $where = count($this->_conditions) > 0 ? (' where '.implode(' and ', $this->_conditions)) : null;
248
        $string = sprintf('update %1$s set %2$s %3$s %4$s', $this->_table_names[0], $update_fields, $where, $limit);
249
250
        return $string;
251
    }
252
253
    public function getInsert()
254
    {
255
        if (count($this->_table_names) == 0) {
256
            throw new \Exception('Specify at least 1 table name');
257
        }
258
        if (count($this->_write_fields) == 0) {
259
            throw new \Exception('Specify at least 1 write field (set function)');
260
        }
261
        $table = $this->_table_names[0];
262
        $fields = '`'.implode('`, `', $this->_write_fields).'`';
263
        $values = array();
264
        for ($d = 0, $m = count($this->_write_fields); $d < $m; ++$d) {
265
            switch ($this->_write_field_types[$d]) {
266
            case 0:
267
              $values[] = "'".$this->_write_values[$d]."'";
268
              break;
269
            default:
270
              $values[] = $this->_write_values[$d];
271
          }
272
        }
273
        $string = sprintf('insert into %1$s (%2$s) values(%3$s)', $table, $fields, implode(', ', $values));
274
275
        return $string;
276
    }
277
278
    public function getDelete()
279
    {
280
        if (count($this->_table_names) == 0) {
281
            throw new \Exception('Specify at least 1 table name');
282
        }
283
        $table = $this->_table_names[0];
284
        $where = count($this->_conditions) > 0 ? (' where '.implode(' and ', $this->_conditions)) : null;
285
        $limit = null;
286 View Code Duplication
        if ($this->limit > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
287
            $limit = ' limit '.($this->limit_offset != -1 ? $this->limit_offset.', ' : null).$this->limit;
288
        }
289
        $string = sprintf('delete from %1$s %2$s %3$s', $table, $where, $limit);
290
291
        return $string;
292
    }
293
}
294