2021-01-28 15:57:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace Battles\Database;
|
2021-03-10 21:03:59 +00:00
|
|
|
use Battles\GameConfigs;
|
2021-01-28 15:57:55 +00:00
|
|
|
use PDO, PDOException;
|
|
|
|
class DBPDO
|
|
|
|
{
|
|
|
|
|
|
|
|
public $pdo;
|
2021-01-28 21:05:34 +00:00
|
|
|
private static $_instance = null;
|
2021-08-25 01:44:36 +00:00
|
|
|
public static DBPDO $db;
|
2021-01-28 15:57:55 +00:00
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
$this->connect();
|
|
|
|
}
|
|
|
|
|
2021-01-28 21:05:34 +00:00
|
|
|
public static function INIT(): DBPDO
|
|
|
|
{
|
|
|
|
if (!self::$_instance) {
|
|
|
|
self::$_instance = new DBPDO();
|
|
|
|
}
|
|
|
|
return self::$_instance;
|
|
|
|
}
|
2021-01-28 15:57:55 +00:00
|
|
|
|
|
|
|
function prep_query($query)
|
|
|
|
{
|
|
|
|
return $this->pdo->prepare($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function connect():bool
|
|
|
|
{
|
|
|
|
if (!$this->pdo) {
|
|
|
|
|
2021-03-10 21:03:59 +00:00
|
|
|
$dsn = 'mysql:dbname=' . GameConfigs::DATABASE_NAME . ';host=' . GameConfigs::DATABASE_HOST . ';port=' . GameConfigs::DATABASE_PORT . ';charset=utf8;';
|
|
|
|
$user = GameConfigs::DATABASE_USER;
|
|
|
|
$password = GameConfigs::DATABASE_PASS;
|
2021-01-28 15:57:55 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT => true));
|
|
|
|
return true;
|
|
|
|
} catch (PDOException $e) {
|
2021-02-01 14:39:48 +00:00
|
|
|
die($e->getMessage());
|
2021-01-28 15:57:55 +00:00
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2021-01-28 21:05:34 +00:00
|
|
|
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]) {
|
2021-01-31 23:43:48 +00:00
|
|
|
$keyed_results = (object) array();
|
2021-01-28 21:05:34 +00:00
|
|
|
foreach ($results as $result) {
|
2021-01-31 23:43:48 +00:00
|
|
|
$keyed_results->$result[$key] = $result;
|
2021-01-28 21:05:34 +00:00
|
|
|
}
|
|
|
|
$results = $keyed_results;
|
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2021-01-28 15:57:55 +00:00
|
|
|
function lastInsertId()
|
|
|
|
{
|
|
|
|
return $this->pdo->lastInsertId();
|
|
|
|
}
|
|
|
|
}
|