|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Muffin\Queries; |
|
4
|
|
|
|
|
5
|
|
|
use Muffin\Query; |
|
6
|
|
|
use Muffin\Condition; |
|
7
|
|
|
use Muffin\Traits\EscaperAware; |
|
8
|
|
|
use Muffin\Snippet; |
|
9
|
|
|
use Muffin\Queries\Snippets\Builders; |
|
10
|
|
|
use Muffin\Queries\Snippets\Having; |
|
11
|
|
|
|
|
12
|
|
|
class Select implements Query |
|
13
|
|
|
{ |
|
14
|
|
|
use |
|
15
|
|
|
EscaperAware, |
|
16
|
|
|
Builders\Join, |
|
17
|
|
|
Builders\Where, |
|
18
|
|
|
Builders\GroupBy, |
|
19
|
|
|
Builders\OrderBy, |
|
20
|
|
|
Builders\Limit; |
|
21
|
|
|
|
|
22
|
|
|
private |
|
23
|
|
|
$select, |
|
24
|
|
|
$from, |
|
25
|
|
|
$having; |
|
26
|
|
|
|
|
27
|
23 |
|
public function __construct($columns = array()) |
|
28
|
|
|
{ |
|
29
|
23 |
|
$this->select = new Snippets\Select(); |
|
30
|
23 |
|
$this->where = new Snippets\Where(); |
|
31
|
23 |
|
$this->groupBy = new Snippets\GroupBy(); |
|
32
|
23 |
|
$this->having = new Snippets\Having(); |
|
33
|
23 |
|
$this->orderBy = new Snippets\OrderBy(); |
|
34
|
|
|
|
|
35
|
23 |
|
$this->select->select($columns); |
|
36
|
23 |
|
} |
|
37
|
|
|
|
|
38
|
22 |
|
public function toString() |
|
39
|
|
|
{ |
|
40
|
|
|
$queryParts = array( |
|
41
|
22 |
|
$this->buildSelect(), |
|
42
|
20 |
|
$this->buildFrom(), |
|
43
|
19 |
|
$this->buildJoin(), |
|
44
|
19 |
|
$this->buildWhere($this->escaper), |
|
45
|
19 |
|
$this->buildGroupBy(), |
|
46
|
19 |
|
$this->buildHaving(), |
|
47
|
19 |
|
$this->buildOrderBy(), |
|
48
|
19 |
|
$this->buildLimit(), |
|
49
|
19 |
|
); |
|
50
|
|
|
|
|
51
|
19 |
|
return implode(' ', array_filter($queryParts)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
21 |
|
public function from($table, $alias = null) |
|
55
|
|
|
{ |
|
56
|
21 |
|
$this->from = new Snippets\From($table, $alias); |
|
57
|
|
|
|
|
58
|
21 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
19 |
|
public function select($columns) |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
19 |
|
$this->select->select($columns); |
|
64
|
|
|
|
|
65
|
19 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
public function having(Condition $condition) |
|
69
|
|
|
{ |
|
70
|
1 |
|
$this->having->having($condition); |
|
71
|
|
|
|
|
72
|
1 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
22 |
|
private function buildSelect() |
|
76
|
|
|
{ |
|
77
|
22 |
|
return $this->select->toString(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
20 |
|
private function buildFrom() |
|
81
|
|
|
{ |
|
82
|
20 |
|
if(!$this->from instanceof Snippet) |
|
83
|
20 |
|
{ |
|
84
|
1 |
|
throw new \LogicException('No column for FROM clause'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
19 |
|
return $this->from->toString(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
19 |
|
private function buildHaving() |
|
91
|
|
|
{ |
|
92
|
19 |
|
$this->having->setEscaper($this->escaper); |
|
93
|
|
|
|
|
94
|
19 |
|
return $this->having->toString(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|