X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fpq-gateway;a=blobdiff_plain;f=tests%2Fsetup.inc;h=fa7af13428ec71522b02d220dd6d81eb3ff0eb8e;hp=798d4a4824ad8eba9213e97e64ab5b593cb5d645;hb=80c4d645e1650304b1dbcae85dbf17a6593896e4;hpb=20d2b6bcce8f1c7a1aaa375b86ffb5be30674956 diff --git a/tests/setup.inc b/tests/setup.inc index 798d4a4..fa7af13 100644 --- a/tests/setup.inc +++ b/tests/setup.inc @@ -1,28 +1,83 @@ getConnection(); + $exec->setConnection(new pq\Connection(PQ_TEST_DSN)); + $exec->execute(new \pq\Query\Writer($sql, $params), function(){}); + $exec->setConnection($conn); +} -spl_autoload_register(function($c) { - if (substr($c,0,3) == "pq\\") return require_once sprintf("%s/../lib/%s.php", __DIR__, strtr($c, "\\", "/")); -}); +class QueryLogger implements \SplObserver +{ + protected $fp; + + function __construct($logfile = null) { + if (!isset($logfile)) { + $logfile = __DIR__."/query.log"; + } + if (!$this->fp = @fopen($logfile, "a")) { + throw new \RuntimeException(error_get_last()["message"]); + } + } + + function __destruct() { + if (is_resource($this->fp)) { + fclose($this->fp); + } + } + + function update(\SplSubject $executor) { + $result = $executor->getResult(); + if (isset($result)) { + fprintf($this->fp, "[%s] R %s\n", + date_create()->format("Y-m-d H:i:s"), + json_encode($result)); + } elseif (($query = $executor->getQuery())) { + fprintf($this->fp, "[%s] Q %s %% %s\n", + date_create()->format("Y-m-d H:i:s"), + preg_replace("/\s+/", " ", $query), + json_encode($query->getParams())); + } + } +}