Get Messages WIP

This commit is contained in:
Dusk 2021-12-10 23:04:55 +01:00
parent 63b19062ea
commit 6538ccd525
1 changed files with 40 additions and 7 deletions

View File

@ -2,14 +2,47 @@
createDB(); createDB();
function createDB() function createDB(): SQLite3 {
{
$db = new SQLite3('guestbook.db'); $db = new SQLite3('guestbook.db');
$db->exec("CREATE TABLE user $db->exec('CREATE TABLE user
(id_user INTEGER PRIMARY KEY, username TEXT NOT NULL UNIQUE, password TEXT)"); (id_user INTEGER PRIMARY KEY,
$db->exec("CREATE TABLE message username TEXT NOT NULL UNIQUE,
(id_msg INTEGER PRIMARY KEY, id_user INT, content TEXT NOT NULL, password TEXT)'
FOREIGN KEY (id_user) REFERENCES user (id_user))"); );
$db->exec('CREATE TABLE message
(id_msg INTEGER PRIMARY KEY,
id_user INT,
content TEXT NOT NULL,
date INT,
FOREIGN KEY (id_user) REFERENCES user (id_user))'
);
return $db;
} }
// getMessages(['id' => 34, 'since' => 383489289424])
function getMessages(SQLite3 $db, int $limit = 25, array $filter = []) {
// Apply filters
$append_string = '';
$verb = 'WHERE';
foreach ($filter as $key => $value) {
$to_append = match($key) {
'id_msg' => "$verb $key IN (:$key)",
// TODO
};
if ($to_append) {
$append_string .= " $to_append";
$verb = 'AND';
}
}
$query = 'SELECT * FROM message' . $append_string;
$statement = $db->prepare($query);
foreach ($filter as $key => $value) {
$statement->bindValue($key, $value);
}
return $statement->execute();
}