php-spark/plugins/com.noccy.pdo/Reflection/Reflector/SqliteTableReflection.php

47 lines
1.3 KiB
PHP
Raw Normal View History

<?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;
}
}