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.
This commit is contained in:
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace SparkPlug\Com\Noccy\Pdo\Reflection\Reflector;
|
||||
|
||||
use PDO;
|
||||
use SparkPlug\Com\Noccy\Pdo\PdoResource;
|
||||
use SparkPlug\Com\Noccy\Pdo\Reflection\ColumnReflectionInterface;
|
||||
|
||||
class MysqlColumnReflection implements ColumnReflectionInterface
|
||||
{
|
||||
|
||||
private array $meta;
|
||||
|
||||
public function __construct(PdoResource $db, array $meta)
|
||||
{
|
||||
$this->meta = $meta;
|
||||
}
|
||||
|
||||
public function isPrimaryKey(): bool
|
||||
{
|
||||
return str_contains($this->meta['Key']??null, "PRI");
|
||||
}
|
||||
|
||||
public function isNullable(): bool
|
||||
{
|
||||
return ($this->meta['Null']??null) == "YES";
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->meta['Field'];
|
||||
}
|
||||
|
||||
public function getType(): string
|
||||
{
|
||||
return $this->meta['Type'];
|
||||
}
|
||||
|
||||
public function getDefaultValue(): mixed
|
||||
{
|
||||
return $this->meta['Default'] ?? null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace SparkPlug\Com\Noccy\Pdo\Reflection\Reflector;
|
||||
|
||||
use PDO;
|
||||
use SparkPlug\Com\Noccy\Pdo\PdoResource;
|
||||
use SparkPlug\Com\Noccy\Pdo\Reflection\DatabaseReflectionInterface;
|
||||
use SparkPlug\Com\Noccy\Pdo\Reflection\TableReflectionInterface;
|
||||
|
||||
class MysqlDatabaseReflection implements DatabaseReflectionInterface
|
||||
{
|
||||
|
||||
private PDO $pdo;
|
||||
|
||||
private array $tables = [];
|
||||
|
||||
public function __construct(PdoResource $db)
|
||||
{
|
||||
$pdo = $db->getPDO();
|
||||
|
||||
$tableQuery = $pdo->prepare('show full tables');
|
||||
$tableQuery->execute();
|
||||
$tables = $tableQuery->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
foreach ($tables as $table) {
|
||||
$name = reset($table);
|
||||
$this->tables[$name] = new MysqlTableReflection($db, $name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getAllTables(): array
|
||||
{
|
||||
return $this->tables;
|
||||
}
|
||||
|
||||
public function getTable(string $name): ?TableReflectionInterface
|
||||
{
|
||||
return $this->tables[$name] ?? null;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace SparkPlug\Com\Noccy\Pdo\Reflection\Reflector;
|
||||
|
||||
use SparkPlug\Com\Noccy\Pdo\PdoResource;
|
||||
use SparkPlug\Com\Noccy\Pdo\Reflection\DatabaseReflectionInterface;
|
||||
use SparkPlug\Com\Noccy\Pdo\Reflection\TableReflectionInterface;
|
||||
|
||||
class MysqlReflector implements ReflectorInterface
|
||||
{
|
||||
private PdoResource $db;
|
||||
|
||||
public function __construct(PdoResource $db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
public function createDatabaseReflection(): DatabaseReflectionInterface
|
||||
{
|
||||
return new MysqlDatabaseReflection($this->db);
|
||||
}
|
||||
|
||||
public function createTableReflection(string $table): TableReflectionInterface
|
||||
{
|
||||
return new MysqlTableReflection($this->db, $table);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,56 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace SparkPlug\Com\Noccy\Pdo\Reflection\Reflector;
|
||||
|
||||
use SparkPlug\Com\Noccy\Pdo\PdoResource;
|
||||
use SparkPlug\Com\Noccy\Pdo\Reflection\DatabaseReflectionInterface;
|
||||
use SparkPlug\Com\Noccy\Pdo\Reflection\TableReflectionInterface;
|
||||
|
||||
interface ReflectorInterface
|
||||
{
|
||||
public function createDatabaseReflection(): DatabaseReflectionInterface;
|
||||
|
||||
public function createTableReflection(string $table): TableReflectionInterface;
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace SparkPlug\Com\Noccy\Pdo\Reflection\Reflector;
|
||||
|
||||
use PDO;
|
||||
use SparkPlug\Com\Noccy\Pdo\PdoResource;
|
||||
use SparkPlug\Com\Noccy\Pdo\Reflection\ColumnReflectionInterface;
|
||||
|
||||
class SqliteColumnReflection implements ColumnReflectionInterface
|
||||
{
|
||||
|
||||
private array $meta;
|
||||
|
||||
public function __construct(PdoResource $db, array $meta)
|
||||
{
|
||||
$this->meta = $meta;
|
||||
}
|
||||
|
||||
public function isPrimaryKey(): bool
|
||||
{
|
||||
return (bool)$this->meta['pk'] ?? false;
|
||||
}
|
||||
|
||||
public function isNullable(): bool
|
||||
{
|
||||
return !($this->meta['notnull'] ?? 0);
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->meta['name'];
|
||||
}
|
||||
|
||||
public function getType(): string
|
||||
{
|
||||
return $this->meta['type'];
|
||||
}
|
||||
|
||||
public function getDefaultValue(): mixed
|
||||
{
|
||||
return $this->meta['dflt_value'] ?? null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace SparkPlug\Com\Noccy\Pdo\Reflection\Reflector;
|
||||
|
||||
use PDO;
|
||||
use SparkPlug\Com\Noccy\Pdo\PdoResource;
|
||||
use SparkPlug\Com\Noccy\Pdo\Reflection\DatabaseReflectionInterface;
|
||||
use SparkPlug\Com\Noccy\Pdo\Reflection\TableReflectionInterface;
|
||||
|
||||
class SqliteDatabaseReflection implements DatabaseReflectionInterface
|
||||
{
|
||||
|
||||
private PDO $pdo;
|
||||
|
||||
private array $tables = [];
|
||||
|
||||
public function __construct(PdoResource $db, SqliteReflector $reflector)
|
||||
{
|
||||
$pdo = $db->getPDO();
|
||||
|
||||
//$tableQuery = $pdo->prepare("SELECT name FROM sqlite_schema WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' ORDER BY 1;");
|
||||
$tableQuery = $pdo->prepare("SELECT * FROM sqlite_schema ORDER BY 1;");
|
||||
$tableQuery->execute();
|
||||
$tables = $tableQuery->fetchAll(PDO::FETCH_ASSOC);
|
||||
foreach ($tables as $tinfo) {
|
||||
$tname = $tinfo['name'];
|
||||
$this->tables[$tname] = $reflector->createTableReflection($tname);
|
||||
}
|
||||
}
|
||||
|
||||
public function getAllTables(): array
|
||||
{
|
||||
return $this->tables;
|
||||
}
|
||||
|
||||
public function getTable(string $name): ?TableReflectionInterface
|
||||
{
|
||||
return $this->tables[$name] ?? null;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace SparkPlug\Com\Noccy\Pdo\Reflection\Reflector;
|
||||
|
||||
use SparkPlug\Com\Noccy\Pdo\PdoResource;
|
||||
use SparkPlug\Com\Noccy\Pdo\Reflection\DatabaseReflectionInterface;
|
||||
use SparkPlug\Com\Noccy\Pdo\Reflection\TableReflectionInterface;
|
||||
|
||||
class SqliteReflector implements ReflectorInterface
|
||||
{
|
||||
private PdoResource $db;
|
||||
|
||||
public function __construct(PdoResource $db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
public function createDatabaseReflection(): DatabaseReflectionInterface
|
||||
{
|
||||
return new SqliteDatabaseReflection($this->db, $this);
|
||||
}
|
||||
|
||||
public function createTableReflection(string $table): TableReflectionInterface
|
||||
{
|
||||
return new SqliteTableReflection($this->db, $table);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,46 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user