2022-11-06 01:07:10 +01:00
|
|
|
<?php
|
2023-06-11 18:59:07 +02:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-11-29 21:21:26 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2022-11-06 01:07:10 +01:00
|
|
|
namespace App\Services\Misc;
|
|
|
|
|
2023-06-11 14:55:06 +02:00
|
|
|
use Doctrine\DBAL\Exception;
|
2022-11-06 01:07:10 +01:00
|
|
|
use Doctrine\DBAL\Connection;
|
|
|
|
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
|
|
|
|
use Doctrine\DBAL\Platforms\SqlitePlatform;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This service provides db independent information about the database.
|
|
|
|
*/
|
|
|
|
class DBInfoHelper
|
|
|
|
{
|
|
|
|
protected Connection $connection;
|
|
|
|
|
2023-06-11 14:15:46 +02:00
|
|
|
public function __construct(protected EntityManagerInterface $entityManager)
|
2022-11-06 01:07:10 +01:00
|
|
|
{
|
|
|
|
$this->connection = $entityManager->getConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the database type of the used database.
|
|
|
|
* @return string|null Returns 'mysql' for MySQL/MariaDB and 'sqlite' for SQLite. Returns null if unknown type
|
|
|
|
*/
|
|
|
|
public function getDatabaseType(): ?string
|
|
|
|
{
|
|
|
|
if ($this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform) {
|
|
|
|
return 'mysql';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
|
|
|
|
return 'sqlite';
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the database version of the used database.
|
2023-06-11 14:55:06 +02:00
|
|
|
* @throws Exception
|
2022-11-06 01:07:10 +01:00
|
|
|
*/
|
|
|
|
public function getDatabaseVersion(): ?string
|
|
|
|
{
|
|
|
|
if ($this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform) {
|
|
|
|
return $this->connection->fetchOne('SELECT VERSION()');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
|
|
|
|
return $this->connection->fetchOne('SELECT sqlite_version()');
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2023-01-16 00:06:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the database size in bytes.
|
|
|
|
* @return int|null The database size in bytes or null if unknown
|
2023-06-11 14:55:06 +02:00
|
|
|
* @throws Exception
|
2023-01-16 00:06:14 +01:00
|
|
|
*/
|
|
|
|
public function getDatabaseSize(): ?int
|
|
|
|
{
|
|
|
|
if ($this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform) {
|
|
|
|
try {
|
|
|
|
return $this->connection->fetchOne('SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema = DATABASE()');
|
2023-06-11 14:55:06 +02:00
|
|
|
} catch (Exception) {
|
2023-01-16 00:06:14 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
|
|
|
|
try {
|
|
|
|
return $this->connection->fetchOne('SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size();');
|
2023-06-11 14:55:06 +02:00
|
|
|
} catch (Exception) {
|
2023-01-16 00:06:14 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-01-16 00:15:15 +01:00
|
|
|
/**
|
|
|
|
* Returns the name of the database.
|
|
|
|
*/
|
|
|
|
public function getDatabaseName(): ?string
|
|
|
|
{
|
|
|
|
return $this->connection->getDatabase() ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name of the database user.
|
|
|
|
*/
|
|
|
|
public function getDatabaseUsername(): ?string
|
|
|
|
{
|
|
|
|
if ($this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform) {
|
|
|
|
try {
|
|
|
|
return $this->connection->fetchOne('SELECT USER()');
|
2023-06-11 14:55:06 +02:00
|
|
|
} catch (Exception) {
|
2023-01-16 00:15:15 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
|
|
|
|
return 'sqlite';
|
|
|
|
}
|
2023-01-17 12:23:12 +01:00
|
|
|
|
|
|
|
return null;
|
2023-01-16 00:15:15 +01:00
|
|
|
}
|
2023-01-16 00:06:14 +01:00
|
|
|
|
2023-01-17 12:23:12 +01:00
|
|
|
}
|