Part-DB.Part-DB-server/src/Services/Misc/DBInfoHelper.php

132 lines
4 KiB
PHP
Raw Normal View History

<?php
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/>.
*/
namespace App\Services\Misc;
2023-06-11 14:55:06 +02:00
use Doctrine\DBAL\Exception;
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;
public function __construct(protected EntityManagerInterface $entityManager)
{
$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
*/
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;
}
/**
* 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) {
return null;
}
}
if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
return 'sqlite';
}
2023-01-17 12:23:12 +01:00
return null;
}
2023-01-16 00:06:14 +01:00
2023-01-17 12:23:12 +01:00
}