aboutsummaryrefslogtreecommitdiff
path: root/lib/database.php
diff options
context:
space:
mode:
authorArno Richter <oelna@oelna.de>2022-12-13 22:27:21 +0100
committerArno Richter <oelna@oelna.de>2022-12-13 22:27:44 +0100
commit0b075f3ea2616cfde4d976199a69ba631174a336 (patch)
treebb1feef58d9c714f6c3fb0b75f1e52b0181cf2af /lib/database.php
parentf0e3ff408db8ee40611f75cdf96892f90034bd60 (diff)
downloadmicroblog-0b075f3ea2616cfde4d976199a69ba631174a336.tar.gz
microblog-0b075f3ea2616cfde4d976199a69ba631174a336.tar.bz2
microblog-0b075f3ea2616cfde4d976199a69ba631174a336.zip
gave up and sorted files into a directory structure. made snippets for header, nav and footer. made it easier to add additional css files as themes. prepare a little for addition of a real template engine. added a css reset file to share between themes, if warranted.
Diffstat (limited to 'lib/database.php')
-rw-r--r--lib/database.php56
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/database.php b/lib/database.php
new file mode 100644
index 0000000..0ffdb3a
--- /dev/null
+++ b/lib/database.php
@@ -0,0 +1,56 @@
+<?php
+
+//connect or create the database
+try {
+ $db = new PDO('sqlite:'.ROOT.DS.'posts.db');
+ $config['db_version'] = $db->query("PRAGMA user_version")->fetch(PDO::FETCH_ASSOC)['user_version'];
+} catch(PDOException $e) {
+ print 'Exception : '.$e->getMessage();
+ die('cannot connect to or open the database');
+}
+
+// first time setup
+if($config['db_version'] == 0) {
+ try {
+ $db->exec("CREATE TABLE IF NOT EXISTS `posts` (
+ `id` integer PRIMARY KEY NOT NULL,
+ `post_content` TEXT,
+ `post_timestamp` INTEGER
+ ); PRAGMA `user_version` = 1;");
+ $config['db_version'] = 1;
+ } catch(PDOException $e) {
+ print 'Exception : '.$e->getMessage();
+ die('cannot set up initial database table!');
+ }
+}
+
+// upgrade database to v2
+if($config['db_version'] == 1) {
+ try {
+ $db->exec("PRAGMA user_version = 2;
+ ALTER TABLE `posts` ADD `post_thread` INTEGER;
+ ALTER TABLE `posts` ADD `post_edited` INTEGER;
+ ALTER TABLE `posts` ADD `post_deleted` INTEGER;
+ ");
+ $config['db_version'] = 2;
+ } catch(PDOException $e) {
+ print 'Exception : '.$e->getMessage();
+ die('cannot upgrade database table to v2!');
+ }
+}
+
+// upgrade database to v3
+if($config['db_version'] == 2) {
+ try {
+ $db->exec("PRAGMA user_version = 3;
+ ALTER TABLE `posts` ADD `post_guid` TEXT;
+ ");
+ $config['db_version'] = 3;
+ } catch(PDOException $e) {
+ print 'Exception : '.$e->getMessage();
+ die('cannot upgrade database table to v3!');
+ }
+}
+
+// debug: get a list of post table columns
+// var_dump($db->query("PRAGMA table_info(`posts`)")->fetchAll(PDO::FETCH_COLUMN, 1));