Christopher Vagnetoft
538383c33d
* 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.
47 lines
1.3 KiB
PHP
47 lines
1.3 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 SqliteTableReflection implements TableReflectionInterface
|
|
{
|
|
|
|
private PDO $pdo;
|
|
|
|
private array $columns = [];
|
|
|
|
public function __construct(PdoResource $db, string $table)
|
|
{
|
|
$pdo = $db->getPDO();
|
|
|
|
//$columnsQuery = $pdo->prepare("SELECT name FROM sqlite_schema WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' ORDER BY 1;");
|
|
$columnsQuery = $pdo->prepare("pragma table_info({$table})");
|
|
$columnsQuery->execute();
|
|
$columns = $columnsQuery->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if (count($columns) == 0) {
|
|
throw new \RuntimeException(sprintf("No such table %s in database", $table));
|
|
}
|
|
|
|
foreach ($columns as $column) {
|
|
$name = $column['name'];
|
|
$this->columns[$name] = new SqliteColumnReflection($db, $column);
|
|
}
|
|
}
|
|
|
|
public function getAllColumns(): array
|
|
{
|
|
return $this->columns;
|
|
}
|
|
|
|
public function getColumn(string $name): ?ColumnReflectionInterface
|
|
{
|
|
return $this->columns[$name] ?? null;
|
|
}
|
|
}
|
|
|