136 lines
3.7 KiB
PHP
136 lines
3.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Battles\Database;
|
|
|
|
use Battles\GameConfigs;
|
|
use PDO;
|
|
use PDOException;
|
|
|
|
class Db
|
|
{
|
|
|
|
private PDO $pdo;
|
|
private static ?self $instance = null;
|
|
|
|
private function __construct()
|
|
{
|
|
$this->connect();
|
|
}
|
|
|
|
private function connect(): void
|
|
{
|
|
$dsn = 'mysql:dbname=' . GameConfigs::DATABASE_NAME . ';host=' . GameConfigs::DATABASE_HOST . ';port=' . GameConfigs::DATABASE_PORT . ';charset=utf8;';
|
|
$user = GameConfigs::DATABASE_USER;
|
|
$password = GameConfigs::DATABASE_PASS;
|
|
try {
|
|
$this->pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT => true));
|
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
|
|
} catch (PDOException $e) {
|
|
die($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public static function getInstance(): self
|
|
{
|
|
if (is_null(self::$instance)) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function execute($query, $values = null)
|
|
{
|
|
if (is_null($values)) {
|
|
$values = [];
|
|
} elseif (!is_array($values)) {
|
|
$values = [$values];
|
|
}
|
|
$stmt = $this->pdo->prepare($query);
|
|
$stmt->execute($values);
|
|
return $stmt;
|
|
}
|
|
|
|
public function fetch($query, $values = null)
|
|
{
|
|
if (is_null($values)) {
|
|
$values = [];
|
|
} elseif (!is_array($values)) {
|
|
$values = [$values];
|
|
}
|
|
$stmt = $this->execute($query, $values);
|
|
return $stmt->fetch(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
public function fetchAll($query, $values = null, $key = null): array
|
|
{
|
|
if (is_null($values)) {
|
|
$values = [];
|
|
} elseif (!is_array($values)) {
|
|
$values = [$values];
|
|
}
|
|
$stmt = $this->execute($query, $values);
|
|
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Allows the user to retrieve results using a
|
|
// column from the results as a key for the array
|
|
if (!is_null($key) && $results[0][$key]) {
|
|
$keyedResults = [];
|
|
foreach ($results as $result) {
|
|
$keyedResults[$result[$key]] = $result;
|
|
}
|
|
$results = $keyedResults;
|
|
}
|
|
return $results;
|
|
}
|
|
|
|
public function ofetch($query, $values = null)
|
|
{
|
|
if (is_null($values)) {
|
|
$values = [];
|
|
} elseif (!is_array($values)) {
|
|
$values = [$values];
|
|
}
|
|
$stmt = $this->execute($query, $values);
|
|
return $stmt->fetch(PDO::FETCH_OBJ);
|
|
}
|
|
|
|
public function ofetchAll($query, $values = null, $key = null): object
|
|
{
|
|
if (is_null($values)) {
|
|
$values = [];
|
|
} elseif (!is_array($values)) {
|
|
$values = [$values];
|
|
}
|
|
$stmt = $this->execute($query, $values);
|
|
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
|
|
|
|
// Allows the user to retrieve results using a
|
|
// column from the results as a key for the array
|
|
if (!is_null($key) && $results[0][$key]) {
|
|
$keyedResults = (object)[];
|
|
foreach ($results as $result) {
|
|
$keyedResults->$result[$key] = $result;
|
|
}
|
|
$results = $keyedResults;
|
|
}
|
|
return $results;
|
|
}
|
|
|
|
public function lastInsertId()
|
|
{
|
|
return $this->pdo->lastInsertId();
|
|
}
|
|
|
|
public function fetchColumn($query, $values = null)
|
|
{
|
|
if (is_null($values)) {
|
|
$values = [];
|
|
} elseif (!is_array($values)) {
|
|
$values = [$values];
|
|
}
|
|
$stmt = $this->execute($query, $values);
|
|
return $stmt->fetchColumn();
|
|
}
|
|
}
|