nur-sery/nur_bin/mysql-storage.php

128 lines
3.8 KiB
PHP
Raw Normal View History

2024-06-06 09:42:16 +04:00
#!/usr/bin/php
<?php
require $_composer_autoload_path?? __DIR__.'/../vendor/autoload.php';
use nur\b\io\CacheFile;
use nur\cli\Application;
use nur\config;
use nur\msg;
use nur\path;
use nur\sery\db\Capacitor;
use nur\sery\db\CapacitorChannel;
use nur\sery\db\mysql\MysqlStorage;
use nur\sery\db\sqlite\SqliteStorage;
2024-06-07 11:57:15 +04:00
use nur\sery\file\Stream;
2024-06-06 09:42:16 +04:00
use nur\yaml;
Application::run(new class extends Application {
2024-06-14 09:32:59 +04:00
const ACTION_QUERY = 0, ACTION_SQL = 1;
2024-06-06 09:42:16 +04:00
const ARGS = [
"merge" => parent::ARGS,
"purpose" => "gestion d'un capacitor mysql",
2024-06-14 09:32:59 +04:00
"usage" => [
"-d DBCONN -c CHANNEL [--query] key=value...",
"-d DBCONN -c CHANNEL --sql-create",
],
2024-06-06 09:42:16 +04:00
["-d", "--dbconn", "args" => 1,
"help" => "nom de la connexion à la base de données",
],
2024-06-06 12:19:06 +04:00
["-t", "--table-name", "args" => 1,
2024-06-06 09:42:16 +04:00
"help" => "nom de la table porteuse du canal de données",
],
2024-06-06 12:19:06 +04:00
["-c", "--channel-class", "args" => 1,
"help" => "nom de la classe dérivée de CapacitorChannel",
],
2024-06-14 09:32:59 +04:00
["--query", "name" => "action", "value" => self::ACTION_QUERY,
"help" => "lister les lignes correspondant aux valeurs spécifiées. c'est l'action par défaut",
],
["-s", "--sql-create", "name" => "action", "value" => self::ACTION_SQL,
"help" => "afficher la requête pour créer la table",
],
2024-06-06 09:42:16 +04:00
];
2024-06-06 12:19:06 +04:00
protected ?string $dbconn = null;
protected ?string $tableName = null;
2024-06-06 09:42:16 +04:00
2024-06-06 12:19:06 +04:00
protected ?string $channelClass = null;
2024-06-06 09:42:16 +04:00
2024-06-14 09:32:59 +04:00
protected int $action = self::ACTION_QUERY;
2024-06-06 12:19:06 +04:00
protected ?array $args = null;
2024-06-06 09:42:16 +04:00
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;
2024-06-06 12:19:06 +04:00
if ($this->channelClass !== null) {
$channelClass = str_replace("/", "\\", $this->channelClass);
$channel = new $channelClass;
} elseif ($this->tableName !== null) {
$channel = new class($this->tableName) extends CapacitorChannel {
2024-06-18 12:41:10 +04:00
function __construct(?string $name=null) {
2024-06-06 12:19:06 +04:00
parent::__construct($name);
$this->tableName = $name;
}
};
} else {
self::die("Vous devez spécifier le canal de données");
}
2024-06-06 09:42:16 +04:00
$storage = new MysqlStorage($dbconn);
$capacitor = new Capacitor($storage, $channel);
2024-06-14 09:32:59 +04:00
switch ($this->action) {
case self::ACTION_QUERY:
$args = $this->args;
if (!$args) {
# lister les id
$out = new Stream(STDOUT);
$primaryKeys = $storage->getPrimaryKeys($channel);
$rows = $storage->db()->all([
"select",
"cols" => $primaryKeys,
"from" => $channel->getTableName(),
]);
$out->fputcsv($primaryKeys);
foreach ($rows as $row) {
$rowIds = $storage->getRowIds($channel, $row);
$out->fputcsv($rowIds);
}
2024-06-06 09:42:16 +04:00
} else {
2024-06-14 09:32:59 +04:00
# 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"];
}
2024-06-06 09:42:16 +04:00
}
}
2024-06-14 09:32:59 +04:00
$first = true;
$capacitor->each($filter, function ($item, $row) use (&$first) {
if ($first) $first = false;
else echo "---\n";
yaml::dump($row);
});
2024-06-06 09:42:16 +04:00
}
2024-06-14 09:32:59 +04:00
break;
case self::ACTION_SQL:
echo $capacitor->getCreateSql()."\n";
break;
2024-06-06 09:42:16 +04:00
}
}
});