|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Driver\Db; |
|
4
|
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity; |
|
6
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableSelectEntity; |
|
7
|
|
|
use Lagdo\DbAdmin\Driver\Entity\ForeignKeyEntity; |
|
8
|
|
|
|
|
9
|
|
|
use Lagdo\DbAdmin\Driver\DriverInterface; |
|
10
|
|
|
use Lagdo\DbAdmin\Driver\UtilInterface; |
|
11
|
|
|
use Lagdo\DbAdmin\Driver\TranslatorInterface; |
|
12
|
|
|
use Lagdo\DbAdmin\Driver\Db\ConnectionInterface; |
|
13
|
|
|
|
|
14
|
|
|
abstract class Grammar implements GrammarInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var DriverInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $driver; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var UtilInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $util; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var TranslatorInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $trans; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ConnectionInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $connection; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The constructor |
|
38
|
|
|
* |
|
39
|
|
|
* @param DriverInterface $driver |
|
40
|
|
|
* @param UtilInterface $util |
|
41
|
|
|
* @param TranslatorInterface $trans |
|
42
|
|
|
* @param ConnectionInterface $connection |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(DriverInterface $driver, UtilInterface $util, TranslatorInterface $trans, ConnectionInterface $connection) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->driver = $driver; |
|
47
|
|
|
$this->util = $util; |
|
48
|
|
|
$this->trans = $trans; |
|
49
|
|
|
$this->connection = $connection; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritDoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public function escapeId(string $idf) |
|
56
|
|
|
{ |
|
57
|
|
|
return $idf; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @inheritDoc |
|
62
|
|
|
*/ |
|
63
|
|
|
public function unescapeId(string $idf) |
|
64
|
|
|
{ |
|
65
|
|
|
$last = substr($idf, -1); |
|
66
|
|
|
return str_replace($last . $last, $last, substr($idf, 1, -1)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @inheritDoc |
|
71
|
|
|
*/ |
|
72
|
|
|
public function table(string $idf) |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->escapeId($idf); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @inheritDoc |
|
79
|
|
|
*/ |
|
80
|
|
|
public function formatForeignKey(ForeignKeyEntity $foreignKey) |
|
81
|
|
|
{ |
|
82
|
|
|
$database = $foreignKey->database; |
|
|
|
|
|
|
83
|
|
|
$schema = $foreignKey->schema; |
|
84
|
|
|
$onActions = $this->driver->actions(); |
|
85
|
|
|
$sources = implode(', ', array_map(function ($idf) { |
|
86
|
|
|
return $this->escapeId($idf); |
|
87
|
|
|
}, $foreignKey->source)); |
|
88
|
|
|
$targets = implode(', ', array_map(function ($idf) { |
|
89
|
|
|
return $this->escapeId($idf); |
|
90
|
|
|
}, $foreignKey->target)); |
|
91
|
|
|
|
|
92
|
|
|
$query = " FOREIGN KEY ($sources) REFERENCES "; |
|
93
|
|
|
if ($database != '' && $database != $this->driver->database()) { |
|
94
|
|
|
$query .= $this->escapeId($database) . '.'; |
|
95
|
|
|
} |
|
96
|
|
|
if ($schema != '' && $schema != $this->driver->schema()) { |
|
97
|
|
|
$query .= $this->escapeId($schema) . '.'; |
|
98
|
|
|
} |
|
99
|
|
|
$query .= $this->table($foreignKey->table) . " ($targets)"; |
|
100
|
|
|
if (preg_match("~^($onActions)\$~", $foreignKey->onDelete)) { |
|
101
|
|
|
$query .= " ON DELETE {$foreignKey->onDelete}"; |
|
102
|
|
|
} |
|
103
|
|
|
if (preg_match("~^($onActions)\$~", $foreignKey->onUpdate)) { |
|
104
|
|
|
$query .= " ON UPDATE {$foreignKey->onUpdate}"; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $query; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @inheritDoc |
|
112
|
|
|
*/ |
|
113
|
|
|
public function quote($string) |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->connection->quote($string); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @inheritDoc |
|
120
|
|
|
*/ |
|
121
|
|
|
public function buildSelectQuery(TableSelectEntity $select) |
|
122
|
|
|
{ |
|
123
|
|
|
$isGroup = (count($select->group) < count($select->fields)); |
|
124
|
|
|
$query = ''; |
|
125
|
|
|
if ($this->driver->jush() === 'sql' && ($select->page) && |
|
126
|
|
|
($select->limit) && !empty($select->group) && $isGroup) { |
|
127
|
|
|
$query = 'SQL_CALC_FOUND_ROWS '; |
|
128
|
|
|
} |
|
129
|
|
|
$query .= \implode(', ', $select->fields) . "\nFROM " . $this->table($select->table); |
|
130
|
|
|
$clauses = ''; |
|
131
|
|
|
if (!empty($select->where)) { |
|
132
|
|
|
$clauses = "\nWHERE " . \implode(' AND ', $select->where); |
|
133
|
|
|
} |
|
134
|
|
|
if (!empty($select->group) && $isGroup) { |
|
135
|
|
|
$clauses .= "\nGROUP BY " . \implode(', ', $select->group); |
|
136
|
|
|
} |
|
137
|
|
|
if (!empty($select->order)) { |
|
138
|
|
|
$clauses .= "\nORDER BY " . \implode(', ', $select->order); |
|
139
|
|
|
} |
|
140
|
|
|
$limit = +$select->limit; |
|
141
|
|
|
$offset = $select->page ? $limit * $select->page : 0; |
|
142
|
|
|
|
|
143
|
|
|
return 'SELECT' . $this->limit($query, $clauses, $limit, $offset, "\n"); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @inheritDoc |
|
148
|
|
|
*/ |
|
149
|
|
|
public function applySqlFunction(string $function, string $column) |
|
150
|
|
|
{ |
|
151
|
|
|
return ($function ? ($function == "unixepoch" ? "DATETIME($column, '$function')" : |
|
152
|
|
|
($function == "count distinct" ? "COUNT(DISTINCT " : strtoupper("$function(")) . "$column)") : $column); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @inheritDoc |
|
157
|
|
|
*/ |
|
158
|
|
|
public function defaultValue($field) |
|
159
|
|
|
{ |
|
160
|
|
|
$default = $field->default; |
|
161
|
|
|
return ($default === null ? "" : " DEFAULT " . |
|
162
|
|
|
(preg_match('~char|binary|text|enum|set~', $field->type) || |
|
163
|
|
|
preg_match('~^(?![a-z])~i', $default) ? $this->quote($default) : $default)); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @inheritDoc |
|
168
|
|
|
*/ |
|
169
|
|
|
public function limit(string $query, string $where, int $limit, int $offset = 0, string $separator = " ") |
|
170
|
|
|
{ |
|
171
|
|
|
return ""; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @inheritDoc |
|
176
|
|
|
*/ |
|
177
|
|
|
public function limitToOne(string $table, string $query, string $where, string $separator = "\n") |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->limit($query, $where, 1, 0, $separator); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @inheritDoc |
|
184
|
|
|
*/ |
|
185
|
|
|
public function convertField(TableFieldEntity $field) |
|
186
|
|
|
{ |
|
187
|
|
|
return ''; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @inheritDoc |
|
192
|
|
|
*/ |
|
193
|
|
|
public function unconvertField(TableFieldEntity $field, string $value) |
|
194
|
|
|
{ |
|
195
|
|
|
return $value; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @inheritDoc |
|
200
|
|
|
*/ |
|
201
|
|
|
public function convertFields(array $columns, array $fields, array $select = []) |
|
202
|
|
|
{ |
|
203
|
|
|
$clause = ""; |
|
204
|
|
|
foreach ($columns as $key => $val) { |
|
205
|
|
|
if (!empty($select) && !in_array($this->escapeId($key), $select)) { |
|
206
|
|
|
continue; |
|
207
|
|
|
} |
|
208
|
|
|
$as = $this->convertField($fields[$key]); |
|
209
|
|
|
if ($as) { |
|
210
|
|
|
$clause .= ", $as AS " . $this->escapeId($key); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
return $clause; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @inheritDoc |
|
218
|
|
|
*/ |
|
219
|
|
|
public function countRowsSql(string $table, array $where, bool $isGroup, array $groups) |
|
220
|
|
|
{ |
|
221
|
|
|
$query = " FROM " . $this->table($table) . ($where ? " WHERE " . implode(" AND ", $where) : ""); |
|
222
|
|
|
return ($isGroup && ($this->driver->jush() == "sql" || count($groups) == 1) |
|
223
|
|
|
? "SELECT COUNT(DISTINCT " . implode(", ", $groups) . ")$query" |
|
224
|
|
|
: "SELECT COUNT(*)" . ($isGroup ? " FROM (SELECT 1$query GROUP BY " . implode(", ", $groups) . ") x" : $query) |
|
225
|
|
|
); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @inheritDoc |
|
230
|
|
|
*/ |
|
231
|
|
|
public function sqlForCreateTable(string $table, bool $autoIncrement, string $style) |
|
232
|
|
|
{ |
|
233
|
|
|
return ''; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @inheritDoc |
|
238
|
|
|
*/ |
|
239
|
|
|
public function sqlForCreateIndex(string $table, string $type, string $name, string $columns) |
|
240
|
|
|
{ |
|
241
|
|
|
return ''; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @inheritDoc |
|
246
|
|
|
*/ |
|
247
|
|
|
public function sqlForUseDatabase(string $database) |
|
248
|
|
|
{ |
|
249
|
|
|
return ""; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @inheritDoc |
|
254
|
|
|
*/ |
|
255
|
|
|
public function sqlForForeignKeys(string $table) |
|
256
|
|
|
{ |
|
257
|
|
|
return ''; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @inheritDoc |
|
262
|
|
|
*/ |
|
263
|
|
|
public function sqlForTruncateTable(string $table) |
|
264
|
|
|
{ |
|
265
|
|
|
return ''; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @inheritDoc |
|
270
|
|
|
*/ |
|
271
|
|
|
public function sqlForCreateTrigger(string $table) |
|
272
|
|
|
{ |
|
273
|
|
|
return ''; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* @inheritDoc |
|
278
|
|
|
*/ |
|
279
|
|
|
public function autoIncrement() |
|
280
|
|
|
{ |
|
281
|
|
|
return ""; |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|