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:
		@@ -1,6 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker;
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker\Commands;
 | 
			
		||||
 | 
			
		||||
use Spark\Commands\Command;
 | 
			
		||||
use SparkPlug;
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker;
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker\Commands;
 | 
			
		||||
 | 
			
		||||
use Spark\Commands\Command;
 | 
			
		||||
use SparkPlug;
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker;
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker\Commands;
 | 
			
		||||
 | 
			
		||||
use Spark\Commands\Command;
 | 
			
		||||
use SparkPlug;
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker;
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker\Commands;
 | 
			
		||||
 | 
			
		||||
use Spark\Commands\Command;
 | 
			
		||||
use SparkPlug;
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker;
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker\Commands;
 | 
			
		||||
 | 
			
		||||
use Spark\Commands\Command;
 | 
			
		||||
use SparkPlug;
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker;
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker\Commands;
 | 
			
		||||
 | 
			
		||||
use Spark\Commands\Command;
 | 
			
		||||
use SparkPlug;
 | 
			
		||||
							
								
								
									
										44
									
								
								plugins/com.noccy.docker/Commands/Stack/RegisterCommand.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								plugins/com.noccy.docker/Commands/Stack/RegisterCommand.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker\Commands\Stack;
 | 
			
		||||
 | 
			
		||||
use Spark\Commands\Command;
 | 
			
		||||
use SparkPlug;
 | 
			
		||||
use SparkPlug\Com\Noccy\Docker\Stack\StackManager;
 | 
			
		||||
use Symfony\Component\Console\Input\InputArgument;
 | 
			
		||||
use Symfony\Component\Console\Input\InputInterface;
 | 
			
		||||
use Symfony\Component\Console\Output\OutputInterface;
 | 
			
		||||
 | 
			
		||||
class RegisterCommand extends Command
 | 
			
		||||
{
 | 
			
		||||
    protected function configure()
 | 
			
		||||
    {
 | 
			
		||||
        $this->setName("docker:stack:register")
 | 
			
		||||
            ->setDescription("Register a stack");
 | 
			
		||||
        
 | 
			
		||||
        $this->addArgument("options", InputArgument::IS_ARRAY, "key=value pairs of stack options");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function execute(InputInterface $input, OutputInterface $output)
 | 
			
		||||
    {
 | 
			
		||||
        $stacks = $this->getStackManager();
 | 
			
		||||
 | 
			
		||||
        $opts = [];
 | 
			
		||||
        foreach ($input->getArgument("options") as $opt) {
 | 
			
		||||
            if (str_contains($opt, "=")) {
 | 
			
		||||
                [$k,$v] = explode("=", $opt, 2);
 | 
			
		||||
                $opts[$k] = $v;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $root = $this->getEnvironment()->getProjectDirectory();
 | 
			
		||||
        $stacks->registerStack($root, $opts);
 | 
			
		||||
 | 
			
		||||
        return Command::SUCCESS;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function getStackManager(): StackManager
 | 
			
		||||
    {
 | 
			
		||||
        return new StackManager($this->getEnvironment());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										43
									
								
								plugins/com.noccy.docker/Commands/Stack/StatusCommand.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								plugins/com.noccy.docker/Commands/Stack/StatusCommand.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker\Commands\Stack;
 | 
			
		||||
 | 
			
		||||
use Spark\Commands\Command;
 | 
			
		||||
use SparkPlug;
 | 
			
		||||
use SparkPlug\Com\Noccy\Docker\Stack\StackManager;
 | 
			
		||||
use Symfony\Component\Console\Input\InputInterface;
 | 
			
		||||
use Symfony\Component\Console\Output\OutputInterface;
 | 
			
		||||
 | 
			
		||||
class StatusCommand extends Command
 | 
			
		||||
{
 | 
			
		||||
    protected function configure()
 | 
			
		||||
    {
 | 
			
		||||
        $this->setName("docker:stack:status")
 | 
			
		||||
            ->setDescription("Show status on registered stacks");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function execute(InputInterface $input, OutputInterface $output)
 | 
			
		||||
    {
 | 
			
		||||
        $stackManager = $this->getStackManager();
 | 
			
		||||
        $stacks = $stackManager->getRegisteredStacks();
 | 
			
		||||
 | 
			
		||||
        foreach ($stacks as $stack) {
 | 
			
		||||
            $output->writeln("<fg=yellow>\u{2bbb}</> <fg=white;options=bold>{$stack->getName()}</>");
 | 
			
		||||
            $table = $stack->getContainersTable($output);
 | 
			
		||||
            $table->setStyle('compact');
 | 
			
		||||
            $table->setColumnWidths([ 30, 20, 20, 0 ]);
 | 
			
		||||
            $table->render();
 | 
			
		||||
            $output->writeln("");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return Command::SUCCESS;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    private function getStackManager(): StackManager
 | 
			
		||||
    {
 | 
			
		||||
        return new StackManager($this->getEnvironment());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -0,0 +1,35 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker\Commands\Stack;
 | 
			
		||||
 | 
			
		||||
use Spark\Commands\Command;
 | 
			
		||||
use SparkPlug;
 | 
			
		||||
use SparkPlug\Com\Noccy\Docker\Stack\StackManager;
 | 
			
		||||
use Symfony\Component\Console\Input\InputArgument;
 | 
			
		||||
use Symfony\Component\Console\Input\InputInterface;
 | 
			
		||||
use Symfony\Component\Console\Output\OutputInterface;
 | 
			
		||||
 | 
			
		||||
class UnregisterCommand extends Command
 | 
			
		||||
{
 | 
			
		||||
    protected function configure()
 | 
			
		||||
    {
 | 
			
		||||
        $this->setName("docker:stack:unregister")
 | 
			
		||||
            ->setDescription("Unregister a stack");
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function execute(InputInterface $input, OutputInterface $output)
 | 
			
		||||
    {
 | 
			
		||||
        $stacks = $this->getStackManager();
 | 
			
		||||
 | 
			
		||||
        $root = $this->getEnvironment()->getProjectDirectory();
 | 
			
		||||
        $stacks->removeStack($root);
 | 
			
		||||
 | 
			
		||||
        return Command::SUCCESS;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function getStackManager(): StackManager
 | 
			
		||||
    {
 | 
			
		||||
        return new StackManager($this->getEnvironment());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										89
									
								
								plugins/com.noccy.docker/Stack/Stack.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								plugins/com.noccy.docker/Stack/Stack.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,89 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker\Stack;
 | 
			
		||||
 | 
			
		||||
use Spark\Environment\Environment;
 | 
			
		||||
use Symfony\Component\Console\Helper\Table;
 | 
			
		||||
use Symfony\Component\Console\Output\OutputInterface;
 | 
			
		||||
 | 
			
		||||
class Stack
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    const BULLET="\u{25cf}";
 | 
			
		||||
 | 
			
		||||
    private string $path;
 | 
			
		||||
 | 
			
		||||
    private array $options = [];
 | 
			
		||||
 | 
			
		||||
    public function __construct(string $path, array $options)
 | 
			
		||||
    {
 | 
			
		||||
        $this->path = $path;
 | 
			
		||||
        $this->options = $options;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getPath(): string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->path;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getName(): string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->options['name'] ?? basename($this->path);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getComposeFile(): string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->path . "/" . ($this->options['compose']??"docker-compose.yml");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getContainersTable(OutputInterface $output): Table
 | 
			
		||||
    {
 | 
			
		||||
        exec("docker-compose -f " . escapeshellarg($this->getComposeFile()) . " ps -q", $ids, $ret);
 | 
			
		||||
 | 
			
		||||
        if (count($ids) === 0) {
 | 
			
		||||
            $json = [];
 | 
			
		||||
        } else {
 | 
			
		||||
            exec("docker inspect ".join(" ",$ids), $out, $ret);
 | 
			
		||||
            $json = json_decode(join("", $out));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $table = new Table($output);
 | 
			
		||||
        $table->setStyle("box");
 | 
			
		||||
        $table->setHeaders([ "Name", "Status", "Image", "Ports" ]);
 | 
			
		||||
        foreach ($json as $container) {
 | 
			
		||||
            $startedTs = preg_replace('/(\.([0-9]+)Z)$/', '+0100', $container->State->StartedAt);
 | 
			
		||||
            $s = date_parse($startedTs);
 | 
			
		||||
            $started = mktime($s['hour'], $s['minute'], $s['second'], $s['month'], $s['day'], $s['year']) + 3600;
 | 
			
		||||
            if ($container->State->Dead) {
 | 
			
		||||
                $status = "<fg=red>".self::BULLET."</> ".$container->State->Status;
 | 
			
		||||
            } elseif ($container->State->Restarting) {
 | 
			
		||||
                $status = "<fg=yellow>".self::BULLET."</> ".$container->State->Status;
 | 
			
		||||
            } elseif ($container->State->Running) {
 | 
			
		||||
                $elapsed = time() - $started;
 | 
			
		||||
                if ($elapsed > 60) {
 | 
			
		||||
                    $em = floor($elapsed / 60);
 | 
			
		||||
                    $es = $elapsed - ($em * 60);
 | 
			
		||||
                    if ($em>60)  {
 | 
			
		||||
                        $eh = floor($em / 60);
 | 
			
		||||
                        $em = $em - ($eh * 60);
 | 
			
		||||
                        $elapsed = sprintf("%dh%dm%ds", $eh, $em, $es);
 | 
			
		||||
                    } else {
 | 
			
		||||
                        $elapsed = sprintf("%dm%ds", $em, $es);
 | 
			
		||||
                    }
 | 
			
		||||
                } else {
 | 
			
		||||
                    $elapsed = sprintf("%ds", $elapsed);
 | 
			
		||||
                }
 | 
			
		||||
                $status = "<fg=green>".self::BULLET."</> ".$container->State->Status." (<fg=green>{$elapsed}</>)";
 | 
			
		||||
            } else {
 | 
			
		||||
                $status = "<fg=red>".self::BULLET."</> ".$container->State->Status;
 | 
			
		||||
            }
 | 
			
		||||
            $ports = $container->Config->ExposedPorts??[];
 | 
			
		||||
            $ports = array_keys((array)$ports);
 | 
			
		||||
            $table->addRow([ $container->Name, $status, $container->Config->Image, join(", ", $ports) ]);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        return $table;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										64
									
								
								plugins/com.noccy.docker/Stack/StackManager.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								plugins/com.noccy.docker/Stack/StackManager.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,64 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Docker\Stack;
 | 
			
		||||
 | 
			
		||||
use Spark\Environment\Environment;
 | 
			
		||||
 | 
			
		||||
class StackManager
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    private Environment $environment;
 | 
			
		||||
 | 
			
		||||
    private array $stacks = [];
 | 
			
		||||
 | 
			
		||||
    private array $stackObjects = [];
 | 
			
		||||
 | 
			
		||||
    public function __construct(Environment $environment)
 | 
			
		||||
    {
 | 
			
		||||
        $this->environment = $environment;
 | 
			
		||||
        $this->readConfig();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function readConfig()
 | 
			
		||||
    {
 | 
			
		||||
        $config = $this->environment->readConfig("com.noccy.docker/stacks.json", true);
 | 
			
		||||
        if (!$config) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->stacks = $config['stacks'] ?? [];
 | 
			
		||||
        foreach ($this->stacks as $i=>$stack) {
 | 
			
		||||
            $this->stackObjects[$i] = new Stack($stack['path'], $stack['options']);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function flushConfig()
 | 
			
		||||
    {
 | 
			
		||||
        $config = [
 | 
			
		||||
            'stacks' => $this->stacks,
 | 
			
		||||
        ];
 | 
			
		||||
        $this->environment->writeConfig("com.noccy.docker/stacks.json", $config, true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function registerStack(string $path, array $options=[])
 | 
			
		||||
    {
 | 
			
		||||
        $this->stacks[$path] = [
 | 
			
		||||
            'path' => $path,
 | 
			
		||||
            'options' => $options
 | 
			
		||||
        ];
 | 
			
		||||
        $this->flushConfig();
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function removeStack(string $path)
 | 
			
		||||
    {
 | 
			
		||||
        unset($this->stacks[$path]);
 | 
			
		||||
        $this->flushConfig();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getRegisteredStacks(): array
 | 
			
		||||
    {
 | 
			
		||||
        return $this->stackObjects;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -27,15 +27,19 @@ class DockerPlug extends SparkPlug
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ($hasCompose || $hasBuild) {
 | 
			
		||||
            register_command(new DockerUpCommand);
 | 
			
		||||
            register_command(new DockerDownCommand);
 | 
			
		||||
            register_command(new DockerStatusCommand);
 | 
			
		||||
            register_command(new Commands\DockerUpCommand);
 | 
			
		||||
            register_command(new Commands\DockerDownCommand);
 | 
			
		||||
            register_command(new Commands\DockerStatusCommand);
 | 
			
		||||
            register_command(new Commands\DockerDbExportCommand);
 | 
			
		||||
        }
 | 
			
		||||
        if ($hasBuild) {
 | 
			
		||||
            register_command(new DockerBuildCommand);
 | 
			
		||||
            register_command(new DockerExecCommand);
 | 
			
		||||
            register_command(new Commands\DockerBuildCommand);
 | 
			
		||||
            register_command(new Commands\DockerExecCommand);
 | 
			
		||||
        }
 | 
			
		||||
        register_command(new DockerDbExportCommand);
 | 
			
		||||
 | 
			
		||||
        register_command(new Commands\Stack\StatusCommand);
 | 
			
		||||
        register_command(new Commands\Stack\RegisterCommand);
 | 
			
		||||
        register_command(new Commands\Stack\UnregisterCommand);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getComposeStack(): ?Stack
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										79
									
								
								plugins/com.noccy.pdo/Commands/PdoInspectCommand.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								plugins/com.noccy.pdo/Commands/PdoInspectCommand.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,79 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Pdo\Commands;
 | 
			
		||||
 | 
			
		||||
use Spark\Commands\Command;
 | 
			
		||||
use SparkPlug\Com\Noccy\Pdo\Reflection\TableReflectionInterface;
 | 
			
		||||
use Symfony\Component\Console\Helper\Table;
 | 
			
		||||
use Symfony\Component\Console\Input\InputArgument;
 | 
			
		||||
use Symfony\Component\Console\Input\InputInterface;
 | 
			
		||||
use Symfony\Component\Console\Input\InputOption;
 | 
			
		||||
use Symfony\Component\Console\Output\OutputInterface;
 | 
			
		||||
 | 
			
		||||
class PdoInspectCommand extends Command {
 | 
			
		||||
 | 
			
		||||
    protected function configure() {
 | 
			
		||||
        $this->setName("pdo:inspect");
 | 
			
		||||
        $this->setDescription("Inspect the database, a table, or a row");
 | 
			
		||||
        $this->addOption("res", "r", InputOption::VALUE_REQUIRED, "Resource to query", "db");
 | 
			
		||||
        $this->addArgument("table", InputArgument::OPTIONAL, "Table name to inspect");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function execute(InputInterface $input, OutputInterface $output)
 | 
			
		||||
    {
 | 
			
		||||
        $sourceName = $input->getOption("res");
 | 
			
		||||
        $source = get_resource($sourceName);
 | 
			
		||||
        if (!$source) {
 | 
			
		||||
            $output->writeln("<error>Invalid resource: {$source}</>");
 | 
			
		||||
            return Command::INVALID;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $reflector = $source->getReflector();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        $tableName = $input->getArgument("table");
 | 
			
		||||
        if ($tableName) {
 | 
			
		||||
            try {
 | 
			
		||||
                $table = $reflector->createTableReflection($tableName);
 | 
			
		||||
                if ($table) {
 | 
			
		||||
                    $this->dumpTable($tableName, $table, $output);
 | 
			
		||||
                }
 | 
			
		||||
            } catch (\Exception $e) {
 | 
			
		||||
                $output->writeln("<error>{$e->getMessage()}</>");
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            try {
 | 
			
		||||
                $database = $reflector->createDatabaseReflection();
 | 
			
		||||
                foreach ($database->getAllTables() as $tableName=>$table) {
 | 
			
		||||
                    $this->dumpTable($tableName, $table, $output);
 | 
			
		||||
                }
 | 
			
		||||
            } catch (\Exception $e) {
 | 
			
		||||
                $output->writeln("<error>{$e->getMessage()}</>");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return Command::SUCCESS;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function dumpTable(string $name, TableReflectionInterface $table, OutputInterface $output)
 | 
			
		||||
    {
 | 
			
		||||
        $output->writeln("<options=bold>{$name}</>");
 | 
			
		||||
        $t = new Table($output); 
 | 
			
		||||
        $t->setStyle('compact');
 | 
			
		||||
        $t->setHeaders([ "Name", "Type", "PK", "NULL", "Default" ]);
 | 
			
		||||
        $t->setColumnWidth(0, 30);
 | 
			
		||||
        $t->setColumnWidth(1, 30);
 | 
			
		||||
        foreach ($table->getAllColumns() as $column) {
 | 
			
		||||
            $t->addRow([
 | 
			
		||||
                $column->getName(),
 | 
			
		||||
                $column->getType(),
 | 
			
		||||
                $column->isPrimaryKey()?"Y":"-",
 | 
			
		||||
                $column->isNullable()?"Y":"-",
 | 
			
		||||
                $column->getDefaultValue(),
 | 
			
		||||
            ]);
 | 
			
		||||
        }
 | 
			
		||||
        $t->render();
 | 
			
		||||
        $output->writeln("");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -4,6 +4,9 @@ namespace SparkPlug\Com\Noccy\Pdo;
 | 
			
		||||
 | 
			
		||||
use Spark\Resource\ResourceType;
 | 
			
		||||
use PDO;
 | 
			
		||||
use SparkPlug\Com\Noccy\Pdo\Reflection\Reflector\MysqlReflector;
 | 
			
		||||
use SparkPlug\Com\Noccy\Pdo\Reflection\Reflector\ReflectorInterface;
 | 
			
		||||
use SparkPlug\Com\Noccy\Pdo\Reflection\Reflector\SqliteReflector;
 | 
			
		||||
 | 
			
		||||
class PdoResource extends ResourceType
 | 
			
		||||
{
 | 
			
		||||
@@ -47,6 +50,23 @@ class PdoResource extends ResourceType
 | 
			
		||||
        return $this->pdo;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getReflector(): ?ReflectorInterface
 | 
			
		||||
    {
 | 
			
		||||
        $uri = $this->options['uri'];
 | 
			
		||||
        
 | 
			
		||||
        if (!preg_match('|^(.+?):|', $uri, $m)) {
 | 
			
		||||
            fprintf(STDERR, "error: Bad resource URI\n");
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
        switch ($m[1]) {
 | 
			
		||||
            case 'mysql':
 | 
			
		||||
                return new MysqlReflector($this);
 | 
			
		||||
            case 'sqlite':
 | 
			
		||||
                return new SqliteReflector($this);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function info()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->options['uri'];
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,20 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Pdo\Reflection;
 | 
			
		||||
 | 
			
		||||
use SparkPlug\Com\Noccy\Pdo\PdoResource;
 | 
			
		||||
 | 
			
		||||
interface ColumnReflectionInterface
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    public function isPrimaryKey(): bool;
 | 
			
		||||
 | 
			
		||||
    public function isNullable(): bool;
 | 
			
		||||
 | 
			
		||||
    public function getName(): string;
 | 
			
		||||
 | 
			
		||||
    public function getType(): string;
 | 
			
		||||
 | 
			
		||||
    public function getDefaultValue(): mixed;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -0,0 +1,15 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Pdo\Reflection;
 | 
			
		||||
 | 
			
		||||
use SparkPlug\Com\Noccy\Pdo\PdoResource;
 | 
			
		||||
 | 
			
		||||
interface DatabaseReflectionInterface
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    public function getAllTables(): array;
 | 
			
		||||
 | 
			
		||||
    public function getTable(string $name): ?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 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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -0,0 +1,13 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Pdo\Reflection;
 | 
			
		||||
 | 
			
		||||
use SparkPlug\Com\Noccy\Pdo\PdoResource;
 | 
			
		||||
 | 
			
		||||
interface TableReflectionInterface
 | 
			
		||||
{
 | 
			
		||||
    public function getColumn(string $name): ?ColumnReflectionInterface;
 | 
			
		||||
 | 
			
		||||
    public function getAllColumns(): array;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -9,6 +9,7 @@ class PdoPlugin extends SparkPlug {
 | 
			
		||||
    {
 | 
			
		||||
        register_command(new Commands\PdoQueryCommand());
 | 
			
		||||
        register_command(new Commands\PdoExecCommand());
 | 
			
		||||
        register_command(new Commands\PdoInspectCommand());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user