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