Completed
Push — master ( 2ae423...cd04f7 )
by Ahmet
02:54
created

qb::setLimit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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