game/_incl_data/class/Core/Db.php

157 lines
3.3 KiB
PHP
Raw Normal View History

2022-12-30 19:03:37 +00:00
<?php
namespace Core;
use PDO;
use PDOException;
use PDOStatement;
class Db
{
private static PDO $db;
private static self $instance;
/**
* DB constructor.
* @throws PDOException
2022-12-30 19:03:37 +00:00
*/
public function __construct()
{
try {
self::$db = Database::pdoinit();
} catch (PDOException $e) {
throw new PDOException($e->getMessage());
2022-12-30 19:03:37 +00:00
}
}
/**
* @param string $query
* @return false|int
2022-12-30 19:03:37 +00:00
*/
public static function exec(string $query): false|int
2022-12-30 19:03:37 +00:00
{
self::init();
return self::$db->exec($query);
}
2023-07-31 17:06:51 +00:00
private static function init(): void
2022-12-30 19:03:37 +00:00
{
2023-07-31 17:06:51 +00:00
self::$instance ??= new self();
2022-12-30 19:03:37 +00:00
}
/**
* @param ?string $name [optional] Name of the sequence object from which the ID should be returned.
* @return false|string
2022-12-30 19:03:37 +00:00
*/
public static function lastInsertId(?string $name = null): false|string
2022-12-30 19:03:37 +00:00
{
self::init();
return self::$db->lastInsertId($name);
2022-12-30 19:03:37 +00:00
}
/**
2023-07-31 17:06:51 +00:00
* @param string $query
* @param array $args
* @return array
2022-12-30 19:03:37 +00:00
*/
public static function getRows(string $query, array $args = []): array
2022-12-30 19:03:37 +00:00
{
$stmt = self::run($query, $args)->fetchAll();
if (!$stmt) {
return [];
}
return $stmt;
2022-12-30 19:03:37 +00:00
}
/**
* @param string $query
* @param array $args
* @return false|PDOStatement
2022-12-30 19:03:37 +00:00
*/
public static function run(string $query, array $args = []): false|PDOStatement
2022-12-30 19:03:37 +00:00
{
try {
if (!$args) {
return self::query($query);
}
$stmt = self::prepare($query);
$stmt->execute($args);
return $stmt;
} catch (PDOException $e) {
throw new PDOException($e->getMessage());
}
}
/**
2023-07-31 17:06:51 +00:00
* @param string $stmt
* @return false|PDOStatement
2022-12-30 19:03:37 +00:00
*/
private static function query(string $stmt): false|PDOStatement
2022-12-30 19:03:37 +00:00
{
2023-07-31 17:06:51 +00:00
self::init();
return self::$db->query($stmt);
2022-12-30 19:03:37 +00:00
}
/**
2023-07-31 17:06:51 +00:00
* @param string $stmt
* @return false|PDOStatement
2022-12-30 19:03:37 +00:00
*/
public static function prepare(string $stmt): false|PDOStatement
2022-12-30 19:03:37 +00:00
{
2023-07-31 17:06:51 +00:00
self::init();
return self::$db->prepare($stmt);
2022-12-30 19:03:37 +00:00
}
/**
* @param string $query
* @param array $args
* @return mixed
*/
public static function getValue(string $query, array $args = []): mixed
2022-12-30 19:03:37 +00:00
{
$result = self::getRow($query, $args);
if (!empty($result)) {
return array_shift($result);
2022-12-30 19:03:37 +00:00
}
return '';
2022-12-30 19:03:37 +00:00
}
2023-07-31 17:06:51 +00:00
/**
* @param string $query
* @param array $args
* @return array
2023-07-31 17:06:51 +00:00
*/
public static function getRow(string $query, array $args = []): array
2023-07-31 17:06:51 +00:00
{
$stmt = self::run($query, $args)->fetch();
if (!$stmt) {
return [];
}
return $stmt;
2023-07-31 17:06:51 +00:00
}
2022-12-30 19:03:37 +00:00
/**
* @param string $query
* @param array $args
* @return array
*/
public static function getColumn(string $query, array $args = []): array
{
$stmt = self::run($query, $args)->fetchAll(PDO::FETCH_COLUMN);
if (!$stmt) {
return [];
}
return $stmt;
2022-12-30 19:03:37 +00:00
}
/**
* @param string $query
* @param array $args
*/
public static function sql(string $query, array $args = []): void
2022-12-30 19:03:37 +00:00
{
self::run($query, $args);
}
}