1
|
|
|
<?php |
2
|
|
|
namespace Aura\SqlQuery\Common; |
3
|
|
|
|
4
|
|
|
use Aura\SqlQuery\Exception; |
5
|
|
|
|
6
|
|
|
class SelectBuilder extends AbstractBuilder |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* |
10
|
|
|
* Builds the columns clause. |
11
|
|
|
* |
12
|
|
|
* @return string |
13
|
|
|
* |
14
|
|
|
* @throws Exception when there are no columns in the SELECT. |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
189 |
|
public function buildCols($cols) |
18
|
|
|
{ |
19
|
189 |
|
if (empty($cols)) { |
20
|
5 |
|
throw new Exception('No columns in the SELECT.'); |
21
|
|
|
} |
22
|
|
|
|
23
|
184 |
|
$list = array(); |
24
|
184 |
|
foreach ($cols as $key => $val) { |
25
|
184 |
|
if (is_int($key)) { |
26
|
184 |
|
$list[] = $this->quoter->quoteNamesIn($val); |
|
|
|
|
27
|
184 |
|
} else { |
28
|
10 |
|
$list[] = $this->quoter->quoteNamesIn("$val AS $key"); |
29
|
|
|
} |
30
|
184 |
|
} |
31
|
|
|
|
32
|
184 |
|
return $this->indentCsv($list); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* |
37
|
|
|
* Builds the FROM clause. |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
184 |
|
public function buildFrom($from, $join) |
43
|
|
|
{ |
44
|
184 |
|
if (empty($from)) { |
45
|
60 |
|
return ''; // not applicable |
46
|
|
|
} |
47
|
|
|
|
48
|
124 |
|
$refs = array(); |
49
|
124 |
|
foreach ($from as $from_key => $from) { |
50
|
124 |
|
if (isset($join[$from_key])) { |
51
|
55 |
|
$from = array_merge($from, $join[$from_key]); |
52
|
55 |
|
} |
53
|
124 |
|
$refs[] = implode(PHP_EOL, $from); |
54
|
124 |
|
} |
55
|
124 |
|
return PHP_EOL . 'FROM' . $this->indentCsv($refs); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* |
60
|
|
|
* Builds the GROUP BY clause. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
* |
64
|
|
|
*/ |
65
|
184 |
|
public function buildGroupBy($group_by) |
66
|
|
|
{ |
67
|
184 |
|
if (empty($group_by)) { |
68
|
179 |
|
return ''; // not applicable |
69
|
|
|
} |
70
|
|
|
|
71
|
5 |
|
return PHP_EOL . 'GROUP BY' . $this->indentCsv($group_by); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* |
76
|
|
|
* Builds the HAVING clause. |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
* |
80
|
|
|
*/ |
81
|
184 |
|
public function buildHaving($having) |
82
|
|
|
{ |
83
|
184 |
|
if (empty($having)) { |
84
|
169 |
|
return ''; // not applicable |
85
|
|
|
} |
86
|
|
|
|
87
|
15 |
|
return PHP_EOL . 'HAVING' . $this->indent($having); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* |
92
|
|
|
* Builds the FOR UPDATE clause. |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
* |
96
|
|
|
*/ |
97
|
184 |
|
public function buildForUpdate($for_update) |
98
|
|
|
{ |
99
|
184 |
|
if (! $for_update) { |
100
|
179 |
|
return ''; // not applicable |
101
|
|
|
} |
102
|
|
|
|
103
|
5 |
|
return PHP_EOL . 'FOR UPDATE'; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: