-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathAlterTableParser.php
More file actions
89 lines (70 loc) · 2.22 KB
/
AlterTableParser.php
File metadata and controls
89 lines (70 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace Vimeo\MysqlEngine\Parser;
use Vimeo\MysqlEngine\Query\AlterTableAutoincrementQuery;
use Vimeo\MysqlEngine\Query\ShowColumnsQuery;
use Vimeo\MysqlEngine\Query\ShowIndexQuery;
use Vimeo\MysqlEngine\Query\ShowTablesQuery;
use Vimeo\MysqlEngine\TokenType;
/**
* Very limited parser for ALTER TABLE {table} AUTO_INCREMENT=1
*/
final class AlterTableParser
{
/**
* @var int
*/
private $pointer = 0;
/**
* @var array<int, Token>
*/
private $tokens;
/**
* @var string
*/
private $sql;
/**
* @param array<int, Token> $tokens
*/
public function __construct(array $tokens, string $sql)
{
$this->tokens = $tokens;
$this->sql = $sql;
}
/**
* @return AlterTableAutoincrementQuery
* @throws ParserException
*/
public function parse()
{
if ($this->tokens[$this->pointer]->value !== 'ALTER') {
throw new ParserException("Parser error: expected ALTER");
}
$this->pointer++;
if ($this->tokens[$this->pointer]->value !== 'TABLE') {
throw new ParserException("Parser error: expected ALTER TABLE");
}
$this->pointer++;
if ($this->tokens[$this->pointer]->type !== TokenType::IDENTIFIER) {
throw new ParserException("Expected table name after TABLE");
}
$table = $this->tokens[$this->pointer]->value;
$this->pointer++;
switch ($this->tokens[$this->pointer]->value) {
case 'AUTO_INCREMENT':
return $this->parseAlterTableAutoIncrement($table);
}
}
private function parseAlterTableAutoIncrement(string $table): AlterTableAutoincrementQuery
{
$this->pointer++;
if ($this->tokens[$this->pointer]->value !== '=') {
throw new ParserException("Parser error: expected ALTER TABLE {table} AUTO_INCREMENT=");
}
$this->pointer++;
if ($this->tokens[$this->pointer]->type !== TokenType::NUMERIC_CONSTANT) {
throw new ParserException("Expected numeric after =");
}
$token = $this->tokens[$this->pointer] ?? null;
return new AlterTableAutoincrementQuery($table, $token->value, $this->sql);
}
}