From b93f8fa553305f47b175179fcbf83b997dee52eb Mon Sep 17 00:00:00 2001 From: bohwaz Date: Fri, 30 Sep 2022 15:17:02 +0200 Subject: [PATCH] Refactor properties handling --- lib/KaraDAV/Properties.php | 30 ++++++++++++++---------------- schema.sql | 7 +++---- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/lib/KaraDAV/Properties.php b/lib/KaraDAV/Properties.php index 0f9b2bc..468639e 100644 --- a/lib/KaraDAV/Properties.php +++ b/lib/KaraDAV/Properties.php @@ -6,8 +6,7 @@ class Properties { protected string $user; protected string $uri; - protected array $ns = []; - protected array $xml = []; + protected array $properties = []; protected bool $loaded = false; @@ -20,38 +19,37 @@ class Properties { if (!$this->loaded) { $this->loaded = true; - $list = DB::getInstance()->iterate('SELECT ns, xml FROM properties WHERE user = ? AND uri = ?;', $this->user, $this->uri); + $list = DB::getInstance()->iterate('SELECT name, attributes, xml FROM properties WHERE user = ? AND uri = ?;', $this->user, $this->uri); foreach ($list as $row) { - $this->ns = array_merge($this->ns, json_decode($row->ns, true)); - $this->xml[] = $row->xml; + $this->properties[$row->name] = [ + 'attributes' => $row->attributes ? json_decode($row->attributes) : null, + 'xml' => $row->xml, + ]; } } } - public function xml(): string + public function all(): array { $this->load(); - return implode("\n", $this->xml); + return $this->properties; } - public function ns(): array + public function get(string $name): ?array { $this->load(); - - return $this->ns; + return $this->properties[$name] ?? null; } - public function set(string $ns_url, string $name, array $ns, string $xml) + public function set(string $name, ?array $attributes, ?string $xml) { - $ns = json_encode($ns); - - DB::getInstance()->run('REPLACE INTO properties (user, uri, ns_url, name, ns, xml) VALUES (?, ?, ?, ?, ?, ?);', $this->user, $this->uri, $ns_url, $name, $ns, $xml); + DB::getInstance()->run('REPLACE INTO properties (user, uri, name, attributes, xml) VALUES (?, ?, ?, ?, ?);', $this->user, $this->uri, $name, $attributes ? json_encode($attributes) : null, $xml); } - public function remove(string $ns_url, string $name) + public function remove(string $name) { - DB::getInstance()->run('DELETE FROM properties WHERE user = ? AND uri = ? AND ns_url = ? AND name = ?;', $this->user, $this->uri, $ns_url, $name); + DB::getInstance()->run('DELETE FROM properties WHERE user = ? AND uri = ? AND name = ?;', $this->user, $this->uri, $name); } public function clear() diff --git a/schema.sql b/schema.sql index 6fe7ac6..f54c02f 100644 --- a/schema.sql +++ b/schema.sql @@ -33,10 +33,9 @@ CREATE UNIQUE INDEX app_sessions_token ON app_sessions (token); CREATE TABLE properties ( user TEXT NOT NULL REFERENCES users(login) ON DELETE CASCADE, uri TEXT NOT NULL, - ns_url TEXT NOT NULL, name TEXT NOT NULL, - ns TEXT NOT NULL, - xml TEXT NOT NULL + attributes TEXT NULL, + xml TEXT NULL ); -CREATE UNIQUE INDEX properties_unique ON properties (user, uri, ns_url, name); +CREATE UNIQUE INDEX properties_unique ON properties (user, uri, name);