1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Migratio\GrammarStructure\Mysql; |
4
|
|
|
|
5
|
|
|
class QuerySyntax extends QuerySyntaxHelper |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @var array $data |
10
|
|
|
*/ |
11
|
|
|
protected $data = array(); |
12
|
|
|
/** |
13
|
|
|
* @var array $syntax |
14
|
|
|
*/ |
15
|
|
|
protected $syntax = array(); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
|
|
public function syntaxCreate() |
21
|
|
|
{ |
22
|
|
|
$this->getWizardObjects($this->object); |
23
|
|
|
|
24
|
|
|
$this->getCreateTableSyntax(); |
25
|
|
|
|
26
|
|
|
$this->getDefaultSyntaxGroup(); |
27
|
|
|
|
28
|
|
|
$this->syntax[]=')'; |
29
|
|
|
|
30
|
|
|
//get table collation |
31
|
|
|
if(isset($this->data['tableCollation']['table'])){ |
32
|
|
|
$this->syntax[]=' DEFAULT CHARACTER SET '.$this->data['tableCollation']['table']; |
33
|
|
|
} |
34
|
|
|
else{ |
35
|
|
|
$this->syntax[]=' DEFAULT CHARACTER SET utf8'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
//get engine |
39
|
|
|
if($this->data['engine']!==null) |
40
|
|
|
{ |
41
|
|
|
$this->syntax[]=' ENGINE='.$this->data['engine'].' '; |
42
|
|
|
} |
43
|
|
|
else{ |
44
|
|
|
$this->syntax[]=' ENGINE=InnoDB '; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$syntax = implode("",$this->syntax); |
48
|
|
|
|
49
|
|
|
$query=$this->schema->getConnection()->setQueryBasic($syntax); |
50
|
|
|
|
51
|
|
|
return [ |
52
|
|
|
'syntax'=>$syntax, |
53
|
|
|
'type'=>'create', |
54
|
|
|
'result'=>$query['result'], |
55
|
|
|
'message'=>$query['message'], |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return mixed|void |
61
|
|
|
*/ |
62
|
|
|
private function getDefaultSyntaxGroup() |
63
|
|
|
{ |
64
|
|
|
|
65
|
|
|
$this->syntax[]=implode(",",$this->getCreateDefaultList()); |
66
|
|
|
|
67
|
|
|
//get unique values |
68
|
|
|
if(isset($this->data['uniqueValueList']) && count($this->data['uniqueValueList'])){ |
69
|
|
|
$this->syntax[]=','.implode(',',$this->data['uniqueValueList']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
//get index values |
73
|
|
|
if(isset($this->data['indexValueList']) && count($this->data['indexValueList'])){ |
74
|
|
|
$this->syntax[]=','.implode(',',$this->data['indexValueList']); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
//get index values for key |
78
|
|
|
if(count($this->getKeyList())){ |
79
|
|
|
$this->syntax[]=','.implode(',',$this->getKeyList()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if(count($this->data['references'])){ |
83
|
|
|
$this->syntax[]=$this->getReferenceSyntax($this->data['references']); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return mixed|void |
90
|
|
|
*/ |
91
|
|
|
public function syntaxAlter() |
92
|
|
|
{ |
93
|
|
|
$this->getWizardObjects($this->object); |
94
|
|
|
|
95
|
|
|
$alterType = $this->object->getAlterType(); |
96
|
|
|
|
97
|
|
|
$group = $alterType['group']; |
98
|
|
|
|
99
|
|
|
$this->getDefaultSyntaxGroup(); |
100
|
|
|
|
101
|
|
|
return $this->{$group}($alterType); |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function change($alterType) |
106
|
|
|
{ |
107
|
|
|
if(isset($alterType['place'])){ |
108
|
|
|
|
109
|
|
|
foreach ($alterType['place'] as $placeKey=>$placeValue){ |
110
|
|
|
$placeList=$placeKey .' '.$placeValue.''; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$syntax = implode("",$this->syntax); |
114
|
|
|
|
115
|
|
|
$columns = $this->schema->getConnection()->showColumnsFrom($this->table); |
116
|
|
|
|
117
|
|
|
foreach ($columns as $columnKey=>$columnData){ |
118
|
|
|
if($columnData['Field']==$placeValue){ |
|
|
|
|
119
|
|
|
$changeAbleField = $columns[$columnKey+1]['Field']; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$syntaxList = explode(' ',$syntax); |
124
|
|
|
|
125
|
|
|
if(current($syntaxList)!==$changeAbleField){ |
|
|
|
|
126
|
|
|
$alterSytanx = 'ALTER TABLE '.$this->table.' change '.$changeAbleField.' '.current($syntaxList).' '.implode(' ',array_splice($syntaxList,1)).' '.$placeList; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
else{ |
129
|
|
|
$alterSytanx = 'ALTER TABLE '.$this->table.' modify '.$syntax.' '.$placeList; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
$query=$this->schema->getConnection()->setQueryBasic($alterSytanx); |
134
|
|
|
|
135
|
|
|
return [ |
136
|
|
|
'syntax'=>$syntax, |
137
|
|
|
'type'=>'create', |
138
|
|
|
'result'=>$query['result'], |
139
|
|
|
'message'=>$query['message'], |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
private function addColumn($alterType) |
145
|
|
|
{ |
146
|
|
|
if(isset($alterType['place'])){ |
147
|
|
|
|
148
|
|
|
foreach ($alterType['place'] as $placeKey=>$placeValue){ |
149
|
|
|
$placeList=$placeKey .' '.$placeValue.''; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$syntax = implode("",$this->syntax); |
153
|
|
|
|
154
|
|
|
$alterSytanx = 'ALTER TABLE '.$this->table.' ADD COLUMN '.$syntax.' '.$placeList; |
|
|
|
|
155
|
|
|
|
156
|
|
|
$query=$this->schema->getConnection()->setQueryBasic($alterSytanx); |
157
|
|
|
|
158
|
|
|
return [ |
159
|
|
|
'syntax'=>$syntax, |
160
|
|
|
'type'=>'create', |
161
|
|
|
'result'=>$query['result'], |
162
|
|
|
'message'=>$query['message'], |
163
|
|
|
]; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|