diff --git a/nur_bin/sqlite-storage.php b/nur_bin/sqlite-storage.php index 7b6d24f..3add3a4 100755 --- a/nur_bin/sqlite-storage.php +++ b/nur_bin/sqlite-storage.php @@ -63,15 +63,21 @@ Application::run(new class extends Application { if ($dbfile === null) self::die("Vous devez spécifier la base de données"); if (!file_exists($dbfile)) self::die("$dbfile: fichier introuvable"); $storage = new SqliteStorage($dbfile); + $db = $storage->db(); + + $haveChannels = $storage->tableExists("_channels"); $name = $this->name; $channelClass = $this->channelClass; $tableName = $this->tableName; if ($name !== null) { - $row = $storage->db()->one([ - "select from _channels", - "where" => ["name" => $name], - ]); + $row = null; + if ($haveChannels) { + $row = $db->one([ + "select from _channels", + "where" => ["name" => $name], + ]); + } if ($row === null) self::die("$name: nom de canal de données introuvable"); if ($row["class"] !== "class@anonymous") $channelClass = $row["class"]; else $tableName = $row["table_name"]; @@ -87,18 +93,30 @@ Application::run(new class extends Application { } }; } else { - self::die("Vous devez spécifier le canal de données"); + $found = false; + if ($haveChannels) { + $rows = $db->all([ + "select from _channels", + ]); + foreach ($rows as $row) { + msg::print($row["name"]); + $found = true; + } + } + if (!$found) self::die("Vous devez spécifier le canal de données"); } $capacitor = new Capacitor($storage, $channel); switch ($this->action) { case self::ACTION_QUERY: $args = $this->args; + var_export($args); #XXX if (!$args) { # lister les id $out = new Stream(STDOUT); $primaryKeys = $storage->getPrimaryKeys($channel); - $rows = $storage->db()->all([ + var_export($primaryKeys); #XXX + $rows = $db->all([ "select", "cols" => $primaryKeys, "from" => $channel->getTableName(), diff --git a/src/app/cli/include-launcher.php b/src/app/cli/include-launcher.php index a42e227..5a33e7e 100644 --- a/src/app/cli/include-launcher.php +++ b/src/app/cli/include-launcher.php @@ -11,9 +11,9 @@ if ($argc <= 1) die("invalid arguments"); app::init(NULIB_APP_app_params); $app = $argv[1]; +array_splice($argv, 0, 1); $argc--; if (class_exists($app)) { # la configuration est celle actuellement chargée - array_splice($argv, 1, 1); $argc--; $app::run(); } elseif (is_executable($app)) { # la configuration est passée par une variable d'environnement @@ -21,7 +21,6 @@ if (class_exists($app)) { pcntl_exec($app, array_slice($argv, 1)); } else { # la configuration est celle actuellement chargée - array_splice($argv, 0, 1); $argc--; $name = preg_replace('/\.php$/', "", path::basename($app)); app::init([ "name" => $name, diff --git a/src/db/sqlite/SqliteStorage.php b/src/db/sqlite/SqliteStorage.php index b6f9ac0..03de35e 100644 --- a/src/db/sqlite/SqliteStorage.php +++ b/src/db/sqlite/SqliteStorage.php @@ -28,13 +28,25 @@ class SqliteStorage extends CapacitorStorage { return self::format_sql($channel, $query->getSql()); } - protected function _afterCreate(CapacitorChannel $channel): void { - $db = $this->db; + function tableExists(string $tableName): bool { $name = $this->db->get([ "select name from sqlite_schema", - "where" => ["name" => "_channels"], + "where" => ["name" => $tableName], ]); - if ($name === null) { + return $name !== null; + } + + function channelExists(string $name): bool { + $name = $this->db->get([ + "select name from _channels", + "where" => ["name" => $name], + ]); + return $name !== null; + } + + protected function _afterCreate(CapacitorChannel $channel): void { + $db = $this->db; + if (!$this->tableExists("_channels")) { # ne pas créer si la table existe déjà, pour éviter d'avoir besoin d'un # verrou en écriture $db->exec([ @@ -47,11 +59,7 @@ class SqliteStorage extends CapacitorStorage { ], ]); } - $name = $this->db->get([ - "select name from _channels", - "where" => ["name" => $channel->getName()], - ]); - if ($name === null) { + if (!$this->channelExists($channel->getName())) { # ne pas insérer si la ligne existe déjà, pour éviter d'avoir besoin d'un # verrou en écriture $db->exec([ @@ -78,13 +86,7 @@ class SqliteStorage extends CapacitorStorage { } function _exists(CapacitorChannel $channel): bool { - $tableName = $this->db->get([ - "select name from sqlite_schema", - "where" => [ - "name" => $channel->getTableName(), - ], - ]); - return $tableName !== null; + return $this->tableExists($channel->getTableName()); } function close(): void {