php-spark/plugins/com.noccy.pdo/Reflection/Reflector/MysqlTableReflection.php
Christopher Vagnetoft 538383c33d PDO plugin: Reflections
* com.noccy.pdo: Implemented reflection for PDO databases,
  tables and columns. Reflectors for MySQL and Sqlite.
* com.noccy.pdo: Added pdo:inspect command.
* com.noccy.docker: Added basic stack management and commands.
* com.noccy.docker: Moved commands to dedicated namespace.
* Environment: readConfig and writeConfig helper added, with
  a flag to use the global config dir ~/.config/spark.
2021-12-24 01:27:57 +01:00

57 lines
1.4 KiB
PHP

<?php
namespace SparkPlug\Com\Noccy\Pdo\Reflection\Reflector;
use PDO;
use SparkPlug\Com\Noccy\Pdo\PdoResource;
use SparkPlug\Com\Noccy\Pdo\Reflection\ColumnReflectionInterface;
use SparkPlug\Com\Noccy\Pdo\Reflection\TableReflectionInterface;
class MysqlTableReflection implements TableReflectionInterface
{
private PDO $pdo;
private string $table;
private array $columns = [];
public function __construct(PdoResource $db, string $table)
{
$pdo = $db->getPDO();
$columnQuery = $pdo->prepare('show fields from '.$table);
$columnQuery->execute();
$columns = $columnQuery->fetchAll(PDO::FETCH_ASSOC);
foreach ($columns as $column) {
$name = $column['Field'];
$this->columns[$name] = new MysqlColumnReflection($db, $column);
}
/*
SELECT table_schema, table_name, column_name, ordinal_position, data_type,
numeric_precision, column_type, column_default, is_nullable, column_comment
FROM information_schema.columns
WHERE (table_schema='schema_name' and table_name = 'table_name')
order by ordinal_position;
OR
show fields from 'table_name'
*/
}
public function getAllColumns(): array
{
return $this->columns;
}
public function getColumn(string $name): ?ColumnReflectionInterface
{
return $this->columns[$name] ?? null;
}
}