Skip to content

Commit f40c620

Browse files
committed
Merge PR #611 Add "UNIQUE" and "UNIQUE KEY" to ALTER TABLE statement
Fixes #610 Signed-off-by: Maurício Meneghini Fauth <[email protected]>
2 parents 26851b6 + b4b3c42 commit f40c620

8 files changed

+562
-28
lines changed

src/Components/AlterOperation.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ class AlterOperation extends Component
146146
2,
147147
'var',
148148
],
149+
'UNIQUE' => 2,
150+
'UNIQUE KEY' => 2,
149151

150152
'CHARACTER SET' => 3,
151153
'TO' => [

tests/Parser/AlterStatementTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public static function alterProvider(): array
4949
['parser/parseAlterTableCoalescePartition'],
5050
['parser/parseAlterTableAddColumnWithCheck'],
5151
['parser/parseAlterTableAddSpatialIndex1'],
52+
['parser/parseAlterTableAddUniqueKey1'],
53+
['parser/parseAlterTableAddUniqueKey2'],
5254
['parser/parseAlterTableDropAddIndex1'],
5355
['parser/parseAlterTableDropColumn1'],
5456
['parser/parseAlterTableModifyColumn'],

tests/data/parser/parseAlter14.out

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -325,24 +325,22 @@
325325
"options": {
326326
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
327327
"options": {
328-
"1": "ADD"
328+
"1": "ADD",
329+
"2": "UNIQUE KEY"
329330
}
330331
},
331-
"field": null,
332+
"field": {
333+
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
334+
"database": null,
335+
"table": null,
336+
"column": "functional_index",
337+
"expr": "`functional_index`",
338+
"alias": null,
339+
"function": null,
340+
"subquery": null
341+
},
332342
"partitions": null,
333343
"unknown": [
334-
{
335-
"@type": "@10"
336-
},
337-
{
338-
"@type": "@11"
339-
},
340-
{
341-
"@type": "@12"
342-
},
343-
{
344-
"@type": "@13"
345-
},
346344
{
347345
"@type": "@14"
348346
},
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE testtable ADD UNIQUE KEY name_of_the_unique_constraint (`UNIQUE_COLUMN`);
Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
{
2+
"query": "ALTER TABLE testtable ADD UNIQUE KEY name_of_the_unique_constraint (`UNIQUE_COLUMN`);\n",
3+
"lexer": {
4+
"@type": "PhpMyAdmin\\SqlParser\\Lexer",
5+
"str": "ALTER TABLE testtable ADD UNIQUE KEY name_of_the_unique_constraint (`UNIQUE_COLUMN`);\n",
6+
"len": 86,
7+
"last": 86,
8+
"list": {
9+
"@type": "PhpMyAdmin\\SqlParser\\TokensList",
10+
"tokens": [
11+
{
12+
"@type": "PhpMyAdmin\\SqlParser\\Token",
13+
"token": "ALTER",
14+
"value": "ALTER",
15+
"keyword": "ALTER",
16+
"type": 1,
17+
"flags": 3,
18+
"position": 0
19+
},
20+
{
21+
"@type": "PhpMyAdmin\\SqlParser\\Token",
22+
"token": " ",
23+
"value": " ",
24+
"keyword": null,
25+
"type": 3,
26+
"flags": 0,
27+
"position": 5
28+
},
29+
{
30+
"@type": "PhpMyAdmin\\SqlParser\\Token",
31+
"token": "TABLE",
32+
"value": "TABLE",
33+
"keyword": "TABLE",
34+
"type": 1,
35+
"flags": 3,
36+
"position": 6
37+
},
38+
{
39+
"@type": "PhpMyAdmin\\SqlParser\\Token",
40+
"token": " ",
41+
"value": " ",
42+
"keyword": null,
43+
"type": 3,
44+
"flags": 0,
45+
"position": 11
46+
},
47+
{
48+
"@type": "PhpMyAdmin\\SqlParser\\Token",
49+
"token": "testtable",
50+
"value": "testtable",
51+
"keyword": null,
52+
"type": 0,
53+
"flags": 0,
54+
"position": 12
55+
},
56+
{
57+
"@type": "PhpMyAdmin\\SqlParser\\Token",
58+
"token": " ",
59+
"value": " ",
60+
"keyword": null,
61+
"type": 3,
62+
"flags": 0,
63+
"position": 21
64+
},
65+
{
66+
"@type": "PhpMyAdmin\\SqlParser\\Token",
67+
"token": "ADD",
68+
"value": "ADD",
69+
"keyword": "ADD",
70+
"type": 1,
71+
"flags": 3,
72+
"position": 22
73+
},
74+
{
75+
"@type": "PhpMyAdmin\\SqlParser\\Token",
76+
"token": " ",
77+
"value": " ",
78+
"keyword": null,
79+
"type": 3,
80+
"flags": 0,
81+
"position": 25
82+
},
83+
{
84+
"@type": "PhpMyAdmin\\SqlParser\\Token",
85+
"token": "UNIQUE KEY",
86+
"value": "UNIQUE KEY",
87+
"keyword": "UNIQUE KEY",
88+
"type": 1,
89+
"flags": 23,
90+
"position": 26
91+
},
92+
{
93+
"@type": "PhpMyAdmin\\SqlParser\\Token",
94+
"token": " ",
95+
"value": " ",
96+
"keyword": null,
97+
"type": 3,
98+
"flags": 0,
99+
"position": 36
100+
},
101+
{
102+
"@type": "PhpMyAdmin\\SqlParser\\Token",
103+
"token": "name_of_the_unique_constraint",
104+
"value": "name_of_the_unique_constraint",
105+
"keyword": null,
106+
"type": 0,
107+
"flags": 0,
108+
"position": 37
109+
},
110+
{
111+
"@type": "PhpMyAdmin\\SqlParser\\Token",
112+
"token": " ",
113+
"value": " ",
114+
"keyword": null,
115+
"type": 3,
116+
"flags": 0,
117+
"position": 66
118+
},
119+
{
120+
"@type": "PhpMyAdmin\\SqlParser\\Token",
121+
"token": "(",
122+
"value": "(",
123+
"keyword": null,
124+
"type": 2,
125+
"flags": 16,
126+
"position": 67
127+
},
128+
{
129+
"@type": "PhpMyAdmin\\SqlParser\\Token",
130+
"token": "`UNIQUE_COLUMN`",
131+
"value": "UNIQUE_COLUMN",
132+
"keyword": null,
133+
"type": 8,
134+
"flags": 2,
135+
"position": 68
136+
},
137+
{
138+
"@type": "PhpMyAdmin\\SqlParser\\Token",
139+
"token": ")",
140+
"value": ")",
141+
"keyword": null,
142+
"type": 2,
143+
"flags": 16,
144+
"position": 83
145+
},
146+
{
147+
"@type": "PhpMyAdmin\\SqlParser\\Token",
148+
"token": ";",
149+
"value": ";",
150+
"keyword": null,
151+
"type": 9,
152+
"flags": 0,
153+
"position": 84
154+
},
155+
{
156+
"@type": "PhpMyAdmin\\SqlParser\\Token",
157+
"token": "\n",
158+
"value": " ",
159+
"keyword": null,
160+
"type": 3,
161+
"flags": 0,
162+
"position": 85
163+
},
164+
{
165+
"@type": "PhpMyAdmin\\SqlParser\\Token",
166+
"token": null,
167+
"value": null,
168+
"keyword": null,
169+
"type": 9,
170+
"flags": 0,
171+
"position": null
172+
}
173+
],
174+
"count": 18,
175+
"idx": 18
176+
},
177+
"delimiter": ";",
178+
"delimiterLen": 1,
179+
"strict": false,
180+
"errors": []
181+
},
182+
"parser": {
183+
"@type": "PhpMyAdmin\\SqlParser\\Parser",
184+
"list": {
185+
"@type": "@1"
186+
},
187+
"statements": [
188+
{
189+
"@type": "PhpMyAdmin\\SqlParser\\Statements\\AlterStatement",
190+
"table": {
191+
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
192+
"database": null,
193+
"table": "testtable",
194+
"column": null,
195+
"expr": "testtable",
196+
"alias": null,
197+
"function": null,
198+
"subquery": null
199+
},
200+
"altered": [
201+
{
202+
"@type": "PhpMyAdmin\\SqlParser\\Components\\AlterOperation",
203+
"ROUTINE_OPTIONS": {
204+
"COMMENT": [
205+
1,
206+
"var"
207+
],
208+
"LANGUAGE SQL": 2,
209+
"CONTAINS SQL": 3,
210+
"NO SQL": 3,
211+
"READS SQL DATA": 3,
212+
"MODIFIES SQL DATA": 3,
213+
"SQL SECURITY": 4,
214+
"DEFINER": 5,
215+
"INVOKER": 5
216+
},
217+
"options": {
218+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
219+
"options": {
220+
"1": "ADD",
221+
"2": "UNIQUE KEY"
222+
}
223+
},
224+
"field": {
225+
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
226+
"database": null,
227+
"table": null,
228+
"column": "name_of_the_unique_constraint",
229+
"expr": "name_of_the_unique_constraint",
230+
"alias": null,
231+
"function": null,
232+
"subquery": null
233+
},
234+
"partitions": null,
235+
"unknown": [
236+
{
237+
"@type": "@14"
238+
},
239+
{
240+
"@type": "@15"
241+
},
242+
{
243+
"@type": "@16"
244+
}
245+
]
246+
}
247+
],
248+
"options": {
249+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
250+
"options": {
251+
"3": "TABLE"
252+
}
253+
},
254+
"first": 0,
255+
"last": 15
256+
}
257+
],
258+
"brackets": 0,
259+
"strict": false,
260+
"errors": []
261+
},
262+
"errors": {
263+
"lexer": [],
264+
"parser": []
265+
}
266+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE testtable ADD UNIQUE name_of_the_unique_constraint (`UNIQUE_COLUMN`);

0 commit comments

Comments
 (0)