|
1
|
|
|
<?php |
|
2
|
|
|
namespace Aura\SqlQuery\Common; |
|
3
|
|
|
|
|
4
|
|
|
use Aura\SqlQuery\Exception; |
|
5
|
|
|
|
|
6
|
|
|
abstract class AbstractBuilder |
|
7
|
|
|
{ |
|
8
|
408 |
|
public function __construct($quoter) |
|
9
|
|
|
{ |
|
10
|
408 |
|
$this->quoter = $quoter; |
|
|
|
|
|
|
11
|
408 |
|
} |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* |
|
15
|
|
|
* Builds the flags as a space-separated string. |
|
16
|
|
|
* |
|
17
|
|
|
* @return string |
|
18
|
|
|
* |
|
19
|
|
|
*/ |
|
20
|
258 |
|
public function buildFlags($flags) |
|
21
|
|
|
{ |
|
22
|
258 |
|
if (empty($flags)) { |
|
23
|
220 |
|
return ''; // not applicable |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
38 |
|
return ' ' . implode(' ', array_keys($flags)); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* |
|
31
|
|
|
* Builds the `WHERE` clause of the statement. |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
* |
|
35
|
|
|
*/ |
|
36
|
211 |
|
public function buildWhere($where) |
|
37
|
|
|
{ |
|
38
|
211 |
|
if (empty($where)) { |
|
39
|
156 |
|
return ''; // not applicable |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
60 |
|
return PHP_EOL . 'WHERE' . $this->indent($where); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* |
|
47
|
|
|
* Builds the `ORDER BY ...` clause of the statement. |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
* |
|
51
|
|
|
*/ |
|
52
|
211 |
|
public function buildOrderBy($order_by) |
|
53
|
|
|
{ |
|
54
|
211 |
|
if (empty($order_by)) { |
|
55
|
202 |
|
return ''; // not applicable |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
9 |
|
return PHP_EOL . 'ORDER BY' . $this->indentCsv($order_by); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* |
|
63
|
|
|
* Builds the `LIMIT` clause of the statement. |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
* |
|
67
|
|
|
*/ |
|
68
|
9 |
|
public function buildLimit($limit) |
|
69
|
|
|
{ |
|
70
|
9 |
|
if (empty($limit)) { |
|
71
|
5 |
|
return ''; |
|
72
|
|
|
} |
|
73
|
4 |
|
return PHP_EOL . "LIMIT {$limit}"; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* |
|
78
|
|
|
* Builds the `LIMIT ... OFFSET` clause of the statement. |
|
79
|
|
|
* |
|
80
|
|
|
* @return string |
|
81
|
|
|
* |
|
82
|
|
|
*/ |
|
83
|
159 |
|
public function buildLimitOffset($limit, $offset) |
|
84
|
|
|
{ |
|
85
|
159 |
|
$clause = ''; |
|
86
|
|
|
|
|
87
|
159 |
|
if (!empty($limit)) { |
|
88
|
15 |
|
$clause .= "LIMIT {$limit}"; |
|
89
|
15 |
|
} |
|
90
|
|
|
|
|
91
|
159 |
|
if (!empty($offset)) { |
|
92
|
10 |
|
$clause .= " OFFSET {$offset}"; |
|
93
|
10 |
|
} |
|
94
|
|
|
|
|
95
|
159 |
|
if (!empty($clause)) { |
|
96
|
15 |
|
$clause = PHP_EOL . trim($clause); |
|
97
|
15 |
|
} |
|
98
|
|
|
|
|
99
|
159 |
|
return $clause; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* |
|
104
|
|
|
* Builds the `RETURNING` clause of the statement. |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
* |
|
108
|
|
|
*/ |
|
109
|
11 |
|
public function buildReturning($returning) |
|
110
|
|
|
{ |
|
111
|
11 |
|
if (empty($returning)) { |
|
112
|
8 |
|
return ''; // not applicable |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
3 |
|
return PHP_EOL . 'RETURNING' . $this->indentCsv($returning); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* |
|
120
|
|
|
* Returns an array as an indented comma-separated values string. |
|
121
|
|
|
* |
|
122
|
|
|
* @param array $list The values to convert. |
|
123
|
|
|
* |
|
124
|
|
|
* @return string |
|
125
|
|
|
* |
|
126
|
|
|
*/ |
|
127
|
225 |
|
public function indentCsv(array $list) |
|
128
|
|
|
{ |
|
129
|
225 |
|
return PHP_EOL . ' ' |
|
130
|
225 |
|
. implode(',' . PHP_EOL . ' ', $list); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* |
|
135
|
|
|
* Returns an array as an indented string. |
|
136
|
|
|
* |
|
137
|
|
|
* @param array $list The values to convert. |
|
138
|
|
|
* |
|
139
|
|
|
* @return string |
|
140
|
|
|
* |
|
141
|
|
|
*/ |
|
142
|
71 |
|
public function indent(array $list) |
|
143
|
|
|
{ |
|
144
|
71 |
|
return PHP_EOL . ' ' |
|
145
|
71 |
|
. implode(PHP_EOL . ' ', $list); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
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: