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