QueryBuilder::getUpdateQuery()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 13
ccs 3
cts 3
cp 1
crap 3
rs 10
1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 */
9
10
namespace miBadger\Query;
11
12
/**
13
 * The query builder class.
14
 *
15
 * @since 1.0.0
16
 */
17
class QueryBuilder
18
{
19
	const SELECT = 'SELECT';
20
	const INSERT = 'INSERT INTO';
21
	const UPDATE = 'UPDATE';
22
	const DELETE = 'DELETE';
23
	
24
	/* @var string The modifier. SELECT, INSERT INTO, UPDATE or DELETE */
25
	private $modifier;
26
27
	/* @var string The table name. */
28
	private $table;
29
30
	/* @var array The columns. */
31
	private $columns;
32
33
	/* @var array The values. */
34
	private $values;
35
36
	/* @var array The join conditions. */
37
	private $join;
38
39
	/* @var QueryExpression The where clause. */
40
	public $where;
41
42
	/* @var array The group by conditions. */
43
	private $groupBy;
44
45
	/* @var array The order by conditions. */
46
	private $orderBy;
47
48
	/* @var string The limit. */
49
	private $limit;
50
51
	/* @var string The offset. */
52
	private $offset;
53
54 45
	/* @var QueryExpression The having clause. */
55
	public $having;
56 45
57 45
	/**
58 45
	 * Construct a query builder object with the given table.
59 45
	 *
60 45
	 * @param string $table
61 45
	 */
62
	public function __construct($table)
63
	{
64
		$this->table = $table;
65
		$this->columns = [];
66
		$this->values = [];
67
		$this->join = [];
68 45
		$this->where = null;
69
		$this->groupBy = [];
70 45
		$this->orderBy = [];
71 45
		$this->limit = null;
72 33
		$this->offset = null;
73
		$this->having = null;
74 12
	}
75 3
76
	/**
77 9
	 * Returns a string representation of the query object.
78 4
	 *
79
	 * @return string a string representation of the query object.
80 5
	 */
81 3
	public function __toString()
82
	{
83
		switch ($this->modifier) {
84 2
			case self::SELECT:
85
				return $this->getSelectQuery();
86
87
			case self::INSERT:
88
				return $this->getInsertQuery();
89
90
			case self::UPDATE:
91 33
				return $this->getUpdateQuery();
92
93 33
			case self::DELETE:
94 33
				return $this->getDeleteQuery();
95
96 33
			default:
97
				return '';
98
		}
99
	}
100
101
	/**
102
	 * {@inheritdoc}
103
	 */
104 33
	public function select($columns = ['*'])
105
	{
106 33
		$this->columns = is_array($columns) ? $columns : func_get_args();
107
		$this->modifier = self::SELECT;
108
109
		return $this;
110
	}
111
112
	/**
113
	 * Returns the select clause.
114 33
	 *
115
	 * @return string the select clause.
116 33
	 */
117
	private function getSelectClause(): string
118 33
	{
119 8
		return sprintf('SELECT %s FROM %s', implode(', ', $this->columns), $this->table);
120
	}
121
122 33
	/**
123 9
	 * Returns the select query.
124
	 *
125
	 * @return string the select query.
126 33
	 */
127 2
	private function getSelectQuery(): string
128
	{
129
		$result = $this->getSelectClause();
130 33
131 4
		if ($join = $this->getJoinClause()) {
132
			$result .= ' ' . $join;
133
		}
134 33
135 4
		if ($where = $this->getWhereClause()) {
136
			$result .= ' ' . $where;
137
		}
138 33
139 2
		if ($groupBy = $this->getGroupByClause()) {
140
			$result .= ' ' . $groupBy;
141
		}
142 33
143
		if ($having = $this->getHavingClause()) {
144
			$result .= ' ' . $having;
145
		}
146
147
		if ($orderBy = $this->getOrderByClause()) {
148 3
			$result .= ' ' . $orderBy;
149
		}
150 3
151 3
		if ($limit = $this->getLimitClause()) {
152
			$result .= ' ' . $limit;
153 3
		}
154
155
		if ($offset = $this->getOffsetClause()) {
156
			$result .= ' ' . $offset;
157
		}
158
159
		return $result;
160
	}
161 3
162
	/**
163 3
	 * {@inheritdoc}
164 3
	 */
165
	public function insert(array $values)
166 3
	{
167 3
		$this->values = $values;
168 3
		$this->modifier = self::INSERT;
169
170
		return $this;
171 3
	}
172
173
	/**
174
	 * Returns the insert clause.
175
	 *
176
	 * @return string the insert clause.
177
	 */
178
	private function getInsertClause(): string
179 3
	{
180
		$columns = [];
181 3
		$values = [];
182
183
		foreach ($this->values as $key => $value) {
184
			$columns[] = sprintf('`%s`', $key);
185
			$values[] = sprintf('%s', $value);
186
		}
187 4
188
		return sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->table, implode(', ', $columns), implode(', ', $values));
189 4
	}
190 4
191
	/**
192 4
	 * Returns the insert query.
193
	 *
194
	 * @return string the insert query.
195
	 */
196
	private function getInsertQuery(): string
197
	{
198
		return $this->getInsertClause();
199
	}
200 4
201
	/**
202 4
	 * {@inheritdoc}
203
	 */
204 4
	public function update(array $values)
205 4
	{
206
		$this->values = $values;
207
		$this->modifier = self::UPDATE;
208 4
209
		return $this;
210
	}
211
212
	/**
213
	 * Returns the update clause.
214
	 *
215
	 * @return string the update clause.
216 4
	 */
217
	private function getUpdateClause(): string
218 4
	{
219
		$placeholders = [];
220 4
221 4
		foreach ($this->values as $key => $value) {
222
			$placeholders[] = sprintf('`%s` = %s', $key, $value);
223
		}
224 4
225 1
		return sprintf('UPDATE %s SET %s', $this->table, implode(', ', $placeholders));
226
	}
227
228 4
	/**
229
	 * Returns the update query.
230
	 *
231
	 * @return string the update query.
232
	 */
233
	private function getUpdateQuery(): string
234 3
	{
235
		$result = $this->getUpdateClause();
236 3
237
		if ($where = $this->getWhereClause()) {
238 3
			$result .= ' ' . $where;
239
		}
240
241
		if ($limit = $this->getLimitClause()) {
242
			$result .= ' ' . $limit;
243
		}
244
245
		return $result;
246 3
	}
247
248 3
	/**
249
	 * {@inheritdoc}
250
	 */
251
	public function delete()
252
	{
253
		$this->modifier = self::DELETE;
254
255
		return $this;
256 3
	}
257
258
	/**
259 3
	 * Returns the delete clause.
260
	 *
261 3
	 * @return string the delete clause.
262 2
	 */
263
	private function getDeleteClause(): string
264
	{
265 3
		return sprintf('DELETE FROM %s', $this->table);
266 1
	}
267
268
	/**
269 3
	 * Returns the delete query.
270
	 *
271
	 * @return string the delete query.
272
	 */
273
	private function getDeleteQuery(): string
274
	{
275 2
276
		$result = $this->getDeleteClause();
277 2
278
		if ($where = $this->getWhereClause()) {
279 2
			$result .= ' ' . $where;
280
		}
281
282
		if ($limit = $this->getLimitClause()) {
283
			$result .= ' ' . $limit;
284
		}
285 2
286
		return $result;
287 2
	}
288
289 2
	/**
290
	 * {@inheritdoc}
291
	 */
292
	public function join($table, $primary, $operator, $secondary)
293
	{
294
		$this->join[] = ['INNER JOIN', $table, $primary, $operator, $secondary];
295 2
296
		return $this;
297 2
	}
298
299 2
	/**
300
	 * {@inheritdoc}
301
	 */
302
	public function leftJoin($table, $primary, $operator, $secondary)
303
	{
304
		$this->join[] = ['LEFT JOIN', $table, $primary, $operator, $secondary];
305 2
306
		return $this;
307 2
	}
308
309 2
	/**
310
	 * {@inheritdoc}
311
	 */
312 33
	public function rightJoin($table, $primary, $operator, $secondary)
313
	{
314 33
		$this->join[] = ['RIGHT JOIN', $table, $primary, $operator, $secondary];
315
316 33
		return $this;
317 8
	}
318
319
	/**
320 33
	 * {@inheritdoc}
321
	 */
322
	public function crossJoin($table, $primary, $operator, $secondary)
323
	{
324
		$this->join[] = ['CROSS JOIN', $table, $primary, $operator, $secondary];
325
326 15
		return $this;
327
	}
328 15
329
	private function getJoinClause()
330 15
	{
331
		$result = [];
332
333
		foreach ($this->join as $key => $value) {
334
			$result[] = sprintf('%s %s ON %s %s %s', $value[0], $value[1], $value[2], $value[3], $value[4]);
335
		}
336
337
		return implode(' ', $result);
338 40
	}
339
340 40
	/**
341 25
	 * {@inheritdoc}
342
	 */
343
	public function where(QueryExpression $whereClause)
344 15
	{
345
		$this->where = $whereClause;
346 15
347 15
		return $this;
348
	}
349
350 15
	/**
351
	 * Returns the where clause.
352
	 *
353
	 * @return string the where clause.
354
	 */
355
	private function getWhereClause(): string
356
	{
357
		if (empty($this->where)) {
358
			return '';
359
		}
360
361 15
		return sprintf('WHERE %s', (string) $this->where);
362
	}
363 15
364 3
	/**
365
	 * {@inheritdoc}
366 12
	 */
367
	public function having(QueryExpression $havingClause)
368
	{
369
		$this->having = $havingClause;
370
371
		return $this;
372
	}
373 2
374
	/**
375 2
	 * Returns the having clause
376
	 * 
377 2
	 * @return string the having clause
378
	 */
379
	private function getHavingClause(): string
380
	{
381
		if (empty($this->having)) {
382
			return '';
383
		}
384
		return sprintf('HAVING %s', (string) $this->having);
385 33
	}
386
387 33
	/**
388 31
	 * {@inheritdoc}
389
	 */
390
	public function groupBy($column)
391 2
	{
392
		$this->groupBy[] = $column;
393
394
		return $this;
395
	}
396
397 4
	/**
398
	 * Returns the group by clause.
399 4
	 *
400 1
	 * @return string the group by clause.
401 3
	 */
402 2
	private function getGroupByClause(): string
403
	{
404
		if (empty($this->groupBy)) {
405 4
			return '';
406
		}
407 4
408
		return sprintf('GROUP BY %s', implode(', ', $this->groupBy));
409
	}
410
411
	/**
412
	 * {@inheritdoc}
413
	 */
414
	public function orderBy($column, $order = null)
415 33
	{
416
		if (strcasecmp($order, 'asc') == 0) {
417 33
			$column .= ' ASC';
418 29
		} elseif(strcasecmp($order, 'desc') == 0) {
419
			$column .= ' DESC';
420
		}
421 4
422
		$this->orderBy[] = $column;
423
424
		return $this;
425
	}
426
427 6
	/**
428
	 * Returns the order by clause.
429 6
	 *
430
	 * @return string the order by clause.
431 6
	 */
432
	private function getOrderByClause(): string
433
	{
434
		if (empty($this->orderBy)) {
435
			return '';
436
		}
437
438
		return sprintf('ORDER BY %s', implode(', ', $this->orderBy));
439 40
	}
440
441 40
	/**
442 34
	 * {@inheritdoc}
443
	 */
444
	public function limit($limit)
445 6
	{
446
		$this->limit = $limit;
447
448
		return $this;
449
	}
450
451 2
	/**
452
	 * Returns the limit clause.
453 2
	 *
454
	 * @return string the limit clause.
455 2
	 */
456
	private function getLimitClause(): string
457
	{
458
		if (!$this->limit) {
459
			return '';
460
		}
461
462
		return sprintf('LIMIT %s', $this->limit);
463 33
	}
464
465 33
	/**
466 31
	 * {@inheritdoc}
467
	 */
468
	public function offset($offset)
469 2
	{
470
		$this->offset = $offset;
471
472
		return $this;
473
	}
474
475
	/**
476
	 * Returns the offset clause.
477
	 *
478
	 * @return string the offset clause.
479
	 */
480
	private function getOffsetClause(): string
481
	{
482
		if (!$this->limit || !$this->offset) {
483
			return '';
484
		}
485
486
		return sprintf('OFFSET %s', $this->offset);
487
	}
488
}
489