From 262f1f486c0fcf3e766156b1f36cd094079233b3 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Thu, 6 Jun 2024 09:42:16 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- nur_bin/mysql-storage.php | 90 +++++++++++++++++++++++++++++++++++++ nur_bin/sqlite-storage.php | 86 +++++++++++++++++++++++++++++++++++ src/db/CapacitorChannel.php | 5 ++- src/db/pdo/Pdo.php | 8 +++- 4 files changed, 187 insertions(+), 2 deletions(-) create mode 100755 nur_bin/mysql-storage.php create mode 100755 nur_bin/sqlite-storage.php diff --git a/nur_bin/mysql-storage.php b/nur_bin/mysql-storage.php new file mode 100755 index 0000000..d794a12 --- /dev/null +++ b/nur_bin/mysql-storage.php @@ -0,0 +1,90 @@ +#!/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", + ], + ["-c", "--channel", "args" => 1, + "help" => "nom de la table porteuse du canal de données", + ], + ]; + + protected $dbconn; + + protected $channel; + + protected $args; + + 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->channel === null) self::die("Vous devez spécifier le canal de données"); + + $storage = new MysqlStorage($dbconn); + $channel = new class($this->channel) extends CapacitorChannel { + public function __construct(?string $name=null) { + parent::__construct($name); + $this->tableName = $name; + } + }; + $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); + }); + } + } +}); diff --git a/nur_bin/sqlite-storage.php b/nur_bin/sqlite-storage.php new file mode 100755 index 0000000..18b05ba --- /dev/null +++ b/nur_bin/sqlite-storage.php @@ -0,0 +1,86 @@ +#!/usr/bin/php + parent::ARGS, + "purpose" => "gestion d'un capacitor sqlite", + "usage" => "-f DBFILE -c CHANNEL key=value...", + ["-f", "--dbfile", "args" => 1, + "help" => "chemin vers la base de données", + ], + ["-c", "--channel", "args" => 1, + "help" => "nom de la table porteuse du canal de données", + ], + ]; + + protected $dbfile; + + protected $channel; + + protected $args; + + 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() { + $dbfile = $this->dbfile; + if ($dbfile === null) self::die("Vous devez spécifier la base de données"); + if (!file_exists($dbfile)) self::die("$dbfile: fichier introuvable"); + + if ($this->channel === null) self::die("Vous devez spécifier le canal de données"); + + $storage = new SqliteStorage($dbfile); + $channel = new class($this->channel) extends CapacitorChannel { + public function __construct(?string $name=null) { + parent::__construct($name); + $this->tableName = $name; + } + }; + $capacitor = new Capacitor($storage, $channel); + + $args = $this->args; + if (!$args) { + # lister les id + $rows = $storage->sqlite()->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); + }); + } + } +}); diff --git a/src/db/CapacitorChannel.php b/src/db/CapacitorChannel.php index 37341b5..f113858 100644 --- a/src/db/CapacitorChannel.php +++ b/src/db/CapacitorChannel.php @@ -24,6 +24,7 @@ class CapacitorChannel { function __construct(?string $name=null, ?int $eachCommitThreshold=null) { $this->name = self::verifix_name($name ?? static::NAME); + $this->tableName = static::TABLE_NAME ?? ($this->name."_channel"); $this->eachCommitThreshold = $eachCommitThreshold ?? static::EACH_COMMIT_THRESHOLD; $this->created = false; $columnDefinitions = cl::withn(static::COLUMN_DEFINITIONS); @@ -54,8 +55,10 @@ class CapacitorChannel { return $this->name; } + protected $tableName; + function getTableName(): string { - return static::TABLE_NAME ?? ($this->name."_channel"); + return $this->tableName; } /** diff --git a/src/db/pdo/Pdo.php b/src/db/pdo/Pdo.php index 8b7a2f1..a285fd5 100644 --- a/src/db/pdo/Pdo.php +++ b/src/db/pdo/Pdo.php @@ -57,7 +57,13 @@ class Pdo { function __construct($dbconn=null, ?array $params=null) { if ($dbconn !== null) { - if (!is_array($dbconn)) $dbconn = ["name" => $dbconn]; + if (!is_array($dbconn)) { + $dbconn = ["name" => $dbconn]; + #XXX à terme, il faudra interroger config + #$tmp = config::db($dbconn); + #if ($tmp !== null) $dbconn = $tmp; + #else $dbconn = ["name" => $dbconn]; + } $params["dbconn"] = $dbconn; } # dbconn