<?php
namespace nulib\cli;

use nulib\A;
use nulib\db\pgsql\PgsqlStorage;
use nur\config;

class PgsqlStorageApp extends AbstractStorageApp {
  const ARGS = [
    "merge" => parent::ARGS,
    "purpose" => "gestion d'un capacitor pgsql",
    "usage" => [
      "DBCONN [CHANNEL_NAME | -t TABLE | -c CHANNEL_CLASS] [--query] key=value...",
      "DBCONN [CHANNEL_NAME | -t TABLE | -c CHANNEL_CLASS] --sql-create",
    ],
    ["-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",
    ],
    ["-z", "--reset", "name" => "action", "value" => self::ACTION_RESET,
      "help" => "réinitialiser le canal",
    ],
    ["-n", "--no-recreate", "name" => "recreate", "value" => false,
      "help" => "ne pas recréer la table correspondant au canal"
    ],
    ["--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",
    ],
  ];

  function main() {
    $dbconn = A::shift($this->args);
    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");
    $storage = new PgsqlStorage($tmp);

    $this->storageCtl($storage);
  }
}
