death-notifier/src/main/Database.php

115 lines
3.7 KiB
PHP

<?php
namespace main;
use SQLite3;
class Database
{
private SQLite3 $db;
function __construct($filename, $flags = SQLITE3_OPEN_READWRITE)
{
$this->db = new SQLite3($filename, $flags);
}
function close(): void
{
$this->db->close();
}
function install(): void
{
$this->db->exec("CREATE TABLE users(uuid text primary key not null, email text not null, password text not null);");
$this->db->exec("CREATE TABLE trackings(user_uuid text not null, person_name text not null, is_deceased int not null default 0)");
}
function add_user($uuid, $email, $password): bool
{
$stmt = $this->db->prepare("INSERT INTO users (uuid, email, password) VALUES (:uuid, :email, :password);");
$stmt->bindValue(":uuid", $uuid);
$stmt->bindValue(":email", $email);
$stmt->bindValue(":password", password_hash($password, PASSWORD_DEFAULT));
return $stmt->execute() !== false;
}
function delete_user($uuid): bool
{
$stmt = $this->db->prepare("DELETE FROM users WHERE uuid=:uuid;");
$stmt->bindValue(":uuid", $uuid);
return $stmt->execute() !== false;
}
function get_user_by_uuid($uuid): array|null
{
$stmt = $this->db->prepare("SELECT uuid, password FROM users WHERE uuid=:uuid;");
$stmt->bindValue(":uuid", $uuid);
$result = $stmt->execute()->fetchArray(SQLITE3_ASSOC);
return $result === false ? null : $result;
}
function get_user_by_email($email): array|null
{
$stmt = $this->db->prepare("SELECT uuid, password FROM users WHERE email=:email;");
$stmt->bindValue(":email", $email);
$result = $stmt->execute()->fetchArray(SQLITE3_ASSOC);
return $result === false ? null : $result;
}
function set_user_email($uuid, $email): bool
{
$stmt = $this->db->prepare("UPDATE users SET email=:email WHERE uuid=:uuid;");
$stmt->bindValue(":uuid", $uuid);
$stmt->bindValue(":email", $email);
return $stmt->execute() !== false;
}
function set_user_password($uuid, $password): bool
{
$stmt = $this->db->prepare("UPDATE users SET password=:password WHERE uuid=:uuid;");
$stmt->bindValue(":uuid", $uuid);
$stmt->bindValue(":password", $password);
return $stmt->execute() !== false;
}
function add_tracking($user_uuid, $person_name): bool
{
$stmt = $this->db->prepare("INSERT INTO trackings (user_uuid, person_name) VALUES (:user_uuid, :person_name);");
$stmt->bindValue(":user_uuid", $user_uuid);
$stmt->bindValue(":person_name", $person_name);
return $stmt->execute() !== false;
}
function remove_tracking($user_uuid, $person_name): bool
{
$stmt = $this->db->prepare("DELETE FROM trackings WHERE user_uuid=:user_uuid AND person_name=:person_name;");
$stmt->bindValue(":user_uuid", $user_uuid);
$stmt->bindValue(":person_name", $person_name);
return $stmt->execute() !== false;
}
function has_tracking($user_uuid, $person_name): bool
{
$stmt = $this->db->prepare("SELECT 1 FROM trackings WHERE user_uuid=:user_uuid AND person_name=:person_name;");
$stmt->bindValue(":user_uuid", $user_uuid);
$stmt->bindValue(":person_name", $person_name);
return $stmt->execute()->fetchArray(SQLITE3_ASSOC) !== false;
}
function list_trackings($user_uuid): array
{
$stmt = $this->db->prepare("SELECT * FROM trackings WHERE user_uuid=:user_uuid;");
$stmt->bindValue(":user_uuid", $user_uuid);
$results = $stmt->execute();
$trackings = [];
while ($row = $results->fetchArray(SQLITE3_ASSOC))
$trackings[] = $row;
return $trackings;
}
}