#!/usr/bin/php parent::ARGS, "purpose" => "gestion d'un capacitor mysql", "usage" => "-d DBCONN -c CHANNEL key=value...", ["-d", "--dbconn", "args" => 1, "help" => "nom de la connexion à la base de données", ], ["-t", "--table-name", "args" => 1, "help" => "nom de la table porteuse du canal de données", ], ["-c", "--channel-class", "args" => 1, "help" => "nom de la classe dérivée de CapacitorChannel", ], ]; protected ?string $dbconn = null; protected ?string $tableName = null; protected ?string $channelClass = null; protected ?array $args = null; protected static function isa_cond(string $arg, ?array &$ms=null): bool { return preg_match('/^(.+?)\s*(=|<>|<|>|<=|>=|(?:is\s+)?null|(?:is\s+)?not\s+null)\s*(.*)$/', $arg, $ms); } function main() { $dbconn = $this->dbconn; if ($dbconn === null) self::die("Vous devez spécifier la base de données"); $tmp = config::db($dbconn); if ($tmp === null) self::die("$dbconn: base de données invalide"); $dbconn = $tmp; if ($this->channelClass !== null) { $channelClass = str_replace("/", "\\", $this->channelClass); $channel = new $channelClass; } elseif ($this->tableName !== null) { $channel = new class($this->tableName) extends CapacitorChannel { public function __construct(?string $name=null) { parent::__construct($name); $this->tableName = $name; } }; } else { self::die("Vous devez spécifier le canal de données"); } $storage = new MysqlStorage($dbconn); $capacitor = new Capacitor($storage, $channel); $args = $this->args; if (!$args) { # lister les id $rows = $storage->mysql()->all([ "select id_", "from" => $channel->getTableName(), ]); foreach ($rows as $row) { echo "$row[id_]\n"; } } else { # afficher les lignes correspondantes if (count($args) == 1 && !self::isa_cond($args[0])) { $filter = $args[0]; } else { $filter = []; $ms = null; foreach ($args as $arg) { if (self::isa_cond($arg, $ms)) { $filter[$ms[1]] = [$ms[2], $ms[3]]; } else { $filter[$arg] = ["not null"]; } } } $first = true; $capacitor->each($filter, function ($item, $row) use (&$first) { if ($first) $first = false; else echo "---\n"; yaml::dump($row); }); } } });