1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Migratio\GrammarStructure\Mysql; |
4
|
|
|
|
5
|
|
|
class QuerySyntax extends QuerySyntaxHelper |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array $data |
9
|
|
|
*/ |
10
|
|
|
protected $data = array(); |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var array $syntax |
14
|
|
|
*/ |
15
|
|
|
protected $syntax = array(); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var null|string |
19
|
|
|
*/ |
20
|
|
|
protected $group; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $alterExtras = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* add column |
29
|
|
|
* |
30
|
|
|
* @param $alterType |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
private function addColumn($alterType) |
34
|
|
|
{ |
35
|
|
|
if(isset($alterType['place'])){ |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
foreach ($alterType['place'] as $placeKey=>$placeValue){ |
39
|
|
|
$placeList=$placeKey .' '.$placeValue.''; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$syntax = implode("",$this->syntax); |
43
|
|
|
|
44
|
|
|
$alterSytanx = 'ALTER TABLE '.$this->table.' ADD COLUMN '.$syntax.' '.$placeList; |
|
|
|
|
45
|
|
|
|
46
|
|
|
$query=$this->schema->getConnection()->setQueryBasic($alterSytanx); |
47
|
|
|
|
48
|
|
|
if(count($this->alterExtras)){ |
49
|
|
|
foreach($this->alterExtras as $extra){ |
50
|
|
|
|
51
|
|
|
$extraSyntax = 'ALTER TABLE '.$this->table.' '.$extra.''; |
52
|
|
|
|
53
|
|
|
$query=$this->schema->getConnection()->setQueryBasic($extraSyntax); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->alterExtras = []; |
58
|
|
|
|
59
|
|
|
return [ |
60
|
|
|
'syntax'=>$syntax, |
61
|
|
|
'type'=>'addColumn', |
62
|
|
|
'result'=>$query['result'], |
63
|
|
|
'message'=>$query['message'], |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* change column |
71
|
|
|
* |
72
|
|
|
* @param $alterType |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
private function change($alterType) |
76
|
|
|
{ |
77
|
|
|
if(isset($alterType['place'])){ |
78
|
|
|
|
79
|
|
|
foreach ($alterType['place'] as $placeKey=>$placeValue){ |
80
|
|
|
$placeList=$placeKey .' '.$placeValue.''; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$syntax = implode("",$this->syntax); |
84
|
|
|
|
85
|
|
|
$columns = $this->schema->getConnection()->showColumnsFrom($this->table); |
86
|
|
|
|
87
|
|
|
foreach ($columns as $columnKey=>$columnData){ |
88
|
|
|
if($columnData['Field']==$placeValue){ |
|
|
|
|
89
|
|
|
$changeAbleField = $columns[$columnKey+1]['Field']; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$syntaxList = explode(' ',$syntax); |
94
|
|
|
|
95
|
|
|
if(current($syntaxList)!==$changeAbleField){ |
|
|
|
|
96
|
|
|
$alterSytanx = 'ALTER TABLE '.$this->table.' change '.$changeAbleField.' '.current($syntaxList).' '.implode(' ',array_splice($syntaxList,1)).' '.$placeList; |
|
|
|
|
97
|
|
|
} |
98
|
|
|
else{ |
99
|
|
|
$alterSytanx = 'ALTER TABLE '.$this->table.' modify '.$syntax.' '.$placeList; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
$query=$this->schema->getConnection()->setQueryBasic($alterSytanx); |
104
|
|
|
|
105
|
|
|
if(count($this->alterExtras)){ |
106
|
|
|
foreach($this->alterExtras as $extra){ |
107
|
|
|
|
108
|
|
|
$extraSyntax = 'ALTER TABLE '.$this->table.' '.$extra.''; |
109
|
|
|
|
110
|
|
|
$query=$this->schema->getConnection()->setQueryBasic($extraSyntax); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->alterExtras = []; |
115
|
|
|
|
116
|
|
|
return [ |
117
|
|
|
'syntax'=>$syntax, |
118
|
|
|
'type'=>'create', |
119
|
|
|
'result'=>$query['result'], |
120
|
|
|
'message'=>$query['message'], |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* add Indexes |
127
|
|
|
* |
128
|
|
|
* @param $alterType |
129
|
|
|
*/ |
130
|
|
|
private function addIndex($alterType) |
131
|
|
|
{ |
132
|
|
|
if(isset($this->syntax[0])){ |
133
|
|
|
|
134
|
|
|
$index = $this->syntax[0]; |
135
|
|
|
|
136
|
|
|
foreach($index as $name=>$item){ |
137
|
|
|
$index_name = $name; |
138
|
|
|
$indexes = implode(',',$item); |
139
|
|
|
} |
140
|
|
|
$alterSytanx = 'create index '.$index_name.' on '.$this->table.' ('.$indexes.')'; |
|
|
|
|
141
|
|
|
|
142
|
|
|
$query=$this->schema->getConnection()->setQueryBasic($alterSytanx); |
143
|
|
|
|
144
|
|
|
return [ |
145
|
|
|
'syntax'=>$this->syntax, |
146
|
|
|
'type'=>'alter', |
147
|
|
|
'result'=>$query['result'], |
148
|
|
|
'message'=>$query['message'], |
149
|
|
|
]; |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* add Indexes |
158
|
|
|
* |
159
|
|
|
* @param $alterType |
160
|
|
|
*/ |
161
|
|
|
private function addUnique($alterType) |
162
|
|
|
{ |
163
|
|
|
if(isset($this->syntax[0])){ |
164
|
|
|
|
165
|
|
|
$unique = $this->syntax[0]; |
166
|
|
|
|
167
|
|
|
foreach($unique as $name=>$item){ |
168
|
|
|
$unique_name = $name; |
169
|
|
|
$uniques = implode(',',$item); |
170
|
|
|
} |
171
|
|
|
$alterSytanx = 'ALTER TABLE '.$this->table.' ADD CONSTRAINT '.$unique_name.' UNIQUE ('.$uniques.')'; |
|
|
|
|
172
|
|
|
|
173
|
|
|
$query=$this->schema->getConnection()->setQueryBasic($alterSytanx); |
174
|
|
|
|
175
|
|
|
return [ |
176
|
|
|
'syntax'=>$this->syntax, |
177
|
|
|
'type'=>'alter', |
178
|
|
|
'result'=>$query['result'], |
179
|
|
|
'message'=>$query['message'], |
180
|
|
|
]; |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* drop column |
189
|
|
|
* |
190
|
|
|
* @param $alterType |
191
|
|
|
*/ |
192
|
|
|
private function dropColumn($alterType) |
193
|
|
|
{ |
194
|
|
|
if(isset($this->syntax[0])){ |
195
|
|
|
|
196
|
|
|
$column = rtrim($this->syntax[0]); |
197
|
|
|
|
198
|
|
|
$alterSytanx = 'alter table '.$this->table.' drop column '.$column; |
199
|
|
|
|
200
|
|
|
$query=$this->schema->getConnection()->setQueryBasic($alterSytanx); |
201
|
|
|
|
202
|
|
|
return [ |
203
|
|
|
'syntax'=>$this->syntax, |
204
|
|
|
'type'=>'alter', |
205
|
|
|
'result'=>$query['result'], |
206
|
|
|
'message'=>$query['message'], |
207
|
|
|
]; |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return array |
216
|
|
|
*/ |
217
|
|
|
public function syntaxCreate() |
218
|
|
|
{ |
219
|
|
|
$this->getWizardObjects($this->object); |
220
|
|
|
|
221
|
|
|
$existTables = $this->schema->getConnection()->showTables(); |
222
|
|
|
|
223
|
|
|
$this->getCreateTableSyntax(); |
224
|
|
|
|
225
|
|
|
$this->getDefaultSyntaxGroup(); |
226
|
|
|
|
227
|
|
|
$this->syntax[]=')'; |
228
|
|
|
|
229
|
|
|
//get table collation |
230
|
|
|
if(isset($this->data['tableCollation']['table'])){ |
231
|
|
|
$this->syntax[]=' DEFAULT CHARACTER SET '.$this->data['tableCollation']['table']; |
232
|
|
|
} |
233
|
|
|
else{ |
234
|
|
|
$this->syntax[]=' DEFAULT CHARACTER SET utf8'; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
//get engine |
238
|
|
|
if($this->data['engine']!==null) |
239
|
|
|
{ |
240
|
|
|
$this->syntax[]=' ENGINE='.$this->data['engine'].' '; |
241
|
|
|
} |
242
|
|
|
else{ |
243
|
|
|
$this->syntax[]=' ENGINE=InnoDB '; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$syntax = implode("",$this->syntax); |
247
|
|
|
|
248
|
|
|
if(in_array($this->table,$existTables)){ |
249
|
|
|
return false; |
|
|
|
|
250
|
|
|
} |
251
|
|
|
else{ |
252
|
|
|
$query=$this->schema->getConnection()->setQueryBasic($syntax); |
253
|
|
|
|
254
|
|
|
return [ |
255
|
|
|
'syntax'=>$syntax, |
256
|
|
|
'type'=>'create', |
257
|
|
|
'result'=>$query['result'], |
258
|
|
|
'message'=>$query['message'], |
259
|
|
|
]; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param null $group |
|
|
|
|
266
|
|
|
*/ |
267
|
|
|
private function getDefaultSyntaxGroup($group=null) |
268
|
|
|
{ |
269
|
|
|
$this->syntax[] = implode(",",$this->getCreateDefaultList()); |
270
|
|
|
|
271
|
|
|
//get unique values |
272
|
|
|
if(isset($this->data['uniqueValueList']) && count($this->data['uniqueValueList'])){ |
273
|
|
|
|
274
|
|
|
if($group=='create'){ |
275
|
|
|
$this->syntax[]=','.implode(',',$this->data['uniqueValueList']); |
276
|
|
|
} |
277
|
|
|
else{ |
278
|
|
|
$this->alterExtras[]='ADD '.implode(',',$this->data['uniqueValueList']); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
//get index values |
284
|
|
|
if(isset($this->data['indexValueList']) && count($this->data['indexValueList'])){ |
285
|
|
|
|
286
|
|
|
if($group=='create'){ |
287
|
|
|
$this->syntax[]=','.implode(',',$this->data['indexValueList']); |
288
|
|
|
} |
289
|
|
|
else{ |
290
|
|
|
$this->alterExtras[]='ADD '.implode(',',$this->data['indexValueList']); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
//get index values for key |
296
|
|
|
if(count($this->getKeyList())){ |
297
|
|
|
$this->syntax[]=','.implode(',',$this->getKeyList()); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
if(count($this->data['references'])){ |
301
|
|
|
$this->syntax[]=$this->getReferenceSyntax($this->data['references']); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
if(isset($this->syntax[0]) && $this->syntax[0]=='' && $group=='addIndex'){ |
305
|
|
|
$this->syntax[0] = $this->data['index']; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
if(isset($this->syntax[0]) && $this->syntax[0]=='' && $group=='addUnique'){ |
309
|
|
|
$this->syntax[0] = $this->data['unique']; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @return mixed|void |
316
|
|
|
*/ |
317
|
|
|
public function syntaxAlter() |
318
|
|
|
{ |
319
|
|
|
$this->getWizardObjects($this->object); |
320
|
|
|
|
321
|
|
|
$alterType = $this->object->getAlterType(); |
322
|
|
|
|
323
|
|
|
$group = $alterType['group']; |
324
|
|
|
|
325
|
|
|
$this->getDefaultSyntaxGroup($group); |
326
|
|
|
|
327
|
|
|
return $this->{$group}($alterType); |
328
|
|
|
|
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
|