From 44226c48099280572008d1be4f358e2b31894bb6 Mon Sep 17 00:00:00 2001 From: Gregory Trolliet Date: Sun, 22 Nov 2020 15:09:31 +0100 Subject: [PATCH] Switch from SQLite to PostgreSQL --- db.php | 25 +++++++++++++++---------- dbconfig.php | 7 +++++++ get.php | 2 +- test.db | Bin 16384 -> 0 bytes 4 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 dbconfig.php delete mode 100644 test.db diff --git a/db.php b/db.php index beb54fa..a5ceb29 100644 --- a/db.php +++ b/db.php @@ -1,4 +1,6 @@ db = new PDO('sqlite:test.db'); + $this->db = new PDO($dsn, $username, $password); $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); } catch (PDOException $e) { echo $e->getMessage(); return null; } - $stmt = $this->db->prepare('PRAGMA foreign_keys = 1;'); - $stmt->execute(); } public function isInit() @@ -38,18 +43,18 @@ class DataBase { $stmt = $this->db->prepare(" CREATE TABLE IF NOT EXISTS clouds( - id_cloud INTEGER PRIMARY KEY, + id_cloud serial PRIMARY KEY, code TEXT NOT NULL UNIQUE, size INTEGER NOT NULL DEFAULT 3, text TEXT, delete_t TIMESTAMP DEFAULT - (datetime('now', '" . self::DEFAULT_DURATION . "')) + (CURRENT_TIMESTAMP + INTERVAL '" . self::DEFAULT_DURATION . "') ); "); $stmt->execute(); $stmt = $this->db->prepare(" CREATE TABLE IF NOT EXISTS words( - id_word INTEGER PRIMARY KEY, + id_word serial PRIMARY KEY, word TEXT NOT NULL, count INT DEFAULT 1, cloud_id INT NOT NULL, @@ -107,7 +112,7 @@ class DataBase $stmt->execute(); } } - public function getWords(string $id) + public function getWordsList(string $id) { $stmt = $this->db->prepare(" SELECT * @@ -132,7 +137,7 @@ class DataBase public function getWordsPercentage(string $id) { - $words = $this->getWords($id); + $words = $this->getWordsList($id); $total = 0; foreach ($words as $word) { $total += $word[1]; @@ -144,9 +149,9 @@ class DataBase return $words; } - public function getWordsMax(string $id) + public function getWordsRelative(string $id) { - $words = $this->getWords($id); + $words = $this->getWordsList($id); $max = 0; foreach ($words as $word) { $max = max($max, $word[1]); diff --git a/dbconfig.php b/dbconfig.php new file mode 100644 index 0000000..6e6578d --- /dev/null +++ b/dbconfig.php @@ -0,0 +1,7 @@ +getWordsMax($id)) { +if ($words = $db->getWordsRelative($id)) { echo json_encode($words); } else { echo json_encode('No id'); diff --git a/test.db b/test.db deleted file mode 100644 index 1f91dddc2295e0c0134b9590642e055ab2b8e620..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16384 zcmeI%v2N2q7y#hAIB9!n(_=wV)a8~ov6`YJrmE-yCb?D-Y1%rDL>D3_KENooOMFoa z6IDl^fG2>R7l0KWfCu0OkXRTw+qkq6s)WQ)q5oITvH$z?$M?mHo!)-OlY%`CCWnq> z2D*VTM)w#)2(74*RU?fBwR7Xh!|ZIaRKjPbbcSOw8V6H1OtzV(#Z8+t+q~c5EXh^!M1|)L;zQQ% z+I*W^Y|m=%nAQP%zz?c%#VBwEi^Ap)ZPwjYqu=SUez(2f=SfNEy-Z5dCg)WxzGe11 zHZzh!DNf{huO#J)v5;aQncd#uJ=@&bW4}HWe>^036k1{RZ;lDFF z{eeDSuM6!j&t^3@Q@mTk$n#xs5OB|rFQ|dlO*!0` zM;Z_y00JNY0w4eaAOHd&00JNY0w8eh1q?icmT}n_iaU<8-dHz=#zxi_p=9d8l0S8x zh(@hZTdUXC8U}0BHrMN$x9fSeEKI*4^@0Ea5C8!X009sH0T2KI5C8!X009vAPXws8 zOfEhkD9{gx&{v3lr(fw8`j&p8Z|HmaTBRUB00ck)1V8`;KmY_l00ck)1V8`;{*eG7 zIdqedBggX>=5lC>kdZS9q!bG?IaI>rXzch>X!)!v!({Z-k=glKHABdu