Skip to content

Commit 36b8bd8

Browse files
committed
Merge branch 'QA'
Signed-off-by: William Desportes <williamdes@wdes.fr>
2 parents c70841c + a724b5d commit 36b8bd8

28 files changed

Lines changed: 309 additions & 774 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373

7474
- Fixed BufferedQuery when it has an odd number of backslashes in the end (#340)
7575
- Fixed the issue that ignored the body tokens when creating views with union (#343)
76+
- Fixed parser errors on "ALTER TABLE" statements to add columns with SET type (#168)
7677

7778
## [4.7.2] - 2021-02-05
7879

src/Components/AlterOperation.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ class AlterOperation extends Component
9292
'ALTER' => 1,
9393
'ANALYZE' => 1,
9494
'CHANGE' => 1,
95+
'CHARSET' => 1,
9596
'CHECK' => 1,
9697
'COALESCE' => 1,
9798
'CONVERT' => 1,
99+
'DEFAULT CHARSET' => 1,
98100
'DISABLE' => 1,
99101
'DISCARD' => 1,
100102
'DROP' => 1,
@@ -125,6 +127,8 @@ class AlterOperation extends Component
125127
'SPATIAL' => 2,
126128
'TABLESPACE' => 2,
127129
'INDEX' => 2,
130+
131+
'CHARACTER SET' => 3,
128132
];
129133

130134
/**
@@ -282,16 +286,35 @@ public static function parse(Parser $parser, TokensList $list, array $options =
282286
}
283287
} elseif (! self::checkIfTokenQuotedSymbol($token)) {
284288
if (! empty(Parser::$STATEMENT_PARSERS[$token->value])) {
285-
// We have reached the end of ALTER operation and suddenly found
286-
// a start to new statement, but have not find a delimiter between them
287-
288-
if (! ($token->value === 'SET' && $list->tokens[$list->idx - 1]->value === 'CHARACTER')) {
289+
// We want to get the next non-comment and non-space token after $token
290+
// therefore, the first getNext call will start with the current $idx which's $token,
291+
// will return it and increase $idx by 1, which's not guaranteed to be non-comment
292+
// and non-space, that's why we're calling getNext again.
293+
// In order to get back to the original value of idx, we kept it in $currentID
294+
295+
$currentTokenID = $list->idx;
296+
$list->getNext();
297+
$nextToken = $list->getNext();
298+
299+
if ($token->value === 'CHARACTER SET') {
300+
// Reverting the changes we made in the beginning
301+
$list->idx = $currentTokenID;
302+
} else if ($token->value === 'SET' && $nextToken->value === '(') {
303+
// To avoid adding the tokens between the SET() parentheses to the unknown tokens
304+
$list->getNextOfTypeAndValue(Token::TYPE_OPERATOR, ')');
305+
} else if ($token->value === 'SET' && $nextToken->value === 'DEFAULT') {
306+
// to avoid adding the `DEFAULT` token to the unknown tokens.
307+
++$list->idx;
308+
} else {
309+
// We have reached the end of ALTER operation and suddenly found
310+
// a start to new statement, but have not find a delimiter between them
289311
$parser->error(
290312
'A new statement was found, but no delimiter between it and the previous one.',
291313
$token
292314
);
293315
break;
294316
}
317+
295318
} elseif (
296319
(array_key_exists($arrayKey, self::$DB_OPTIONS)
297320
|| array_key_exists($arrayKey, self::$TABLE_OPTIONS))

tests/data/parser/parseAlter.out

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,9 +654,11 @@
654654
"ALTER": 1,
655655
"ANALYZE": 1,
656656
"CHANGE": 1,
657+
"CHARSET": 1,
657658
"CHECK": 1,
658659
"COALESCE": 1,
659660
"CONVERT": 1,
661+
"DEFAULT CHARSET": 1,
660662
"DISABLE": 1,
661663
"DISCARD": 1,
662664
"DROP": 1,
@@ -685,7 +687,8 @@
685687
"PRIMARY KEY": 2,
686688
"SPATIAL": 2,
687689
"TABLESPACE": 2,
688-
"INDEX": 2
690+
"INDEX": 2,
691+
"CHARACTER SET": 3
689692
},
690693
"VIEW_OPTIONS": {
691694
"AS": 1
@@ -771,9 +774,11 @@
771774
"ALTER": 1,
772775
"ANALYZE": 1,
773776
"CHANGE": 1,
777+
"CHARSET": 1,
774778
"CHECK": 1,
775779
"COALESCE": 1,
776780
"CONVERT": 1,
781+
"DEFAULT CHARSET": 1,
777782
"DISABLE": 1,
778783
"DISCARD": 1,
779784
"DROP": 1,
@@ -802,7 +807,8 @@
802807
"PRIMARY KEY": 2,
803808
"SPATIAL": 2,
804809
"TABLESPACE": 2,
805-
"INDEX": 2
810+
"INDEX": 2,
811+
"CHARACTER SET": 3
806812
},
807813
"VIEW_OPTIONS": {
808814
"AS": 1

tests/data/parser/parseAlter10.out

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,11 @@
618618
"ALTER": 1,
619619
"ANALYZE": 1,
620620
"CHANGE": 1,
621+
"CHARSET": 1,
621622
"CHECK": 1,
622623
"COALESCE": 1,
623624
"CONVERT": 1,
625+
"DEFAULT CHARSET": 1,
624626
"DISABLE": 1,
625627
"DISCARD": 1,
626628
"DROP": 1,
@@ -649,7 +651,8 @@
649651
"PRIMARY KEY": 2,
650652
"SPATIAL": 2,
651653
"TABLESPACE": 2,
652-
"INDEX": 2
654+
"INDEX": 2,
655+
"CHARACTER SET": 3
653656
},
654657
"VIEW_OPTIONS": {
655658
"AS": 1

0 commit comments

Comments
 (0)