|
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
|
184 |
|
return $this->indentCsv($cols); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
* Builds the FROM clause. |
|
28
|
|
|
* |
|
29
|
|
|
* @return string |
|
30
|
|
|
* |
|
31
|
|
|
*/ |
|
32
|
184 |
|
public function buildFrom($from, $join) |
|
33
|
|
|
{ |
|
34
|
184 |
|
if (empty($from)) { |
|
35
|
60 |
|
return ''; // not applicable |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
124 |
|
$refs = array(); |
|
39
|
124 |
|
foreach ($from as $from_key => $from) { |
|
40
|
124 |
|
if (isset($join[$from_key])) { |
|
41
|
55 |
|
$from = array_merge($from, $join[$from_key]); |
|
42
|
|
|
} |
|
43
|
124 |
|
$refs[] = implode(PHP_EOL, $from); |
|
44
|
|
|
} |
|
45
|
124 |
|
return PHP_EOL . 'FROM' . $this->indentCsv($refs); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* |
|
50
|
|
|
* Builds the GROUP BY clause. |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
* |
|
54
|
|
|
*/ |
|
55
|
184 |
|
public function buildGroupBy($group_by) |
|
56
|
|
|
{ |
|
57
|
184 |
|
if (empty($group_by)) { |
|
58
|
179 |
|
return ''; // not applicable |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
5 |
|
return PHP_EOL . 'GROUP BY' . $this->indentCsv($group_by); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* |
|
66
|
|
|
* Builds the HAVING clause. |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
* |
|
70
|
|
|
*/ |
|
71
|
184 |
|
public function buildHaving($having) |
|
72
|
|
|
{ |
|
73
|
184 |
|
if (empty($having)) { |
|
74
|
169 |
|
return ''; // not applicable |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
15 |
|
return PHP_EOL . 'HAVING' . $this->indent($having); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* |
|
82
|
|
|
* Builds the FOR UPDATE clause. |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
* |
|
86
|
|
|
*/ |
|
87
|
184 |
|
public function buildForUpdate($for_update) |
|
88
|
|
|
{ |
|
89
|
184 |
|
if (! $for_update) { |
|
90
|
179 |
|
return ''; // not applicable |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
5 |
|
return PHP_EOL . 'FOR UPDATE'; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|