|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Puzzle\QueryBuilder\Queries; |
|
6
|
|
|
|
|
7
|
|
|
use Puzzle\QueryBuilder\Query; |
|
8
|
|
|
use Puzzle\QueryBuilder\Condition; |
|
9
|
|
|
use Puzzle\QueryBuilder\Traits\EscaperAware; |
|
10
|
|
|
use Puzzle\QueryBuilder\Snippet; |
|
11
|
|
|
use Puzzle\QueryBuilder\Queries\Snippets\Builders; |
|
12
|
|
|
use Puzzle\QueryBuilder\Queries\Snippets\Having; |
|
13
|
|
|
|
|
14
|
|
|
class Select implements Query |
|
15
|
|
|
{ |
|
16
|
|
|
use |
|
17
|
|
|
EscaperAware, |
|
18
|
|
|
Builders\Join, |
|
19
|
|
|
Builders\Where, |
|
20
|
|
|
Builders\GroupBy, |
|
21
|
|
|
Builders\OrderBy, |
|
22
|
|
|
Builders\Limit; |
|
23
|
|
|
|
|
24
|
|
|
private |
|
25
|
|
|
$select, |
|
|
|
|
|
|
26
|
|
|
$from, |
|
27
|
|
|
$having; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param array[string|Selectable] | string|Selectable $columns |
|
31
|
|
|
*/ |
|
32
|
23 |
|
public function __construct($columns = []) |
|
33
|
|
|
{ |
|
34
|
23 |
|
$this->select = new Snippets\Select(); |
|
35
|
23 |
|
$this->where = new Snippets\Where(); |
|
36
|
23 |
|
$this->groupBy = new Snippets\GroupBy(); |
|
37
|
23 |
|
$this->having = new Snippets\Having(); |
|
38
|
23 |
|
$this->orderBy = new Snippets\OrderBy(); |
|
39
|
|
|
|
|
40
|
23 |
|
$this->select($columns); |
|
41
|
23 |
|
} |
|
42
|
|
|
|
|
43
|
22 |
|
public function toString(): string |
|
44
|
|
|
{ |
|
45
|
|
|
$queryParts = array( |
|
46
|
22 |
|
$this->buildSelect(), |
|
47
|
20 |
|
$this->buildFrom(), |
|
48
|
19 |
|
$this->buildJoin(), |
|
49
|
19 |
|
$this->buildWhere($this->escaper), |
|
50
|
19 |
|
$this->buildGroupBy(), |
|
51
|
19 |
|
$this->buildHaving(), |
|
52
|
19 |
|
$this->buildOrderBy(), |
|
53
|
19 |
|
$this->buildLimit(), |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
19 |
|
return implode(' ', array_filter($queryParts)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param Snippets\TableName|string $table |
|
61
|
|
|
*/ |
|
62
|
21 |
|
public function from($table, ?string $alias = null): self |
|
63
|
|
|
{ |
|
64
|
21 |
|
$this->from = new Snippets\From($table, $alias); |
|
65
|
|
|
|
|
66
|
21 |
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param array[string|Selectable] | string|Selectable $columns |
|
71
|
|
|
*/ |
|
72
|
23 |
|
public function select($columns): self |
|
73
|
|
|
{ |
|
74
|
23 |
|
$this->select->select($columns); |
|
75
|
|
|
|
|
76
|
23 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
public function having(Condition $condition): self |
|
80
|
|
|
{ |
|
81
|
1 |
|
$this->having->having($condition); |
|
82
|
|
|
|
|
83
|
1 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
22 |
|
private function buildSelect(): string |
|
87
|
|
|
{ |
|
88
|
22 |
|
return $this->select->toString(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
20 |
|
private function buildFrom(): string |
|
92
|
|
|
{ |
|
93
|
20 |
|
if(!$this->from instanceof Snippet) |
|
94
|
|
|
{ |
|
95
|
1 |
|
throw new \LogicException('No column for FROM clause'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
19 |
|
return $this->from->toString(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
19 |
|
private function buildHaving(): string |
|
102
|
|
|
{ |
|
103
|
19 |
|
$this->having->setEscaper($this->escaper); |
|
104
|
|
|
|
|
105
|
19 |
|
return $this->having->toString(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.