Refactor properties handling

This commit is contained in:
bohwaz 2022-09-30 15:17:02 +02:00
parent b4819c5c88
commit b93f8fa553
2 changed files with 17 additions and 20 deletions

View file

@ -6,8 +6,7 @@ class Properties
{ {
protected string $user; protected string $user;
protected string $uri; protected string $uri;
protected array $ns = []; protected array $properties = [];
protected array $xml = [];
protected bool $loaded = false; protected bool $loaded = false;
@ -20,38 +19,37 @@ class Properties
{ {
if (!$this->loaded) { if (!$this->loaded) {
$this->loaded = true; $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) { foreach ($list as $row) {
$this->ns = array_merge($this->ns, json_decode($row->ns, true)); $this->properties[$row->name] = [
$this->xml[] = $row->xml; 'attributes' => $row->attributes ? json_decode($row->attributes) : null,
'xml' => $row->xml,
];
} }
} }
} }
public function xml(): string public function all(): array
{ {
$this->load(); $this->load();
return implode("\n", $this->xml); return $this->properties;
} }
public function ns(): array public function get(string $name): ?array
{ {
$this->load(); $this->load();
return $this->properties[$name] ?? null;
return $this->ns;
} }
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, name, attributes, xml) VALUES (?, ?, ?, ?, ?);', $this->user, $this->uri, $name, $attributes ? json_encode($attributes) : null, $xml);
DB::getInstance()->run('REPLACE INTO properties (user, uri, ns_url, name, ns, xml) VALUES (?, ?, ?, ?, ?, ?);', $this->user, $this->uri, $ns_url, $name, $ns, $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() public function clear()

View file

@ -33,10 +33,9 @@ CREATE UNIQUE INDEX app_sessions_token ON app_sessions (token);
CREATE TABLE properties ( CREATE TABLE properties (
user TEXT NOT NULL REFERENCES users(login) ON DELETE CASCADE, user TEXT NOT NULL REFERENCES users(login) ON DELETE CASCADE,
uri TEXT NOT NULL, uri TEXT NOT NULL,
ns_url TEXT NOT NULL,
name TEXT NOT NULL, name TEXT NOT NULL,
ns TEXT NOT NULL, attributes TEXT NULL,
xml TEXT NOT 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);