77 lines
2.1 KiB
PHP
77 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace danog\MadelineProto\Db\Driver;
|
|
|
|
use Amp\Mysql\ConnectionConfig;
|
|
use Amp\Mysql\Pool;
|
|
use danog\MadelineProto\Logger;
|
|
use danog\MadelineProto\Settings\Database\Mysql as DatabaseMysql;
|
|
|
|
use function Amp\Mysql\Pool;
|
|
|
|
/**
|
|
* MySQL driver wrapper.
|
|
*/
|
|
class Mysql
|
|
{
|
|
/** @var Pool[] */
|
|
private static array $connections = [];
|
|
|
|
/**
|
|
* @param string $host
|
|
* @param int $port
|
|
* @param string $user
|
|
* @param string $password
|
|
* @param string $db
|
|
*
|
|
* @param int $maxConnections
|
|
* @param int $idleTimeout
|
|
*
|
|
* @throws \Amp\Sql\ConnectionException
|
|
* @throws \Amp\Sql\FailureException
|
|
* @throws \Throwable
|
|
*
|
|
* @return \Generator<Pool>
|
|
*/
|
|
public static function getConnection(DatabaseMysql $settings): \Generator
|
|
{
|
|
$dbKey = $settings->getKey();
|
|
if (empty(static::$connections[$dbKey])) {
|
|
$config = ConnectionConfig::fromString("host=".\str_replace("tcp://", "", $settings->getUri()))
|
|
->withUser($settings->getUsername())
|
|
->withPassword($settings->getPassword())
|
|
->withDatabase($settings->getDatabase());
|
|
|
|
yield from static::createDb($config);
|
|
static::$connections[$dbKey] = pool($config, $settings->getMaxConnections(), $settings->getIdleTimeout());
|
|
}
|
|
|
|
return static::$connections[$dbKey];
|
|
}
|
|
|
|
/**
|
|
* @param ConnectionConfig $config
|
|
*
|
|
* @throws \Amp\Sql\ConnectionException
|
|
* @throws \Amp\Sql\FailureException
|
|
* @throws \Throwable
|
|
*
|
|
* @return \Generator
|
|
*/
|
|
private static function createDb(ConnectionConfig $config): \Generator
|
|
{
|
|
try {
|
|
$db = $config->getDatabase();
|
|
$connection = pool($config->withDatabase(null));
|
|
yield $connection->query("
|
|
CREATE DATABASE IF NOT EXISTS `{$db}`
|
|
CHARACTER SET 'utf8mb4'
|
|
COLLATE 'utf8mb4_general_ci'
|
|
");
|
|
$connection->close();
|
|
} catch (\Throwable $e) {
|
|
Logger::log($e->getMessage(), Logger::ERROR);
|
|
}
|
|
}
|
|
}
|