| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- declare(strict_types=1);
- /**
- * This file is part of Simps.
- *
- * @link https://simps.io
- * @document https://doc.simps.io
- * @license https://github.com/simple-swoole/simps/blob/master/LICENSE
- */
- namespace Simps\DB;
- use RuntimeException;
- use Swoole\Database\PDOConfig;
- use Swoole\Database\PDOPool;
- class PDO
- {
- protected $pools;
- /**
- * @var array
- */
- protected $config = [
- 'host' => 'localhost',
- 'port' => 3306,
- 'database' => 'test',
- 'username' => 'root',
- 'password' => 'root',
- 'charset' => 'utf8mb4',
- 'unixSocket' => null,
- 'options' => [],
- 'size' => 64,
- ];
- private static $instance;
- private function __construct(array $config)
- {
- if (empty($this->pools)) {
- $this->config = array_replace_recursive($this->config, $config);
- $this->pools = new PDOPool(
- (new PDOConfig())
- ->withHost($this->config['host'])
- ->withPort($this->config['port'])
- ->withUnixSocket($this->config['unixSocket'])
- ->withDbName($this->config['database'])
- ->withCharset($this->config['charset'])
- ->withUsername($this->config['username'])
- ->withPassword($this->config['password'])
- ->withOptions($this->config['options']),
- $this->config['size']
- );
- }
- }
- public static function getInstance($config = null)
- {
- if (empty(self::$instance)) {
- if (empty($config)) {
- throw new RuntimeException('pdo config empty');
- }
- if (empty($config['size'])) {
- throw new RuntimeException('the size of database connection pools cannot be empty');
- }
- self::$instance = new static($config);
- }
- return self::$instance;
- }
- public function getConnection()
- {
- return $this->pools->get();
- }
- public function close($connection = null)
- {
- $this->pools->put($connection);
- }
- }
|