Adds database helper functions

main
Yasen Pramatarov 2024-08-10 21:42:44 +03:00
parent 91aac20998
commit aeb837fee5
10 changed files with 128 additions and 48 deletions

View File

@ -3,8 +3,12 @@
$config = [
'domain' => 'localhost',
'folder' => '/jilo-web/',
'database' => '/home/yasen/work/code/git/lindeas-code/jilo-web/jilo-web.db',
'jilo_database' => '/home/yasen/work/code/git/lindeas-code/jilo/jilo.db',
'jilo_database' => '../../jilo/jilo.db',
'db_type' => 'sqlite',
'sqlite_file' => '../jilo-web.db',
'registration_enabled' => true,
'login_message' => '',
'version' => '0.1.1',

View File

@ -3,25 +3,68 @@
class Database {
private $pdo;
public function __construct($dbFile) {
public function __construct($options) {
// pdo needed
if ( !extension_loaded('pdo') ) {
throw new Exception('PDO extension not loaded.');
}
// pdo and pdo_sqlite needed
if ( !extension_loaded('pdo_sqlite') ) {
// options check
if (empty($options['type'])) {
throw new Exception('Database type is not set.');
}
// database type
switch ($options['type']) {
case 'sqlite':
$this->connectSqlite($options);
break;
case 'mysql' || 'mariadb':
$this->connectMysql($options);
break;
default:
throw newException("Database type \"{$options['type']}\" is not supported.");
}
}
private function connectSqlite($options) {
// pdo_sqlite extension is needed
if (!extension_loaded('pdo_sqlite')) {
throw new Exception('PDO extension for SQLite not loaded.');
}
// database file check
if (empty($dbFile) || !file_exists($dbFile)) {
throw new Exception('Database file is not found.');
// SQLite options
if (empty($options['dbFile']) || !file_exists($options['dbFile'])) {
throw new Exception('SQLite database file not found.');
}
// connect to database
// FIXME: add mysql/mariadb option
// connect to SQLite
try {
$this->pdo = new PDO("sqlite:" . $dbFile);
$this->pdo = new PDO("sqlite:" . $options['dbFile']);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
throw new Exception('DB connection failed: ' . $e->getMessage());
throw new Exception('SQLite connection failed: ' . $e->getMessage());
}
}
private function connectMysql($options) {
// pdo_mysql extension is needed
if (!extension_loaded('pdo_mysql')) {
throw new Exception('PDO extension for MySQL not loaded.');
}
// MySQL options
if (empty($options['host']) || empty($options['dbname']) || empty($options['user'])) {
throw new Exception('MySQL connection data is missing.');
}
// Connect to MySQL
try {
$dsn = "mysql:host={$options['host']};port={$options['port']};dbname={$options['dbname']};charset=utf8";
$this->pdo = new PDO($dsn, $options['user'], $options['password'] ?? '');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
throw new Exception('MySQL connection failed: ' . $e->getMessage());
}
}

View File

@ -0,0 +1,43 @@
<?php
// connect to database
function connectDB($config) {
// sqlite database file
if ($config['db_type'] === 'sqlite') {
try {
$db = new Database([
'type' => $config['db_type'],
'dbFile' => $config['sqlite_file'],
]);
$pdo = $db->getConnection();
} catch (Exception $e) {
$error = 'Error: ' . $e->getMessage();
include 'templates/block-message.php';
exit();
}
// mysql/mariadb database
} elseif ($config['db_type'] === 'mysql' || $config['db_type'] === 'mariadb') {
try {
$db = new Database([
'type' => $config['db_type'],
'host' => $config['sql_host'] ?? 'localhost',
'port' => $config['sql_port'] ?? '3306',
'dbname' => $config['sql_database'],
'user' => $config['sql_username'],
'password' => $config['sql_password'],
]);
$pdo = $db->getConnection();
} catch (Exception $e) {
$error = 'Error: ' . $e->getMessage();
include 'templates/block-message.php';
exit();
}
// unknown database
} else {
$error = "Error: unknow database type \"{$config['db_type']}\"";
include 'templates/block-message.php';
exit();
}
return $db;
}
?>

View File

@ -54,6 +54,7 @@ if ($config_file) {
$app_root = $config['folder'];
session_name('jilo');
session_start();
if (isset($_GET['page'])) {

View File

@ -3,6 +3,10 @@
require_once 'classes/database.php';
require 'classes/component.php';
// connect to database
require 'helpers/database.php';
$db = connectDB($config);
// FIXME move thi sto a special function
$time_range_specified = false;
if (!isset($_REQUEST['from_time']) || (isset($_REQUEST['from_time']) && $_REQUEST['from_time'] == '')) {
@ -32,15 +36,6 @@ if (isset($_REQUEST['name']) && $_REQUEST['name'] != '') {
$component_id = 'component_id';
}
// connect to database
try {
$db = new Database($config['jilo_database']);
} catch (Exception $e) {
$error = 'Error: ' . $e->getMessage();
include 'templates/block-message.php';
exit();
}
//
// Component events listings

View File

@ -3,6 +3,10 @@
require_once 'classes/database.php';
require 'classes/conference.php';
// connect to database
require 'helpers/database.php';
$db = connectDB($config);
// FIXME move thi sto a special function
$time_range_specified = false;
if (!isset($_REQUEST['from_time']) || (isset($_REQUEST['from_time']) && $_REQUEST['from_time'] == '')) {
@ -33,15 +37,6 @@ if (isset($_REQUEST['id']) && $_REQUEST['id'] != '') {
unset($conferenceName);
}
// connect to database
try {
$db = new Database($config['jilo_database']);
} catch (Exception $e) {
$error = 'Error: ' . $e->getMessage();
include 'templates/block-message.php';
exit();
}
//
// Conference listings

View File

@ -5,13 +5,9 @@ require 'classes/conference.php';
require 'classes/participant.php';
// connect to database
try {
$db = new Database($config['jilo_database']);
} catch (Exception $e) {
$error = 'Error: ' . $e->getMessage();
include 'templates/block-message.php';
exit();
}
require 'helpers/database.php';
$db = connectDB($config);
//
// dashboard widget listings

View File

@ -7,7 +7,11 @@ require 'classes/user.php';
unset($error);
try {
$db = new Database($config['database']);
// connect to database
require 'helpers/database.php';
$db = connectDB($config);
$user = new User($db);
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

View File

@ -3,6 +3,10 @@
require_once 'classes/database.php';
require 'classes/participant.php';
// connect to database
require 'helpers/database.php';
$db = connectDB($config);
// FIXME move thi sto a special function
$time_range_specified = false;
if (!isset($_REQUEST['from_time']) || (isset($_REQUEST['from_time']) && $_REQUEST['from_time'] == '')) {
@ -37,15 +41,6 @@ if (isset($_REQUEST['id']) && $_REQUEST['id'] != '') {
unset($participantName);
}
// connect to database
try {
$db = new Database($config['jilo_database']);
} catch (Exception $e) {
$error = 'Error: ' . $e->getMessage();
include 'templates/block-message.php';
exit();
}
//
// Participant listings

View File

@ -8,7 +8,11 @@ if ($config['registration_enabled'] === true) {
unset($error);
try {
$db = new Database($config['database']);
// connect to database
require 'helpers/database.php';
$db = connectDB($config);
$user = new User($db);
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {