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

qb::getUpdate()   C

Complexity

Conditions 8
Paths 20

Size

Total Lines 30
Code Lines 22

Duplication

Lines 3
Ratio 10 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 3
loc 30
rs 5.3846
cc 8
eloc 22
nc 20
nop 0
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 getSelect()
136
    {
137
		if(count($this->_table_names) == 0) {
138
			throw new exception('Specify at least 1 table name');
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) {
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...
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
		if(count($this->_table_names) == 0) {
183
			throw new exception('Specify at least 1 table name');
184
		}
185
		if(count($this->_write_fields) == 0) {
186
			throw new exception('Specify at least 1 write field (set function)');
187
		}
188
        $updates = array();
189
        for ($d = 0, $m = count($this->_write_fields); $d < $m; ++$d) {
190
            $t = $this->_write_fields[$d].'=';
191
            switch ($this->_write_field_types[$d]) {
192
            case 0:
193
              $t .= "'".$this->_write_values[$d]."'";
194
              break;
195
          default:
196
              $t .= $this->_write_values[$d];
197
          }
198
            $updates[] = $t;
199
        }
200
        $update_fields = implode(', ', $updates);
201
        $limit = null;
202 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...
203
            $limit = ' limit '.($this->limit_offset != -1 ? $this->limit_offset.', ' : null).$this->limit;
204
        }
205
        $where = count($this->_conditions) > 0 ? (' where '.implode(' and ', $this->_conditions)) : null;
206
        $string = sprintf('update %1$s set %2$s %3$s %4$s', $this->_table_names[0], $update_fields, $where, $limit);
207
208
        return $string;
209
    }
210
211
    public function getInsert()
212
    {
213
		if(count($this->_table_names) == 0) {
214
			throw new exception('Specify at least 1 table name');
215
		}
216
		if(count($this->_write_fields) == 0) {
217
			throw new exception('Specify at least 1 write field (set function)');
218
		}
219
        $table = $this->_table_names[0];
220
        $fields = '`'.implode('`, `', $this->_write_fields).'`';
221
        $values = array();
222
        for ($d = 0, $m = count($this->_write_fields); $d < $m; ++$d) {
223
            switch ($this->_write_field_types[$d]) {
224
            case 0:
225
              $values[] = "'".$this->_write_values[$d]."'";
226
              break;
227
            default:
228
              $values[] = $this->_write_values[$d];
229
          }
230
        }
231
        $string = sprintf('insert into %1$s (%2$s) values(%3$s)', $table, $fields, implode(', ', $values));
232
233
        return $string;
234
    }
235
236
    public function getDelete()
237
    {
238
		if(count($this->_table_names) == 0) {
239
			throw new exception('Specify at least 1 table name');
240
		}
241
        $table = $this->_table_names[0];
242
        $where = count($this->_conditions) > 0 ? (' where '.implode(' and ', $this->_conditions)) : null;
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
        $string = sprintf('delete from %1$s %2$s %3$s', $table, $where, $limit);
248
249
        return $string;
250
    }
251
}
252