136 lines
4.1 KiB
PHP
Executable File
136 lines
4.1 KiB
PHP
Executable File
<?php
|
|
|
|
/*
|
|
--- The Database Module
|
|
--- This module is for the general CRUD methods.
|
|
--- This module was crafted by Skylear. : )
|
|
--- Copyright (c) Mad Splash, 2014, all rights reserved.
|
|
*/
|
|
|
|
class Database
|
|
{
|
|
/* --- Variables - data the module needs to work with. --- */
|
|
private static PDO $c;
|
|
|
|
/* --- Constructor - creates the module's instance. --- */
|
|
public static function init(string $path, array $opts = [])
|
|
{
|
|
$opts = !empty($opts) ? $opts : [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
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 {
|
|
self::$c = new PDO("sqlite:$path", null, null, $opts);
|
|
if ($new) { self::build(); }
|
|
} catch (PDOException $e) {
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/* --- Module Functions - the meat and purpose of the module. --- */
|
|
/* Create Table - for creating a table when need be.
|
|
* @param string $name - the name of the table to be created.
|
|
* @param string $columns - a long string containing the columns to be made.
|
|
*/
|
|
public static function createTable($name, $columns) {
|
|
// No need for creating tables for the time being.
|
|
}
|
|
|
|
/* Select Data - used for getting data from the database.
|
|
* @return array $selectedRos - an array containing the data.
|
|
*/
|
|
public static function select(string $from, string $retrieve, string $condition, string $orderby = '', int $limit = 0, array $data = [])
|
|
{
|
|
$query = "SELECT $retrieve FROM $from $condition";
|
|
if (!empty($orderby)) { $query .= " ORDER BY $orderby"; }
|
|
if ($limit > 0) { $query .= " LIMIT $limit"; }
|
|
|
|
$selectQuery = self::$c->prepare($query);
|
|
$selectQuery->execute($data);
|
|
return $selectQuery;
|
|
}
|
|
|
|
public static function count(string $from, string $condition = ''): mixed
|
|
{
|
|
$query = "SELECT COUNT(*) FROM $from";
|
|
if (!empty($condition)) { $query .= " $condition"; }
|
|
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
|
|
);");
|
|
}
|
|
}
|