<?php
namespace App;
use PDO;
class SchemaInspector
{
private PDO $pdo;
public function __construct(PDO $pdo)
{
$this->pdo = $pdo;
}
public function getTables(): array
{
$stmt = $this->pdo->query('SHOW TABLES');
return $stmt->fetchAll(PDO::FETCH_COLUMN);
}
public function getColumns(string $table): array
{
$stmt = $this->pdo->prepare("SHOW COLUMNS FROM `$table`");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function getPrimaryKey(string $table): ?string
{
$columns = $this->getColumns($table);
foreach ($columns as $column) {
if ($column['Key'] === 'PRI') {
return $column['Field'];
}
}
return null;
}
}
|