2024-07-04 12:04:41 +03:00
|
|
|
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use \PDO;
|
|
|
|
|
|
|
|
class DB {
|
|
|
|
private static $pdoInstance = null;
|
|
|
|
private static $cache = [];
|
|
|
|
|
|
|
|
public static function connect() {
|
|
|
|
if (self::$pdoInstance === null) {
|
2024-07-22 00:36:36 +03:00
|
|
|
$dsn = 'mysql:host='.NGALLERY['root']['db']['host'].';dbname='.NGALLERY['root']['db']['name'].';charset=utf8mb4';
|
|
|
|
$username = NGALLERY['root']['db']['login'];
|
|
|
|
$password = NGALLERY['root']['db']['password'];
|
2024-07-04 12:04:41 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
self::$pdoInstance = new PDO($dsn, $username, $password);
|
|
|
|
self::$pdoInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
} catch (PDOException $ex) {
|
|
|
|
die("Connection failed: " . $ex->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$pdoInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function query($query, $params = array(), $useCache = false) {
|
|
|
|
if ($useCache && isset(self::$cache[$query])) {
|
|
|
|
return self::$cache[$query];
|
|
|
|
}
|
|
|
|
|
|
|
|
$statement = self::connect()->prepare($query);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$statement->execute($params);
|
|
|
|
|
|
|
|
if (explode(' ', $query)[0] === 'SELECT' || explode(' ', $query)[0] === 'SHOW' || explode(' ', $query)[0] === 'DESCRIBE') {
|
|
|
|
$data = $statement->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if ($useCache) {
|
|
|
|
self::$cache[$query] = $data;
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
} catch (PDOException $ex) {
|
|
|
|
die("Query failed: " . $ex->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|