Implement listing of collections

This commit is contained in:
2024-09-27 18:21:27 +02:00
parent f05aa8a1ea
commit 6d873bcbcd
3 changed files with 40 additions and 3 deletions

View File

@@ -1,6 +1,8 @@
#!/usr/bin/env php
<?php
use PhpParser\Node\Expr\List_;
require_once __DIR__."/../vendor/autoload.php";
$opts = (object)[
@@ -59,6 +61,7 @@ function print_help() {
printf(" -u,--until DATETIME Set the validity end timestamp\n");
printf(" -j,--json Assume json encoded values with set\n");
printf("\nActions:\n");
printf(" list\n List collections\n");
printf(" set {collection} {key}={value}*\n Set the keys to value, using the --from and --until values. Multiple\n key-value pairs can be specified. Use --json to pass values as json.\n");
printf(" get {collection} [{when}]\n Fetch the current values, or the values at {when}.\n");
printf(" show {collection}\n Show keys and all values, allowing to delete values.\n");
@@ -90,6 +93,18 @@ function http_post(string $url, array $headers, ?string $body = null) {
return $http->post("http://{$opts->server}/{$url}", $headers, $body??'');
}
function action_list(object $opts) {
http_get("", [])->then(
function ($response) {
echo join("\n", json_decode($response->getBody()->getContents(),true))."\n";
},
function ($error) {
echo $error->getMessage()."\n";
}
);
}
function action_get(object $opts) {
$collection = array_shift($opts->args);
if (!$collection) {
@@ -179,9 +194,13 @@ function action_set(object $opts) {
}
if ($opts->from || $opts->until) {
$vfrom = $opts->from ? [ 'from' => $opts->from->format('Y-m-d H:i:s P') ] : null;
$vuntil = $opts->until ? [ 'until' => $opts->until->format('Y-m-d H:i:s P') ] : null;
$op['validity'] = [ ...$vfrom, ...$vuntil ];
$op['validity'] = [];
if ($opts->from) {
$op['validity']['from'] = $opts->from->format('Y-m-d H:i:s P');
}
if ($opts->until) {
$op['validity']['until'] = $opts->until->format('Y-m-d H:i:s P');
}
}
$body[$key] = $op;
}
@@ -199,6 +218,10 @@ $action = array_shift($opts->args);
switch ($action) {
case 'list':
action_list($opts);
break;
case 'get':
action_get($opts);
break;

View File

@@ -72,6 +72,12 @@ class Daemon
$collectionName = array_shift($paths);
$collectionOp = array_shift($paths);
if ($collectionName == null) {
$collections = $this->db->getCollectionNames();
return Response::json($collections);
}
switch ($collectionOp) {
case null:

View File

@@ -72,6 +72,14 @@ class Database
return new Collection($this->pdo, $id);
}
public function getCollectionNames(): array
{
$query = $this->pdo->prepare("SELECT * FROM collections ORDER BY name ASC");
$query->execute();
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
return array_column($rows, "name");
}
public function createCollection(string $name, array $meta = []): Collection
{