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