Completed
Push — v2 ( f1b86f...9a6357 )
by Berend
01:53
created

QueryBuilder::limit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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