1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BfwSql\Queries\SGBD; |
4
|
|
|
|
5
|
|
|
use \BfwSql\Queries\AbstractQuery; |
6
|
|
|
|
7
|
|
|
abstract class AbstractSGBD |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var \BfwSql\Queries\AbstractQuery $querySystem The object who generate |
11
|
|
|
* the full query |
12
|
|
|
*/ |
13
|
|
|
protected $querySystem; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Define querySystem property |
17
|
|
|
* |
18
|
|
|
* @param \BfwSql\Queries\AbstractQuery $querySystem |
19
|
|
|
*/ |
20
|
|
|
public function __construct(AbstractQuery $querySystem) |
21
|
|
|
{ |
22
|
|
|
$this->querySystem = $querySystem; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Getter accessor to property querySystem |
27
|
|
|
* |
28
|
|
|
* @return \BfwSql\Queries\AbstractQuery |
29
|
|
|
*/ |
30
|
|
|
public function getQuerySystem(): AbstractQuery |
31
|
|
|
{ |
32
|
|
|
return $this->querySystem; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Return the current request type |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
protected function obtainRequestType(): string |
41
|
|
|
{ |
42
|
|
|
return $this->querySystem->getRequestType(); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Define queries part to disable |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
protected function obtainPartsToDisable(): array |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
'delete' => [], |
54
|
|
|
'insert' => [], |
55
|
|
|
'select' => [], |
56
|
|
|
'update' => [] |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Disable queries part define by method obtainPartsToDisable() |
62
|
|
|
* |
63
|
|
|
* @param array &$queriesParts List of queries part |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function disableQueriesParts(array &$queriesParts) |
68
|
|
|
{ |
69
|
|
|
$requestType = $this->obtainRequestType(); |
70
|
|
|
$toDisable = $this->obtainPartsToDisable(); |
71
|
|
|
|
72
|
|
|
if (!isset($toDisable[$requestType])) { |
73
|
|
|
$toDisable[$requestType] = []; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (empty($toDisable[$requestType])) { |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
foreach ($toDisable[$requestType] as $partName) { |
81
|
|
|
//Maybe called from AbstractQuery at this time |
82
|
|
|
if (!isset($queriesParts[$partName])) { |
83
|
|
|
continue; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$queriesParts[$partName]->setIsDisabled(true); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Generate an item in a list |
92
|
|
|
* |
93
|
|
|
* @param string $expr The item expression |
94
|
|
|
* @param int $index The index of the item in the list |
95
|
|
|
* @param string $separator The separator to use between items |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function listItem( |
100
|
|
|
string $expr, |
101
|
|
|
int $index, |
102
|
|
|
string $separator |
103
|
|
|
): string { |
104
|
|
|
$partSeparator = ''; |
105
|
|
|
if ($index > 0) { |
106
|
|
|
$partSeparator .= $separator; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $partSeparator.$expr; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Generate the column name |
114
|
|
|
* |
115
|
|
|
* @param string $colName The column name |
116
|
|
|
* @param string $tableName The table name of the column |
117
|
|
|
* @param bool $isFunction If the expr in colName is a function |
118
|
|
|
* @param bool $isJoker If the colName is the joker |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function columnName( |
123
|
|
|
string $colName, |
124
|
|
|
string $tableName, |
125
|
|
|
bool $isFunction, |
126
|
|
|
bool $isJoker |
127
|
|
|
): string { |
128
|
|
|
if ($isFunction === true) { |
129
|
|
|
return $colName; |
130
|
|
|
} elseif ($isJoker) { |
131
|
|
|
return '`'.$tableName.'`.'.$colName; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return '`'.$tableName.'`.`'.$colName.'`'; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Generate the JOIN (left|right) part |
139
|
|
|
* |
140
|
|
|
* @param string $tableName The table name |
141
|
|
|
* @param string|null $shortcut The shortcut for the table |
142
|
|
|
* @param string $on The on condition |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function join(string $tableName, $shortcut, string $on): string |
147
|
|
|
{ |
148
|
|
|
$partQuery = '`'.$tableName.'`'; |
149
|
|
|
if ($shortcut !== null) { |
150
|
|
|
$partQuery .= ' AS `'.$shortcut.'`'; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$partQuery .= ' ON '.$on; |
154
|
|
|
|
155
|
|
|
return $partQuery; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Generate the LIMIT part |
160
|
|
|
* |
161
|
|
|
* @param int|null $rowCount The maximum number of rows to return |
162
|
|
|
* @param int|null $offset The offset of the first row to return |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public function limit($rowCount, $offset): string |
167
|
|
|
{ |
168
|
|
|
if ($rowCount === null) { |
169
|
|
|
return ''; |
170
|
|
|
} else if ($offset === null) { |
171
|
|
|
return (string) $rowCount; |
172
|
|
|
} else { |
173
|
|
|
return $offset.', '.$rowCount; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Generate the ORDER part |
179
|
|
|
* |
180
|
|
|
* @param string $expr The expression to use into the order |
181
|
|
|
* @param string|null $sort The sort order : ASC or DESC |
182
|
|
|
* @param bool $isFunction If the expression contain a function |
183
|
|
|
* |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
public function order(string $expr, $sort, bool $isFunction): string |
187
|
|
|
{ |
188
|
|
|
if ($isFunction === false) { |
189
|
|
|
$expr = '`'.$expr.'`'; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if ($sort === null) { |
193
|
|
|
return $expr; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $expr.' '.$sort; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Generate the part for a sub-query |
201
|
|
|
* |
202
|
|
|
* @param string $subQuery The request for the sub-query |
203
|
|
|
* @param string $shortcut The sub-query return shortcut |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
public function subQuery(string $subQuery, string $shortcut): string |
208
|
|
|
{ |
209
|
|
|
return '('.$subQuery.') AS `'.$shortcut.'`'; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Generate the part for a table |
214
|
|
|
* |
215
|
|
|
* @param string $name The table name |
216
|
|
|
* @param string|null $shortcut The table shortcut |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
public function table(string $name, $shortcut): string |
221
|
|
|
{ |
222
|
|
|
$partQuery = '`'.$name.'`'; |
223
|
|
|
|
224
|
|
|
if ($shortcut !== null) { |
225
|
|
|
$partQuery .= ' AS `'.$shortcut.'`'; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $partQuery; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|