-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add handling of command SHOW FULL COLUMNS FOR {table_name}
Handling multiple databases for create queries, then sql is CREATE TABLE `database`.`table` and in same script is `information_schema`.`table` Fixing PDO::getAttribute() missing method
- Loading branch information
Andrew Svirin
committed
Oct 5, 2021
1 parent
a0005ec
commit ad3113e
Showing
7 changed files
with
236 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace Vimeo\MysqlEngine\Processor; | ||
|
||
use Vimeo\MysqlEngine\FakePdoInterface; | ||
use Vimeo\MysqlEngine\Query\ShowColumnsQuery; | ||
use Vimeo\MysqlEngine\Query\ShowIndexQuery; | ||
use Vimeo\MysqlEngine\Schema\Column; | ||
use function PHPUnit\Framework\assertIsArray; | ||
|
||
class ShowColumnsProcessor extends Processor | ||
{ | ||
public static function process( | ||
FakePdoInterface $conn, | ||
Scope $scope, | ||
ShowColumnsQuery $stmt | ||
): QueryResult { | ||
[$database, $table] = Processor::parseTableName($conn, $stmt->table); | ||
$table_definition = $conn->getServer()->getTableDefinition( | ||
$database, | ||
$table | ||
); | ||
if (!$table_definition) { | ||
return new QueryResult([], []); | ||
} | ||
$columns = [ | ||
'Field' => new Column\Varchar(255), | ||
'Type' => new Column\Varchar(255), | ||
'Collation' => new Column\Varchar(255), | ||
'Null' => new Column\Enum(['NO', 'YES']), | ||
'Key' => new Column\Enum(['PRI']), | ||
'Default' => new Column\Varchar(255), | ||
'Extra' => new Column\Enum(['auto_increment']), | ||
'Privilegies' => new Column\Varchar(255), | ||
'Comment' => new Column\Varchar(255), | ||
]; | ||
$rows = []; | ||
foreach ($table_definition->columns as $name => $column) { | ||
$rows[] = [ | ||
'Field' => $name, | ||
'Type' => self::resolveType($column), | ||
'Collation' => self::resolveCollation($column), | ||
'Null' => $column->isNullable() ? 'YES' : 'NO', | ||
'Key' => in_array($name, $table_definition->primaryKeyColumns) ? 'PRI' : '', | ||
'Default' => $column->getDefault(), | ||
'Extra' => self::resolveExtra($column), | ||
'Privilegies' => 'select,insert,update,references', | ||
'Comment' => '', | ||
]; | ||
} | ||
$result = self::applyWhere($conn, $scope, $stmt->whereClause, new QueryResult($rows, $columns)); | ||
|
||
$rows = array_merge($result->rows); | ||
$columns = $result->columns; | ||
if (!$stmt->isFull) { | ||
$allowedColumns = [ | ||
'Field', | ||
'Type', | ||
'Null', | ||
'Key', | ||
'Default', | ||
'Extra', | ||
]; | ||
$columns = array_intersect_key($columns, array_flip($allowedColumns)); | ||
} | ||
|
||
return new QueryResult(array_merge($result->rows), $result->columns); | ||
} | ||
|
||
private static function resolveType(Column $column): string | ||
{ | ||
if ($column instanceof Column\Varchar) { | ||
$type = 'varchar(255)'; | ||
} elseif ($column instanceof Column\IntColumn) { | ||
$type = 'int(11)'; | ||
} elseif ($column instanceof Column\DateTime) { | ||
$type = 'datetime'; | ||
} else { | ||
throw new \UnexpectedValueException('Column type not specified.'); | ||
} | ||
|
||
return $type; | ||
} | ||
|
||
private static function resolveCollation(Column $column): string | ||
{ | ||
if (is_subclass_of($column, Column\CharacterColumn::class)) { | ||
$collation = $column->getCollation(); | ||
} | ||
|
||
return $collation ?? ''; | ||
} | ||
|
||
private static function resolveKey(Column $column): string | ||
{ | ||
|
||
} | ||
|
||
private static function resolveDefault(Column $column): ?string | ||
{ | ||
if ($column instanceof Column\Defaultable) { | ||
$default = $column->getDefault(); | ||
} | ||
|
||
return $default ?? null; | ||
} | ||
|
||
private static function resolveExtra(Column $column): string | ||
{ | ||
if ($column instanceof Column\IntegerColumn) { | ||
$extra = $column->isAutoIncrement() ? 'auto_increment' : ''; | ||
} | ||
|
||
return $extra ?? ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Vimeo\MysqlEngine\Query; | ||
|
||
use Vimeo\MysqlEngine\Query\Expression\Expression; | ||
|
||
final class ShowColumnsQuery | ||
{ | ||
/** | ||
* @var bool | ||
*/ | ||
public $isFull = false; | ||
|
||
/** | ||
* @var ?Expression | ||
*/ | ||
public $whereClause = null; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $table; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $sql; | ||
|
||
public function __construct(string $table, string $sql) | ||
{ | ||
$this->table = $table; | ||
$this->sql = $sql; | ||
} | ||
} |