*/ 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); } } }