|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Vimeo\MysqlEngine\Processor; |
| 4 | + |
| 5 | +use Vimeo\MysqlEngine\FakePdoInterface; |
| 6 | +use Vimeo\MysqlEngine\Query\ShowColumnsQuery; |
| 7 | +use Vimeo\MysqlEngine\Query\ShowIndexQuery; |
| 8 | +use Vimeo\MysqlEngine\Schema\Column; |
| 9 | +use function PHPUnit\Framework\assertIsArray; |
| 10 | + |
| 11 | +class ShowColumnsProcessor extends Processor |
| 12 | +{ |
| 13 | + public static function process( |
| 14 | + FakePdoInterface $conn, |
| 15 | + Scope $scope, |
| 16 | + ShowColumnsQuery $stmt |
| 17 | + ): QueryResult { |
| 18 | + [$database, $table] = Processor::parseTableName($conn, $stmt->table); |
| 19 | + $table_definition = $conn->getServer()->getTableDefinition( |
| 20 | + $database, |
| 21 | + $table |
| 22 | + ); |
| 23 | + if (!$table_definition) { |
| 24 | + return new QueryResult([], []); |
| 25 | + } |
| 26 | + $columns = [ |
| 27 | + 'Field' => new Column\Varchar(255), |
| 28 | + 'Type' => new Column\Varchar(255), |
| 29 | + 'Collation' => new Column\Varchar(255), |
| 30 | + 'Null' => new Column\Enum(['NO', 'YES']), |
| 31 | + 'Key' => new Column\Enum(['PRI']), |
| 32 | + 'Default' => new Column\Varchar(255), |
| 33 | + 'Extra' => new Column\Enum(['auto_increment']), |
| 34 | + 'Privilegies' => new Column\Varchar(255), |
| 35 | + 'Comment' => new Column\Varchar(255), |
| 36 | + ]; |
| 37 | + $rows = []; |
| 38 | + foreach ($table_definition->columns as $name => $column) { |
| 39 | + $rows[] = [ |
| 40 | + 'Field' => $name, |
| 41 | + 'Type' => self::resolveType($column), |
| 42 | + 'Collation' => self::resolveCollation($column), |
| 43 | + 'Null' => $column->isNullable() ? 'YES' : 'NO', |
| 44 | + 'Key' => in_array($name, $table_definition->primaryKeyColumns) ? 'PRI' : '', |
| 45 | + 'Default' => $column->getDefault(), |
| 46 | + 'Extra' => self::resolveExtra($column), |
| 47 | + 'Privilegies' => 'select,insert,update,references', |
| 48 | + 'Comment' => '', |
| 49 | + ]; |
| 50 | + } |
| 51 | + $result = self::applyWhere($conn, $scope, $stmt->whereClause, new QueryResult($rows, $columns)); |
| 52 | + |
| 53 | + $rows = array_merge($result->rows); |
| 54 | + $columns = $result->columns; |
| 55 | + if (!$stmt->isFull) { |
| 56 | + $allowedColumns = [ |
| 57 | + 'Field', |
| 58 | + 'Type', |
| 59 | + 'Null', |
| 60 | + 'Key', |
| 61 | + 'Default', |
| 62 | + 'Extra', |
| 63 | + ]; |
| 64 | + $columns = array_intersect_key($columns, array_flip($allowedColumns)); |
| 65 | + } |
| 66 | + |
| 67 | + return new QueryResult(array_merge($result->rows), $result->columns); |
| 68 | + } |
| 69 | + |
| 70 | + private static function resolveType(Column $column): string |
| 71 | + { |
| 72 | + if ($column instanceof Column\Varchar) { |
| 73 | + $type = 'varchar(255)'; |
| 74 | + } elseif ($column instanceof Column\IntColumn) { |
| 75 | + $type = 'int(11)'; |
| 76 | + } elseif ($column instanceof Column\DateTime) { |
| 77 | + $type = 'datetime'; |
| 78 | + } else { |
| 79 | + throw new \UnexpectedValueException('Column type not specified.'); |
| 80 | + } |
| 81 | + |
| 82 | + return $type; |
| 83 | + } |
| 84 | + |
| 85 | + private static function resolveCollation(Column $column): string |
| 86 | + { |
| 87 | + if (is_subclass_of($column, Column\CharacterColumn::class)) { |
| 88 | + $collation = $column->getCollation(); |
| 89 | + } |
| 90 | + |
| 91 | + return $collation ?? ''; |
| 92 | + } |
| 93 | + |
| 94 | + private static function resolveKey(Column $column): string |
| 95 | + { |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + private static function resolveDefault(Column $column): ?string |
| 100 | + { |
| 101 | + if ($column instanceof Column\Defaultable) { |
| 102 | + $default = $column->getDefault(); |
| 103 | + } |
| 104 | + |
| 105 | + return $default ?? null; |
| 106 | + } |
| 107 | + |
| 108 | + private static function resolveExtra(Column $column): string |
| 109 | + { |
| 110 | + if ($column instanceof Column\IntegerColumn) { |
| 111 | + $extra = $column->isAutoIncrement() ? 'auto_increment' : ''; |
| 112 | + } |
| 113 | + |
| 114 | + return $extra ?? ''; |
| 115 | + } |
| 116 | +} |
0 commit comments