1
+ <?php
2
+
3
+
4
+ namespace Vimeo \MysqlEngine \Processor ;
5
+
6
+
7
+ use Vimeo \MysqlEngine \FakePdoInterface ;
8
+ use Vimeo \MysqlEngine \Query \ShowIndexQuery ;
9
+ use Vimeo \MysqlEngine \Schema \Column ;
10
+ use function PHPUnit \Framework \assertIsArray ;
11
+
12
+ class ShowIndexProcessor extends Processor
13
+ {
14
+ public static function process (
15
+ FakePdoInterface $ conn ,
16
+ Scope $ scope ,
17
+ ShowIndexQuery $ stmt
18
+ ): QueryResult {
19
+ [$ database , $ table ] = Processor::parseTableName ($ conn , $ stmt ->table );
20
+ $ table_definition = $ conn ->getServer ()->getTableDefinition (
21
+ $ database ,
22
+ $ table
23
+ );
24
+ if (!$ table_definition ) {
25
+ return new QueryResult ([], []);
26
+ }
27
+ $ columns = [
28
+ 'Table ' => new Column \Varchar (255 ),
29
+ 'Non_unique ' => new Column \TinyInt (true , 1 ),
30
+ 'Key_name ' => new Column \Varchar (255 ),
31
+ 'Seq_in_index ' => new Column \IntColumn (true , 4 ),
32
+ 'Column_name ' => new Column \Varchar (255 ),
33
+ 'Collation ' => new Column \Char (1 ),
34
+ 'Cardinality ' => new Column \IntColumn (true , 4 ),
35
+ 'Sub_part ' => new Column \IntColumn (true , 4 ),
36
+ 'Packed ' => new Column \TinyInt (true , 1 ),
37
+ 'Null ' => new Column \Varchar (3 ),
38
+ 'Index_type ' => new Column \Varchar (5 ),
39
+ 'Comment ' => new Column \Varchar (255 ),
40
+ 'Index_comment ' => new Column \Varchar (255 )
41
+ ];
42
+ $ rows = [];
43
+ foreach ($ table_definition ->indexes as $ name => $ index ) {
44
+ foreach ($ index ->columns as $ i => $ column ) {
45
+ $ rows [] = [
46
+ 'Table ' => $ table_definition ->name ,
47
+ 'Non_unique ' => $ index ->type === 'INDEX ' ? 1 : 0 ,
48
+ 'Key_name ' => $ name ,
49
+ 'Seq_in_index ' => 1 + (int ) $ i ,
50
+ 'Column_name ' => $ column ,
51
+ // because Index does not have "direction" (in the $cols of CreateIndex)
52
+ 'Collation ' => null ,
53
+ /*
54
+ * https://dev.mysql.com/doc/refman/8.0/en/analyze-table.html
55
+ * because ANALYZE TABLE is not implemented
56
+ */
57
+ 'Cardinality ' => null ,
58
+ // because Index does not have "length" (in the $cols of CreateIndex)
59
+ 'Sub_part ' => null ,
60
+ // because PACK_KEYS is not implemented
61
+ 'Packed ' => null ,
62
+ 'Null ' => $ table_definition ->columns [$ column ]->isNullable ? 'YES ' : '' ,
63
+ // because Index does not have $mode (in the CreateIndex)
64
+ 'Index_type ' => null ,
65
+ // because DISABLE KEYS is not implemented
66
+ 'Comment ' => '' ,
67
+ // because INDEX COMMENT is skipped in CREATE TABLE
68
+ 'Index_comment ' => ''
69
+ ];
70
+ }
71
+ }
72
+ $ result = self ::applyWhere ($ conn , $ scope , $ stmt ->whereClause , new QueryResult ($ rows , $ columns ));
73
+ return new QueryResult (array_merge ($ result ->rows ), $ result ->columns );
74
+ }
75
+ }
0 commit comments