<?php # -*- coding: utf-8 mode: php -*- vim:sw=2:sts=2:et:ai:si:sta:fenc=utf-8
# fichier utilisé pour les tests. on peut y écrire du code pour vérifier le
# fonctionnement de certaines classes et méthodes
require(__DIR__.'/../vendor/autoload.php');

use nur\cli\Application;
use nur\m\oracle\OracleConn;
use nur\m\pdo\mysql\MysqlConn;

Application::run(new class extends Application {
  const ARGS = [
    "merge" => Application::ARGS,
    ["--debug", "action" => "->setApplicationDebug",
      "help" => "Activer le mode DEBUG: comme VERBOSE mais les traceback sont affichés à partir des messages normaux",
    ],
    ["-0", "--init"],
    ["-t", "--throw"],
  ];

  private $init, $throw;

  private $args;

  function main() {
    $this->testOracle();
    #$this->testMysql();
    #$this->testPgsql();
  }

  function testOracle(): void {
    $conn = new OracleConn("//vs-apotest-bdd.univ.run:1521/apotest", "webread", "run974");

    $q = $conn->query("select * from individu where cod_etu = :cod_etu", ["cod_etu" => "99002956"]);
    foreach ($q->execute() as $row) {
      Txx($row);
    }
  }

  function testMysql(): void {
    #$conn = new MysqlConn("mysql:host=localhost;dbname=test;charset=utf8", "jclain");
    $conn = new MysqlConn("mysql:host=192.168.1.20;dbname=test;charset=utf8", "jclain", 'klokis$');

    if ($this->init) {
      $conn->execDml("drop table if exists t");
      $conn->execDml("create table t(id integer not null primary key auto_increment, a varchar(30), b integer, d date, t timestamp default current_timestamp)");
      Txx($conn->execDml("insert into t(a, b) values ('first', 1)"));
      Txx($conn->execDml("insert into t(a, b, d) values ('second', 2, '2020-10-20')"));
      Txx($conn->execDml("insert into t(a, b, d) values ('second', 2, '2020-05-15')"));
      if ($this->throw) {
        Txx($conn->execDml("insert into t(id, a, b) values (2, 'conflict', 3)"));
      }
      $conn->commit();
    }

    $do = 2;
    if ($do == 0) {
      $row = $conn->fetchRow("select a, b from t where b = 1");
      Txx("b==1", $row);

      $row = $conn->fetchRow("select a, b from t where b = 100");
      Txx("b==100", $row);

      $rows = $conn->fetchRows("select a, b from t");
      Txx("all", $rows);

      $rows = $conn->fetchRows("select a, b from t where b > 100");
      Txx("all b>100", $rows);
    }

    if ($do == 1) {
      $rows = $conn->fetchRows("select * from t");
      foreach ($rows as $row) {
        Txx("row", $row);
      }
    }

    if ($do == 2) {
      $rows = $conn->query("select * from t")->execute();
      foreach ($rows as $row) {
        Txx("row", $row);
      }
    }

    if ($do == 4) {
      $q = $conn->query("select * from t where d > :date");
      Txx("after 01/01", $q->execute(false, ["date" => "01/01/2020"])->all());
      Txx("after 15/06", $q->execute(false, ["date" => "15/06/2020"])->all());
    }
  }

  function testPgsql(): void {
  }
});