php-guestbook/persistence.php

119 lines
2.8 KiB
PHP
Raw Normal View History

2021-12-10 20:22:06 +00:00
<?php
2021-12-10 22:53:41 +00:00
function createDB($db_name = 'guestbook.db'): SQLite3 {
// Can't create table if it already exists
$exists = file_exists($db_name);
$db = new SQLite3($db_name);
if(!$exists) {
$db->exec('CREATE TABLE user
(id_user INTEGER PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
password TEXT)'
);
$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))'
);
}
2021-12-10 22:04:55 +00:00
return $db;
2021-12-10 20:22:06 +00:00
}
2021-12-10 22:53:41 +00:00
function getMessages(SQLite3 $db, array $filter = [], int $limit = 25) {
2021-12-10 22:04:55 +00:00
// Apply filters
$append_string = '';
$verb = 'WHERE';
2021-12-10 22:53:41 +00:00
foreach($filter as $key => $value) {
2021-12-10 22:04:55 +00:00
$to_append = match($key) {
2021-12-10 22:53:41 +00:00
'id_user' => "$verb $key = :$key",
2021-12-10 22:04:55 +00:00
// TODO
};
2021-12-10 22:53:41 +00:00
if($to_append) {
2021-12-10 22:04:55 +00:00
$append_string .= " $to_append";
$verb = 'AND';
}
}
2021-12-10 22:53:41 +00:00
// Prepare statement
2021-12-10 22:04:55 +00:00
$query = 'SELECT * FROM message' . $append_string;
2021-12-10 23:53:23 +00:00
//var_dump($query);
2021-12-10 22:04:55 +00:00
$statement = $db->prepare($query);
2021-12-10 22:53:41 +00:00
foreach($filter as $key => $value) {
$statement->bindParam($key, $value);
2021-12-10 22:04:55 +00:00
}
2021-12-10 23:53:23 +00:00
$result = $statement->execute();
$notnull = true;
$ret = [];
while($notnull) {
$arr = $result->fetchArray(SQLITE3_ASSOC);
//var_dump($arr);
if($arr !== false) {
$ret[] = $arr;
}
else $notnull = false;
}
return $ret;
}
2021-12-22 21:41:48 +00:00
function storeMessage(SQLite3 $db, string $content, ?int $user_id = null) : bool {
2021-12-21 19:28:43 +00:00
$query = 'INSERT INTO message(id_user, content, date)
VALUES (:id_user, :content, :date)';
2021-12-10 23:53:23 +00:00
$date = (new DateTime('now'))->getTimestamp();
$statement = $db->prepare($query);
$statement->bindParam(':id_user', $user_id);
$statement->bindParam(':content', $content);
$statement->bindParam(':date', $date);
$statement->execute();
2021-12-22 21:41:48 +00:00
return ($db->changes() > 0);
}
function storeUser(SQLite3 $db, string $username, string $password) : bool {
$query = 'INSERT INTO user(username, password)
VALUES (:username, :password)';
$statement = $db->prepare($query);
$statement->bindParam(':username', $username);
$statement->bindParam(':password', $password);
$statement->execute();
return ($db->changes() > 0);
2021-12-10 22:04:55 +00:00
}
2021-12-21 19:28:43 +00:00
function deleteMessage(SQLite3 $db, int $message_id) : int {
$query = 'DELETE FROM message
WHERE id_msg = :id_msg';
$statement = $db->prepare($query);
$statement->bindParam(':id_msg', $message_id);
$result = $statement->execute();
2021-12-22 21:41:48 +00:00
if($result === false) {
2021-12-21 19:28:43 +00:00
return -1;
}
// Number of changed rows
return $db->changes();
}
2021-12-22 21:41:48 +00:00
function userFromMessage(SQLite3 $db, int $message_id) {
$query = 'SELECT user.id_user, user.username
FROM user, message
WHERE message.id_user = user.id_user
AND message.id_msg = :id_msg';
$statement = $db->prepare($query);
$statement->bindParam(':id_msg', $message_id);
$result = $statement->execute();
$array = $result->fetchArray();
if(!$array) return null;
return ['id' => $array['id_user'], 'username' => $array['username']];
}