Add a mechanism for initializing the database and tables
This commit is contained in:
parent
ed038beb4e
commit
4948da4024
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
.env
|
.env
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
app/database.db
|
||||||
|
|
|
@ -20,8 +20,12 @@ class Database
|
||||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Check to see if the database exists; if not, we have to build the tables
|
||||||
|
$new = !file_exists($path);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
self::$c = new PDO("sqlite:$path", null, null, $opts);
|
self::$c = new PDO("sqlite:$path", null, null, $opts);
|
||||||
|
if ($new) { self::build(); }
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
@ -50,10 +54,82 @@ class Database
|
||||||
return $selectQuery;
|
return $selectQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function count(string $from, string $condition = ''): mixed
|
public static function count(string $from, string $condition = ''): mixed
|
||||||
{
|
{
|
||||||
$query = "SELECT COUNT(*) FROM $from";
|
$query = "SELECT COUNT(*) FROM $from";
|
||||||
if (!empty($condition)) { $query .= " $condition"; }
|
if (!empty($condition)) { $query .= " $condition"; }
|
||||||
return self::$c->query($query)->fetch();
|
return self::$c->query($query)->fetch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function build()
|
||||||
|
{
|
||||||
|
// Create the users table
|
||||||
|
self::$c->exec("CREATE TABLE users (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
username TEXT NOT NULL,
|
||||||
|
password TEXT NOT NULL,
|
||||||
|
email TEXT NOT NULL,
|
||||||
|
title TEXT NOT NULL DEFAULT 'Newbie',
|
||||||
|
gender INTEGER NOT NULL DEFAULT 0,
|
||||||
|
badges TEXT NOT NULL DEFAULT '',
|
||||||
|
blurb TEXT NOT NULL DEFAULT '',
|
||||||
|
status TEXT NOT NULL DEFAULT '',
|
||||||
|
bday DATE NOT NULL DEFAULT '0000-00-00',
|
||||||
|
posts INTEGER NOT NULL DEFAULT 0,
|
||||||
|
avatar TEXT NOT NULL DEFAULT '',
|
||||||
|
reputation INTEGER NOT NULL DEFAULT 0,
|
||||||
|
mlevel INTEGER NOT NULL DEFAULT 0,
|
||||||
|
joindate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);");
|
||||||
|
|
||||||
|
// Create the articles table
|
||||||
|
self::$c->exec("CREATE TABLE articles (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
articleName TEXT NOT NULL,
|
||||||
|
articleCover TEXT NOT NULL,
|
||||||
|
articleAuthor INTEGER NOT NULL DEFAULT 1,
|
||||||
|
articleContent TEXT NOT NULL,
|
||||||
|
articleDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
articleComments INTEGER NOT NULL DEFAULT 0,
|
||||||
|
articleDescription TEXT NOT NULL DEFAULT ''
|
||||||
|
);");
|
||||||
|
|
||||||
|
// Create the article comments table
|
||||||
|
self::$c->exec("CREATE TABLE articlecomments (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
commentAuthor INTEGER NOT NULL,
|
||||||
|
commentContent TEXT NOT NULL,
|
||||||
|
commentDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
articleID INTEGER NOT NULL
|
||||||
|
);");
|
||||||
|
|
||||||
|
// Create the episode comments table
|
||||||
|
self::$c->exec("CREATE TABLE episodecomments (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
commentAuthor INTEGER NOT NULL,
|
||||||
|
commentContent TEXT NOT NULL,
|
||||||
|
commentDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
showID INTEGER NOT NULL
|
||||||
|
);");
|
||||||
|
|
||||||
|
// Create the projects table
|
||||||
|
self::$c->exec("CREATE TABLE projects (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
type TEXT NOT NULL,
|
||||||
|
desc TEXT NOT NULL,
|
||||||
|
title TEXT NOT NULL,
|
||||||
|
cover TEXT NOT NULL,
|
||||||
|
lastUpdate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);");
|
||||||
|
|
||||||
|
// Create the shows table
|
||||||
|
self::$c->exec("CREATE TABLE shows (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
title TEXT NOT NULL,
|
||||||
|
thumbnail TEXT NOT NULL,
|
||||||
|
desc TEXT NOT NULL,
|
||||||
|
showid INTEGER NOT NULL,
|
||||||
|
lastUpdate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user