|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Entity\ConfigEntity; |
|
6
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableEntity; |
|
7
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity; |
|
8
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableSelectEntity; |
|
9
|
|
|
use Lagdo\DbAdmin\Driver\Entity\ForeignKeyEntity; |
|
10
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TriggerEntity; |
|
11
|
|
|
use Lagdo\DbAdmin\Driver\Entity\RoutineEntity; |
|
12
|
|
|
use Lagdo\DbAdmin\Driver\Entity\QueryEntity; |
|
13
|
|
|
|
|
14
|
|
|
trait GrammarTrait |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Get escaped table name |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $idf |
|
20
|
|
|
* |
|
21
|
|
|
* @return string |
|
22
|
|
|
*/ |
|
23
|
|
|
public function table(string $idf) |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->grammar->table($idf); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Escape database identifier |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $idf |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
public function escapeId(string $idf) |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->grammar->escapeId($idf); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Unescape database identifier |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $idf |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function unescapeId(string $idf) |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->grammar->unescapeId($idf); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Convert field in select and edit |
|
54
|
|
|
* |
|
55
|
|
|
* @param TableFieldEntity $field one element from $this->fields() |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function convertField(TableFieldEntity $field) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->grammar->convertField($field); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Convert value in edit after applying functions back |
|
66
|
|
|
* |
|
67
|
|
|
* @param TableFieldEntity $field One element from $this->fields() |
|
68
|
|
|
* @param string $value |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function unconvertField(TableFieldEntity $field, string $value) |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->grammar->unconvertField($field, $value); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get select clause for convertible fields |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $columns |
|
81
|
|
|
* @param array $fields |
|
82
|
|
|
* @param array $select |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function convertFields(array $columns, array $fields, array $select = []) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->grammar->convertFields($columns, $fields, $select); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Select data from table |
|
93
|
|
|
* |
|
94
|
|
|
* @param TableSelectEntity $select |
|
95
|
|
|
* |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function buildSelectQuery(TableSelectEntity $select) |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->grammar->buildSelectQuery($select); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Parse a string containing SQL queries |
|
105
|
|
|
* |
|
106
|
|
|
* @param QueryEntity $queryEntity |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
|
|
public function parseQueries(QueryEntity $queryEntity) |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->grammar->parseQueries($queryEntity); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get query to compute number of found rows |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $table |
|
119
|
|
|
* @param array $where |
|
120
|
|
|
* @param bool $isGroup |
|
121
|
|
|
* @param array $groups |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
public function sqlForRowCount(string $table, array $where, bool $isGroup, array $groups) |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->grammar->sqlForRowCount($table, $where, $isGroup, $groups); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get default value clause |
|
132
|
|
|
* |
|
133
|
|
|
* @param TableFieldEntity $field |
|
134
|
|
|
* |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
public function defaultValue(TableFieldEntity $field) |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->grammar->defaultValue($field); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Formulate SQL query with limit |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $query Everything after SELECT |
|
146
|
|
|
* @param string $where Including WHERE |
|
147
|
|
|
* @param int $limit |
|
148
|
|
|
* @param int $offset |
|
149
|
|
|
* |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
|
|
public function limit(string $query, string $where, int $limit, int $offset = 0) |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->grammar->limit($query, $where, $limit, $offset); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Format foreign key to use in SQL query |
|
159
|
|
|
* |
|
160
|
|
|
* @param ForeignKeyEntity $foreignKey |
|
161
|
|
|
* |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
|
|
public function formatForeignKey(ForeignKeyEntity $foreignKey) |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->grammar->formatForeignKey($foreignKey); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Generate modifier for auto increment column |
|
171
|
|
|
* |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
|
|
public function autoIncrement() |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->grammar->autoIncrement(); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Get SQL command to create table |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $table |
|
183
|
|
|
* @param bool $autoIncrement |
|
184
|
|
|
* @param string $style |
|
185
|
|
|
* |
|
186
|
|
|
* @return string |
|
187
|
|
|
*/ |
|
188
|
|
|
public function sqlForCreateTable(string $table, bool $autoIncrement, string $style) |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->grammar->sqlForCreateTable($table, $autoIncrement, $style); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Command to create an index |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $table |
|
197
|
|
|
* @param string $type |
|
198
|
|
|
* @param string $name |
|
199
|
|
|
* @param string $columns |
|
200
|
|
|
* |
|
201
|
|
|
* @return string |
|
202
|
|
|
*/ |
|
203
|
|
|
public function sqlForCreateIndex(string $table, string $type, string $name, string $columns) |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->grammar->sqlForCreateIndex($table, $type, $name, $columns); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Get SQL command to create foreign keys |
|
210
|
|
|
* |
|
211
|
|
|
* sqlForCreateTable() produces CREATE TABLE without FK CONSTRAINTs |
|
212
|
|
|
* sqlForForeignKeys() produces all FK CONSTRAINTs as ALTER TABLE ... ADD CONSTRAINT |
|
213
|
|
|
* so that all FKs can be added after all tables have been created, avoiding any need |
|
214
|
|
|
* to reorder CREATE TABLE statements in order of their FK dependencies |
|
215
|
|
|
* |
|
216
|
|
|
* @param string $table |
|
217
|
|
|
* |
|
218
|
|
|
* @return string |
|
219
|
|
|
*/ |
|
220
|
|
|
public function sqlForForeignKeys(string $table) |
|
221
|
|
|
{ |
|
222
|
|
|
return $this->grammar->sqlForForeignKeys($table); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Get SQL command to truncate table |
|
227
|
|
|
* |
|
228
|
|
|
* @param string $table |
|
229
|
|
|
* |
|
230
|
|
|
* @return string |
|
231
|
|
|
*/ |
|
232
|
|
|
public function sqlForTruncateTable(string $table) |
|
233
|
|
|
{ |
|
234
|
|
|
return $this->grammar->sqlForTruncateTable($table); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Get SQL command to change database |
|
239
|
|
|
* |
|
240
|
|
|
* @param string $database |
|
241
|
|
|
* |
|
242
|
|
|
* @return string |
|
243
|
|
|
*/ |
|
244
|
|
|
public function sqlForUseDatabase(string $database) |
|
245
|
|
|
{ |
|
246
|
|
|
return $this->grammar->sqlForUseDatabase($database); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Get SQL commands to create triggers |
|
251
|
|
|
* |
|
252
|
|
|
* @param string $table |
|
253
|
|
|
* |
|
254
|
|
|
* @return string |
|
255
|
|
|
*/ |
|
256
|
|
|
public function sqlForCreateTrigger(string $table) |
|
257
|
|
|
{ |
|
258
|
|
|
return $this->grammar->sqlForCreateTrigger($table); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Return query to get connection ID |
|
263
|
|
|
* |
|
264
|
|
|
* @return string |
|
265
|
|
|
*/ |
|
266
|
|
|
// public function connectionId() |
|
267
|
|
|
// { |
|
268
|
|
|
// return $this->grammar->connectionId(); |
|
269
|
|
|
// } |
|
270
|
|
|
} |
|
271
|
|
|
|