Query   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 360
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 82.11%

Importance

Changes 11
Bugs 0 Features 6
Metric Value
c 11
b 0
f 6
dl 0
loc 360
ccs 78
cts 95
cp 0.8211
rs 9.2
wmc 34
lcom 1
cbo 1

29 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A sql() 0 10 2
A table() 0 4 1
A setTable() 0 4 1
A select() 0 6 1
A insert() 0 7 1
A update() 0 7 1
A delete() 0 5 1
A whereQ() 0 7 2
A where() 0 5 1
A andWhere() 0 4 1
A orWhere() 0 4 1
A like() 0 4 1
A andLike() 0 4 1
A orLike() 0 4 1
A between() 0 8 2
A andBetween() 0 4 1
A orBetween() 0 4 1
A sort() 0 9 1
A asc() 0 4 1
A desc() 0 4 1
A limit() 0 7 1
A build() 0 12 3
A clear() 0 5 1
A __get() 0 4 1
A __set() 0 4 1
A getType() 0 4 1
A setType() 0 4 1
A initColVal() 0 5 1
1
<?php
2
namespace JayaCode\Framework\Core\Database\Query;
3
4
use JayaCode\Framework\Core\Database\Query\Grammar\Grammar;
5
6
/**
7
 * Class Query
8
 * @property array where
9
 * @property array columns
10
 * @property mixed query
11
 * @property null params
12
 * @property array values
13
 * @property array sort
14
 * @property array offset
15
 * @property array limit
16
 * @package JayaCode\Framework\Core\Database\Query
17
 */
18
class Query
19
{
20
    /**
21
     *
22
     */
23
    const TYPE_QUERY = 'QUERY';
24
    /**
25
     *
26
     */
27
    const TYPE_SELECT = 'SELECT';
28
29
    /**
30
     *
31
     */
32
    const TYPE_INSERT = 'INSERT';
33
34
    /**
35
     *
36
     */
37
    const TYPE_UPDATE = 'UPDATE';
38
39
    /**
40
     *
41
     */
42
    const TYPE_DELETE = 'DELETE';
43
44
    /**
45
     * @var string
46
     */
47
    public $table;
48
49
    /**
50
     * @var array
51
     */
52
    protected $attributes = array(
53
        'where' => array(),
54
        'columns' => array()
55
    );
56
57
    /**
58
     * @var string
59
     */
60
    protected $type = "SELECT";
61
62
    /**
63
     * @param null $table
64
     */
65 45
    public function __construct($table = null)
66
    {
67 45
        $this->table = $table;
68 45
    }
69
70
    /**
71
     * @param $queryStr
72
     * @param $params
73
     * @return Query
74
     */
75 45
    public static function sql($queryStr, $params = null)
76
    {
77 45
        $query = new self();
78 45
        if ($params) {
79
            $query->params = $params;
80
        }
81 45
        $query->setType(self::TYPE_QUERY);
82 45
        $query->query = $queryStr;
83 45
        return $query;
84
    }
85
86
    /**
87
     * @param $table
88
     * @return Query
89
     */
90 3
    public static function table($table)
91
    {
92 3
        return new self($table);
93
    }
94
95
    /**
96
     * @param $table
97
     */
98
    public function setTable($table)
99
    {
100
        $this->table = $table;
101
    }
102
103
    /**
104
     * @param $columns
105
     * @return $this
106
     */
107 3
    public function select($columns = null)
108
    {
109 3
        $this->attributes['columns'] = $columns;
110 3
        $this->type = Query::TYPE_SELECT;
111 3
        return $this;
112
    }
113
114
    /**
115
     * @param array $columnsVal
116
     * @return $this
117
     */
118 3
    public function insert(array $columnsVal)
119
    {
120 3
        $this->type = Query::TYPE_INSERT;
121 3
        $this->initColVal($columnsVal);
122
123 3
        return $this;
124
    }
125
126
127
    /**
128
     * @param $columnsVal
129
     * @return $this
130
     */
131 3
    public function update($columnsVal)
132
    {
133 3
        $this->type = Query::TYPE_UPDATE;
134 3
        $this->initColVal($columnsVal);
135
136 3
        return $this;
137
    }
138
139
    /**
140
     * @return $this
141
     */
142 3
    public function delete()
143
    {
144 3
        $this->type = Query::TYPE_DELETE;
145 3
        return $this;
146
    }
147
148
    /**
149
     * @param $query
150
     * @param string $type
151
     * @return Query
152
     */
153 3
    public function whereQ(Query $query, $type = "AND")
154
    {
155 3
        if ($query->getType() == Query::TYPE_QUERY) {
156 3
            $this->attributes['where'][] = array($type, $query->query);
157 3
        }
158 3
        return $this;
159
    }
160
161
    /**
162
     * @param $column
163
     * @param $value
164
     * @param string $operator
165
     * @param string $type
166
     * @return Query
167
     */
168 3
    public function where($column, $value, $operator = "=", $type = "AND")
169
    {
170 3
        $this->attributes['where'][] = array($type, array($column, $operator, $value));
171 3
        return $this;
172
    }
173
174
    /**
175
     * @param $column
176
     * @param $value
177
     * @param string $operator
178
     * @return Query
179
     */
180 3
    public function andWhere($column, $value, $operator = "=")
181
    {
182 3
        return $this->where($column, $value, $operator, "AND");
183
    }
184
185
    /**
186
     * @param $column
187
     * @param $value
188
     * @param string $operator
189
     * @return Query
190
     */
191 3
    public function orWhere($column, $value, $operator = "=")
192
    {
193 3
        return $this->where($column, $value, $operator, "OR");
194
    }
195
196
    /**
197
     * @param $column
198
     * @param $value
199
     * @param string $type
200
     * @return Query
201
     */
202 3
    public function like($column, $value, $type = "AND")
203
    {
204 3
        return $this->where($column, $value, "LIKE", $type);
205
    }
206
207
    /**
208
     * @param $column
209
     * @param $value
210
     * @return Query
211
     */
212 3
    public function andLike($column, $value)
213
    {
214 3
        return $this->like($column, $value);
215
    }
216
217
    /**
218
     * @param $column
219
     * @param $value
220
     * @return Query
221
     */
222 3
    public function orLike($column, $value)
223
    {
224 3
        return $this->like($column, $value, "OR");
225
    }
226
227
    /**
228
     * @param $column
229
     * @param array $value
230
     * @param string $type
231
     * @return Query
232
     */
233
    public function between($column, $value = array(), $type = "AND")
234
    {
235
        if (count($value) != 2) {
236
            throw new \OutOfBoundsException();
237
        }
238
239
        return $this->where($column, $value, "BETWEEN", $type);
240
    }
241
242
    /**
243
     * @param $column
244
     * @param array $value
245
     * @return Query
246
     */
247
    public function andBetween($column, $value = array())
248
    {
249
        return $this->between($column, $value);
250
    }
251
252
    /**
253
     * @param $column
254
     * @param $value
255
     * @return Query
256
     */
257
    public function orBetween($column, $value)
258
    {
259
        return $this->between($column, $value, "OR");
260
    }
261
262
    /**
263
     * @param $column
264
     * @param $order
265
     * @return $this
266
     */
267 3
    public function sort($column, $order)
268
    {
269 3
        $this->attributes['sort'] = array(
270 3
            'column' => $column,
271
            'order' => $order
272 3
        );
273
274 3
        return $this;
275
    }
276
277
    /**
278
     * @param $column
279
     * @return Query
280
     */
281 3
    public function asc($column)
282
    {
283 3
        return $this->sort($column, "ASC");
284
    }
285
286
    /**
287
     * @param $column
288
     * @return Query
289
     */
290 3
    public function desc($column)
291
    {
292 3
        return $this->sort($column, "DESC");
293
    }
294
295
    /**
296
     * @param $num
297
     * @param null $offset
298
     * @return $this
299
     */
300 3
    public function limit($num, $offset = null)
301
    {
302 3
        $this->attributes['limit'] = $num;
303 3
        $this->attributes['offset'] = $offset;
304
        
305 3
        return $this;
306
    }
307
308
    /**
309
     * @param Grammar $grammar
310
     * @return array
311
     */
312 60
    public function build(Grammar $grammar)
313
    {
314 60
        $grammar->setQuery($this);
315
316 60
        $queryStr = $grammar->build();
317
318 60
        $queryParams = isset($this->attributes['params']) && !empty($this->attributes['params'])?
319 60
            $this->attributes['params']
320 60
            :$grammar->getParams();
321
322 60
        return [$queryStr, $queryParams];
323
    }
324
325
    /**
326
     * Clear Query Builder
327
     */
328
    public function clear()
329
    {
330
        $this->attributes = array();
331
        $this->params = array();
332
    }
333
334
    /**
335
     * @param $name
336
     * @return array
337
     */
338 63
    public function __get($name)
339
    {
340 63
        return arr_get($this->attributes, $name);
341
    }
342
343
    /**
344
     * @param $name
345
     * @param $value
346
     */
347 45
    public function __set($name, $value)
348
    {
349 45
        $this->attributes[$name] = $value;
350 45
    }
351
352
    /**
353
     * @return string
354
     */
355 63
    public function getType()
356
    {
357 63
        return $this->type;
358
    }
359
360
    /**
361
     * @param string $type
362
     */
363 45
    public function setType($type)
364
    {
365 45
        $this->type = $type;
366 45
    }
367
368
    /**
369
     * init col and val to attributes
370
     * @param $columnsVal
371
     */
372 3
    public function initColVal($columnsVal)
373
    {
374 3
        $this->attributes['columns'] = array_keys($columnsVal);
375 3
        $this->attributes['values'] = array_values($columnsVal);
376 3
    }
377
}
378