battles/classes/Battles/Database/DBPDO.php

154 lines
4.0 KiB
PHP

<?php
namespace Battles\Database;
use Battles\GameConfigs;
use PDO, PDOException;
class DBPDO
{
public $pdo;
private static $_instance = null;
public static DBPDO $db;
function __construct()
{
$this->connect();
}
public static function INIT(): DBPDO
{
if (!self::$_instance) {
self::$_instance = new DBPDO();
}
return self::$_instance;
}
function prep_query($query)
{
return $this->pdo->prepare($query);
}
function connect():bool
{
if (!$this->pdo) {
$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));
return true;
} catch (PDOException $e) {
die($e->getMessage());
}
} else {
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
return true;
}
}
function table_exists($table_name)
{
$stmt = $this->prep_query('SHOW TABLES LIKE ?');
$stmt->execute(array($table_name));
return $stmt->rowCount() > 0;
}
function execute($query, $values = null)
{
if ($values == null) {
$values = array();
} else if (!is_array($values)) {
$values = array($values);
}
$stmt = $this->prep_query($query);
$stmt->execute($values);
return $stmt;
}
function fetch($query, $values = null)
{
if ($values == null) {
$values = array();
} else if (!is_array($values)) {
$values = array($values);
}
$stmt = $this->execute($query, $values);
return $stmt->fetch(PDO::FETCH_ASSOC);
}
function fetchAll($query, $values = null, $key = null)
{
if ($values == null) {
$values = array();
} else if (!is_array($values)) {
$values = array($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 ($key != null && $results[0][$key]) {
$keyed_results = array();
foreach ($results as $result) {
$keyed_results[$result[$key]] = $result;
}
$results = $keyed_results;
}
return $results;
}
function ofetch($query, $values = null)
{
if ($values == null) {
$values = array();
} else if (!is_array($values)) {
$values = array($values);
}
$stmt = $this->execute($query, $values);
return $stmt->fetch(PDO::FETCH_OBJ);
}
function ofetchAll($query, $values = null, $key = null)
{
if ($values == null) {
$values = array();
} else if (!is_array($values)) {
$values = array($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 ($key != null && $results[0][$key]) {
$keyed_results = (object) array();
foreach ($results as $result) {
$keyed_results->$result[$key] = $result;
}
$results = $keyed_results;
}
return $results;
}
function lastInsertId()
{
return $this->pdo->lastInsertId();
}
function fetchColumn($query, $values = null) {
if (is_null($values)) {
$values = [];
} elseif (!is_array($values)) {
$values = [$values];
}
$stmt = $this->execute($query, $values);
return $stmt->fetchColumn();
}
}