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