aboutsummaryrefslogtreecommitdiff
path: root/models
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2025-01-19 08:30:53 +0200
committerSyndamia <kamen@syndamia.com>2025-01-19 08:30:53 +0200
commitfbf2b935d5dbce778021da4e938aec183fc0e2d2 (patch)
tree91c15fcbf1a0793cdfa19f075dcdc086ea3ae2a0 /models
parent5c0dad31512870257178e9950ae688e57206dd22 (diff)
downloadnowayforward_human-fbf2b935d5dbce778021da4e938aec183fc0e2d2.tar
nowayforward_human-fbf2b935d5dbce778021da4e938aec183fc0e2d2.tar.gz
nowayforward_human-fbf2b935d5dbce778021da4e938aec183fc0e2d2.zip
feat(database): Created a generic Table class
Diffstat (limited to 'models')
-rw-r--r--models/database.php67
1 files changed, 46 insertions, 21 deletions
diff --git a/models/database.php b/models/database.php
index c8123ad..cd4bf77 100644
--- a/models/database.php
+++ b/models/database.php
@@ -2,30 +2,55 @@
namespace Database;
use PDO;
-class User {
+class User extends Table {
public $UID;
public $Username;
public $Password;
public $Role;
+ static function create(string $Username, string $Password, string $Role) : int {
+ return Table::_create(
+ "Users",
+ "(Username, Password, Role)",
+ "(\"$Username\", \"$Password\", \"$Role\")",
+ );
+ }
+
function fromDB(string $username) : User {
- $conn = connect();
- $query = $conn->query("SELECT * FROM Users WHERE Username = \"$username\"");
+ return Table::_fromDB(
+ "SELECT * FROM Users WHERE Username = \"$username\"",
+ "Database\User"
+ );
+ }
+
+ static function get_all() : array {
+ return Table::_get_all("Database\User");
+ }
+}
+
+abstract class Table {
+ // Cannot be created, because FETCH_CLASS will assign to all attributes
+ // and then call the constructor
+ final private function __construct() {}
+
+ static protected function _fromDB(string $query_str, string $class) : object {
+ $conn = Table::connect();
+ $query = $conn->query($query_str);
// TODO: research if this is enough to close the connection, $query may store a reference
$conn = null;
if ($query->rowCount() == 0) {
- throw new Exception("User $username doesn't exist!");
+ throw new Exception("Value for $class doesn't exist!");
}
- assert($query->rowCount() == 1, "Users must have unique usernames!");
+ assert($query->rowCount() == 1, "Vaue for $class must be uniqely specified!");
- $query->setFetchMode(PDO::FETCH_CLASS, "User");
+ $query->setFetchMode(PDO::FETCH_CLASS, $class);
return $query->fetch();
}
- static function create(string $Username, string $Password, string $Role) : int {
- $conn = connect();
- $query = $conn->query("INSERT INTO Users (Username, Password, Role) VALUES (\"$Username\", \"$Password\", \"$Role\")");
+ static protected function _create(string $table, string $columns, string $value) : int {
+ $conn = Table::connect();
+ $query = $conn->query("INSERT INTO $table $columns VALUES $value");
// NOTE: If we ever insert more than one values, lastInsertId will returne the first id
$id = $conn->lastInsertId();
@@ -33,22 +58,22 @@ class User {
return $id;
}
- static function get_all() : array {
- $conn = connect();
+ static protected function _get_all(string $class) : array {
+ $conn = Table::connect();
$query = $conn->query("SELECT * FROM Users");
$conn = null;
- $query->setFetchMode(PDO::FETCH_CLASS, "Database\User");
+ $query->setFetchMode(PDO::FETCH_CLASS, $class);
return $query->fetchAll();
}
-}
-function connect() : PDO {
- $conn = new PDO(
- "mysql:unix_socket=" . getenv('MYSQL_UNIX_SOCKET') . ";dbname=nwfh",
- getenv('USER'),
- "");
- // set the PDO error mode to exception
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- return $conn;
+ static protected function connect() : PDO {
+ $conn = new PDO(
+ "mysql:unix_socket=" . getenv('MYSQL_UNIX_SOCKET') . ";dbname=nwfh",
+ getenv('USER'),
+ "");
+ // set the PDO error mode to exception
+ $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ return $conn;
+ }
}