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
|
|
|
* drop column |
127
|
|
|
* |
128
|
|
|
* @param $alterType |
129
|
|
|
*/ |
130
|
|
|
private function dropColumn($alterType) |
131
|
|
|
{ |
132
|
|
|
if(isset($this->syntax[0])){ |
133
|
|
|
|
134
|
|
|
$column = rtrim($this->syntax[0]); |
135
|
|
|
|
136
|
|
|
$alterSytanx = 'alter table '.$this->table.' drop column '.$column; |
137
|
|
|
|
138
|
|
|
$query=$this->schema->getConnection()->setQueryBasic($alterSytanx); |
139
|
|
|
|
140
|
|
|
return [ |
141
|
|
|
'syntax'=>$this->syntax, |
142
|
|
|
'type'=>'alter', |
143
|
|
|
'result'=>$query['result'], |
144
|
|
|
'message'=>$query['message'], |
145
|
|
|
]; |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
public function syntaxCreate() |
156
|
|
|
{ |
157
|
|
|
$this->getWizardObjects($this->object); |
158
|
|
|
|
159
|
|
|
$existTables = $this->schema->getConnection()->showTables(); |
160
|
|
|
|
161
|
|
|
$this->getCreateTableSyntax(); |
162
|
|
|
|
163
|
|
|
$this->getDefaultSyntaxGroup(); |
164
|
|
|
|
165
|
|
|
$this->syntax[]=')'; |
166
|
|
|
|
167
|
|
|
//get table collation |
168
|
|
|
if(isset($this->data['tableCollation']['table'])){ |
169
|
|
|
$this->syntax[]=' DEFAULT CHARACTER SET '.$this->data['tableCollation']['table']; |
170
|
|
|
} |
171
|
|
|
else{ |
172
|
|
|
$this->syntax[]=' DEFAULT CHARACTER SET utf8'; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
//get engine |
176
|
|
|
if($this->data['engine']!==null) |
177
|
|
|
{ |
178
|
|
|
$this->syntax[]=' ENGINE='.$this->data['engine'].' '; |
179
|
|
|
} |
180
|
|
|
else{ |
181
|
|
|
$this->syntax[]=' ENGINE=InnoDB '; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$syntax = implode("",$this->syntax); |
185
|
|
|
|
186
|
|
|
if(in_array($this->table,$existTables)){ |
187
|
|
|
return false; |
|
|
|
|
188
|
|
|
} |
189
|
|
|
else{ |
190
|
|
|
$query=$this->schema->getConnection()->setQueryBasic($syntax); |
191
|
|
|
|
192
|
|
|
return [ |
193
|
|
|
'syntax'=>$syntax, |
194
|
|
|
'type'=>'create', |
195
|
|
|
'result'=>$query['result'], |
196
|
|
|
'message'=>$query['message'], |
197
|
|
|
]; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param null $group |
|
|
|
|
204
|
|
|
*/ |
205
|
|
|
private function getDefaultSyntaxGroup($group=null) |
206
|
|
|
{ |
207
|
|
|
$this->syntax[]=implode(",",$this->getCreateDefaultList()); |
208
|
|
|
|
209
|
|
|
//get unique values |
210
|
|
|
if(isset($this->data['uniqueValueList']) && count($this->data['uniqueValueList'])){ |
211
|
|
|
|
212
|
|
|
if($group=='create'){ |
213
|
|
|
$this->syntax[]=','.implode(',',$this->data['uniqueValueList']); |
214
|
|
|
} |
215
|
|
|
else{ |
216
|
|
|
$this->alterExtras[]='ADD '.implode(',',$this->data['uniqueValueList']); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
//get index values |
222
|
|
|
if(isset($this->data['indexValueList']) && count($this->data['indexValueList'])){ |
223
|
|
|
|
224
|
|
|
if($group=='create'){ |
225
|
|
|
$this->syntax[]=','.implode(',',$this->data['indexValueList']); |
226
|
|
|
} |
227
|
|
|
else{ |
228
|
|
|
$this->alterExtras[]='ADD '.implode(',',$this->data['indexValueList']); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
//get index values for key |
234
|
|
|
if(count($this->getKeyList())){ |
235
|
|
|
$this->syntax[]=','.implode(',',$this->getKeyList()); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if(count($this->data['references'])){ |
239
|
|
|
$this->syntax[]=$this->getReferenceSyntax($this->data['references']); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return mixed|void |
246
|
|
|
*/ |
247
|
|
|
public function syntaxAlter() |
248
|
|
|
{ |
249
|
|
|
$this->getWizardObjects($this->object); |
250
|
|
|
|
251
|
|
|
$alterType = $this->object->getAlterType(); |
252
|
|
|
|
253
|
|
|
$group = $alterType['group']; |
254
|
|
|
|
255
|
|
|
$this->getDefaultSyntaxGroup($group); |
256
|
|
|
|
257
|
|
|
return $this->{$group}($alterType); |
258
|
|
|
|
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
|