2024-09-03 15:41:45 +03:00
|
|
|
|
<?php
|
|
|
|
|
namespace ContingentParser\Database;
|
|
|
|
|
|
|
|
|
|
use ContingentParser\Logger\DatabaseLogger;
|
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
|
use PDOException;
|
|
|
|
|
use PDO;
|
|
|
|
|
|
|
|
|
|
final class Database
|
|
|
|
|
{
|
2024-09-04 16:12:03 +03:00
|
|
|
|
private PDO $pdo;
|
|
|
|
|
private static $logfile = 'log/database.log';
|
|
|
|
|
private DatabaseConfig $databaseConfig;
|
|
|
|
|
private DatabaseLogger $logger;
|
2024-09-03 15:41:45 +03:00
|
|
|
|
/**
|
|
|
|
|
* Конструктор
|
|
|
|
|
* @param \ContingentParser\Database\DatabaseConfig $config
|
|
|
|
|
* Конфигурация подключения к базе данных
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(DatabaseConfig $config)
|
|
|
|
|
{
|
2024-09-04 16:12:03 +03:00
|
|
|
|
$this->logger = new DatabaseLogger(self::$logfile);
|
|
|
|
|
$this->databaseConfig = $config;
|
2024-09-03 15:41:45 +03:00
|
|
|
|
try {
|
2024-09-04 16:12:03 +03:00
|
|
|
|
$dsn = $this->databaseConfig->getDsn();
|
|
|
|
|
$username = $this->databaseConfig->getUsername();
|
|
|
|
|
$password = $this->databaseConfig->getPassword();
|
|
|
|
|
$this->pdo = new PDO(
|
2024-09-03 15:41:45 +03:00
|
|
|
|
$dsn,
|
|
|
|
|
$username,
|
|
|
|
|
$password,
|
|
|
|
|
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
|
|
|
|
|
);
|
2024-09-04 16:12:03 +03:00
|
|
|
|
$message = "Подключение к {$this->databaseConfig->getDBName()} успешно!";
|
|
|
|
|
$this->logger->log($message);
|
2024-09-03 15:41:45 +03:00
|
|
|
|
} catch (PDOException $e) {
|
2024-09-04 16:12:03 +03:00
|
|
|
|
$message = "Ошибка подключения к {$this->databaseConfig->getDBName()}: {$e->getMessage()}";
|
|
|
|
|
$this->logger->log($message);
|
2024-09-03 15:41:45 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Сообщение о разрыве соединения
|
|
|
|
|
*/
|
|
|
|
|
public function __destruct()
|
|
|
|
|
{
|
2024-09-04 16:12:03 +03:00
|
|
|
|
$message = "Подключение к {$this->databaseConfig->getDBName()} прервано!";
|
|
|
|
|
$this->logger->log($message);
|
2024-09-03 15:41:45 +03:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Выборка данных из базы
|
|
|
|
|
* @param string $sql
|
|
|
|
|
* SQL-запрос
|
|
|
|
|
* @param array $params
|
|
|
|
|
* Параметры запроса
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function select(string $sql, array $params = []) : array
|
|
|
|
|
{
|
|
|
|
|
try {
|
2024-09-04 16:12:03 +03:00
|
|
|
|
$stmt = $this->pdo->prepare($sql);
|
2024-09-03 15:41:45 +03:00
|
|
|
|
if (!empty($params)) {
|
|
|
|
|
for ($i = 0; $i < count($params); $i++) {
|
|
|
|
|
$stmt->bindParam(":v".($i+1), $params[$i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$stmt->execute();
|
|
|
|
|
$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
|
$message = "Ошибка запроса: " . $e->getMessage();
|
2024-09-04 16:12:03 +03:00
|
|
|
|
$this->logger->log($message);
|
2024-09-03 15:41:45 +03:00
|
|
|
|
} finally {
|
|
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Добавление данных в базу
|
|
|
|
|
* @param string $sql
|
|
|
|
|
* SQL-запрос
|
|
|
|
|
* @param array $params
|
|
|
|
|
* Параметры запроса
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function insert(string $sql, array $params)
|
|
|
|
|
{
|
|
|
|
|
try {
|
2024-09-04 16:12:03 +03:00
|
|
|
|
$stmt = $this->pdo->prepare($sql);
|
2024-09-03 15:41:45 +03:00
|
|
|
|
$count = 1;
|
|
|
|
|
$size = count($params[0]);
|
|
|
|
|
foreach ($params as $param) {
|
|
|
|
|
for ($i = $count; $i <= $size; $i++) {
|
|
|
|
|
$param = array_values($param);
|
|
|
|
|
$stmt->bindParam(":v$i", $param[$i-$count]);
|
|
|
|
|
}
|
|
|
|
|
$count += count($param);
|
|
|
|
|
$size += count($param);
|
|
|
|
|
}
|
|
|
|
|
$stmt->execute();
|
2024-09-04 16:12:03 +03:00
|
|
|
|
$this->logger->log("Запрос выполнен успешно!");
|
2024-09-03 15:41:45 +03:00
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
|
$message = "Ошибка запроса:" . $e->getMessage();
|
2024-09-04 16:12:03 +03:00
|
|
|
|
$this->logger->log($message);
|
2024-09-03 15:41:45 +03:00
|
|
|
|
// При ошибке запроса сохраняем валидные данные в yaml-файл
|
|
|
|
|
if ($e->getCode() === "HY000") {
|
|
|
|
|
$yaml = Yaml::dump($params);
|
|
|
|
|
file_put_contents('not-recorded-in-db.yaml', $yaml, FILE_APPEND);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Обновление данных в базе
|
|
|
|
|
* @param string $sql
|
|
|
|
|
* SQL-запрос
|
|
|
|
|
* @param array $params
|
|
|
|
|
* Параметры запроса
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function update(string $sql, array $params)
|
|
|
|
|
{
|
|
|
|
|
try {
|
2024-09-04 16:12:03 +03:00
|
|
|
|
$stmt = $this->pdo->prepare($sql);
|
2024-09-03 15:41:45 +03:00
|
|
|
|
$count = count($params);
|
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
|
$stmt->bindParam(":v".($i+1), $params[$i]);
|
|
|
|
|
}
|
|
|
|
|
// $stmt->execute();
|
2024-09-04 16:12:03 +03:00
|
|
|
|
$this->logger->log("Запрос выполнен успешно!");
|
2024-09-03 15:41:45 +03:00
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
|
$message = "Ошибка запроса:" . $e->getMessage();
|
2024-09-04 16:12:03 +03:00
|
|
|
|
$this->logger->log($message);
|
2024-09-03 15:41:45 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|