1
1
<?php
2
2
namespace Vimeo \MysqlEngine \Parser ;
3
3
4
+ use Vimeo \MysqlEngine \Query \ShowIndexQuery ;
4
5
use Vimeo \MysqlEngine \TokenType ;
5
6
use Vimeo \MysqlEngine \Query \ShowTablesQuery ;
6
7
@@ -33,18 +34,33 @@ public function __construct(array $tokens, string $sql)
33
34
$ this ->sql = $ sql ;
34
35
}
35
36
36
- public function parse () : ShowTablesQuery
37
+ /**
38
+ * @return ShowTablesQuery|ShowIndexQuery
39
+ * @throws ParserException
40
+ */
41
+ public function parse ()
37
42
{
38
43
if ($ this ->tokens [$ this ->pointer ]->value !== 'SHOW ' ) {
39
44
throw new ParserException ("Parser error: expected SHOW " );
40
45
}
41
46
42
47
$ this ->pointer ++;
43
48
44
- if ($ this ->tokens [$ this ->pointer ]->value !== 'TABLES ' ) {
45
- throw new ParserException ("Parser error: expected SHOW TABLES " );
49
+ switch ($ this ->tokens [$ this ->pointer ]->value ) {
50
+ case 'TABLES ' :
51
+ return $ this ->parseShowTables ();
52
+ case 'INDEX ' :
53
+ case 'INDEXES ' :
54
+ case 'KEYS ' :
55
+ return $ this ->parseShowIndex ();
56
+ default :
57
+ throw new ParserException ("Parser error: expected SHOW TABLES " );
46
58
}
47
59
60
+ }
61
+
62
+ private function parseShowTables ()
63
+ {
48
64
$ this ->pointer ++;
49
65
50
66
if ($ this ->tokens [$ this ->pointer ]->value !== 'LIKE ' ) {
@@ -61,4 +77,19 @@ public function parse() : ShowTablesQuery
61
77
62
78
return new ShowTablesQuery ($ token ->value , $ this ->sql );
63
79
}
80
+
81
+ private function parseShowIndex ()
82
+ {
83
+ $ this ->pointer ++;
84
+
85
+ if ($ this ->tokens [$ this ->pointer ]->value !== 'FROM ' ) {
86
+ throw new ParserException ("Parser error: expected SHOW INDEX FROM " );
87
+ }
88
+ $ this ->pointer ++;
89
+ $ token = $ this ->tokens [$ this ->pointer ];
90
+ if ($ token === null || $ token ->type !== TokenType::IDENTIFIER ) {
91
+ throw new ParserException ("Expected table name after FROM " );
92
+ }
93
+ return new ShowIndexQuery ($ token ->value , $ this ->sql );
94
+ }
64
95
}
0 commit comments