First working version

This commit is contained in:
Gregory Trolliet 2020-11-16 21:31:59 +01:00
parent 8a6973fcb6
commit baa592b81f
8 changed files with 192 additions and 51 deletions

155
db.php
View file

@ -1,6 +1,14 @@
<?php
class DataBase
{
const DEFAULT_DURATION = '+7 day';
const OPTIONS_DURATION = array(
'day' => '+1 day',
'week' => '+7 day',
'2week' => '+14 day',
'month' => '+1 month'
);
private $db;
public function __construct()
@ -28,21 +36,28 @@ class DataBase
public function buildTables()
{
$stmt = $this->db->prepare('CREATE TABLE IF NOT EXISTS clouds('
. 'id_cloud INTEGER PRIMARY KEY, '
. 'code TEXT NOT NULL UNIQUE, '
. 'create_t TIMESTAMP DEFAULT CURRENT_TIMESTAMP);'
);
$stmt = $this->db->prepare("
CREATE TABLE IF NOT EXISTS clouds(
id_cloud INTEGER PRIMARY KEY,
code TEXT NOT NULL UNIQUE,
size INTEGER NOT NULL DEFAULT 3,
text TEXT,
delete_t TIMESTAMP DEFAULT
(datetime('now', '" . self::DEFAULT_DURATION . "'))
);
");
$stmt->execute();
$stmt = $this->db->prepare('CREATE TABLE IF NOT EXISTS words('
. 'id_word INTEGER PRIMARY KEY, '
. 'word TEXT NOT NULL, '
. 'count INT DEFAULT 1, '
. 'cloud_id INT NOT NULL, '
. 'FOREIGN KEY (cloud_id) '
. 'REFERENCES clouds(id_cloud) ON UPDATE CASCADE '
. 'ON DELETE CASCADE);'
);
$stmt = $this->db->prepare("
CREATE TABLE IF NOT EXISTS words(
id_word INTEGER PRIMARY KEY,
word TEXT NOT NULL,
count INT DEFAULT 1,
cloud_id INT NOT NULL,
FOREIGN KEY (cloud_id)
REFERENCES clouds(id_cloud) ON UPDATE CASCADE
ON DELETE CASCADE
);
");
$stmt->execute();
}
@ -51,7 +66,11 @@ class DataBase
if (empty($word)) {
return false;
}
$stmt = $this->db->prepare('SELECT * FROM clouds WHERE code = :id;');
$stmt = $this->db->prepare("
SELECT *
FROM clouds
WHERE code = :id;
");
$stmt->bindValue(':id', $cloud, PDO::PARAM_STR);
$stmt->execute();
$cloudId = null;
@ -60,23 +79,29 @@ class DataBase
} else {
return false;
}
$stmt = $this->db->prepare('SELECT * FROM words '
. 'WHERE cloud_id = :cid AND word = :w;'
);
$stmt = $this->db->prepare("
SELECT *
FROM words
WHERE cloud_id = :cid
AND word = :w;
");
$stmt->bindValue(':cid', $cloudId, PDO::PARAM_INT);
$stmt->bindValue(':w', $word, PDO::PARAM_STR);
$stmt->execute();
if ($data = $stmt->fetch()) {
$wordId = $data['id_word'];
$stmt = $this->db->prepare(
'UPDATE words SET count = count + 1 WHERE id_word = :id;'
);
$stmt = $this->db->prepare("
UPDATE words
SET count = count + 1
WHERE id_word = :id;
");
$stmt->bindValue(':id', $wordId, PDO::PARAM_INT);
$stmt->execute();
} else {
$stmt = $this->db->prepare(
'INSERT INTO words(word, cloud_id) VALUES(:w, :cid);'
);
$stmt = $this->db->prepare("
INSERT INTO words(word, cloud_id)
VALUES (:w, :cid);
");
$stmt->bindValue(':w', $word, PDO::PARAM_STR);
$stmt->bindValue(':cid', $cloudId, PDO::PARAM_INT);
$stmt->execute();
@ -84,10 +109,13 @@ class DataBase
}
public function getWords(string $id)
{
$stmt = $this->db->prepare('SELECT * FROM words '
. 'JOIN clouds ON words.cloud_id = clouds.id_cloud '
. 'WHERE code = :id;'
);
$stmt = $this->db->prepare("
SELECT *
FROM words
JOIN clouds
ON words.cloud_id = clouds.id_cloud
WHERE code = :id;
");
$stmt->bindValue(':id', $id);
$stmt->execute();
$words = [];
@ -105,8 +133,6 @@ class DataBase
public function getWordsPercentage(string $id)
{
$words = $this->getWords($id);
//echo 'test';
//var_dump($words);
$total = 0;
foreach ($words as $word) {
$total += $word[1];
@ -121,8 +147,6 @@ class DataBase
public function getWordsMax(string $id)
{
$words = $this->getWords($id);
//echo 'test';
//var_dump($words);
$max = 0;
foreach ($words as $word) {
$max = max($max, $word[1]);
@ -134,10 +158,29 @@ class DataBase
return $words;
}
public function createCloud(string $ref)
public function createCloud(
string $ref,
string $text = '',
int $size = null,
string $duration = null)
{
$stmt = $this->db->prepare('INSERT INTO clouds(code) VALUES(:code);');
if (!isset($size)) {
$size = 3;
}
if (!isset($duration)) {
$duration = self::DEFAULT_DURATION;
} elseif (!in_array($duration, self::OPTIONS_DURATION)) {
$duration = self::DEFAULT_DURATION;
}
$duration = date('Y-m-d H:i:s', strtotime($duration));
$stmt = $this->db->prepare("
INSERT INTO clouds(code, text, size, delete_t)
VALUES (:code, :text, :size, :duration);
");
$stmt->bindValue(':code', $ref);
$stmt->bindValue(':text', $text);
$stmt->bindValue(':size', $size);
$stmt->bindValue(':duration', $duration, PDO::PARAM_STR);
try {
$stmt->execute();
} catch (PDOEXception $e) {
@ -146,18 +189,54 @@ class DataBase
return true;
}
public function getCloudSize(string $ref)
{
$stmt = $this->db->prepare("
SELECT *
FROM clouds
WHERE code= :code;
");
$stmt->bindValue(':code', $ref, PDO::PARAM_STR);
$stmt->execute();
if ($data = $stmt->fetch()) {
return $data['size'];
}
return null;
}
public function getCloudText(string $ref)
{
$stmt = $this->db->prepare("
SELECT *
FROM clouds
WHERE code= :code;
");
$stmt->bindValue(':code', $ref, PDO::PARAM_STR);
$stmt->execute();
if ($data = $stmt->fetch()) {
return $data['text'];
}
return null;
}
public function cleanCloud()
{
$stmt = $this->db->prepare('DELETE FROM clouds '
. "WHERE create_t < (SELECT datetime('now', '-1 week'));"
);
$stmt = $this->db->prepare("
DELETE
FROM clouds
WHERE delete_t < CURRENT_TIMESTAMP;
");
$stmt->execute();
return true;
}
public function isCloud(string $id)
{
$stmt = $this->db->prepare('SELECT * FROM clouds WHERE code = :id;');
$stmt = $this->db->prepare("
SELECT *
FROM clouds
WHERE code = :id;
");
$stmt->bindValue(':id', $id, PDO::PARAM_STR);
$stmt->execute();
if ($stmt->fetch()) {