swoole-framwork/core/lib/PDOPool.php

46 lines
1.3 KiB
PHP
Raw Normal View History

2024-05-21 14:33:56 +08:00
<?php
namespace Core\lib;
2024-06-26 17:13:45 +08:00
use function DI\get;
2024-05-21 14:33:56 +08:00
class PDOPool extends DBPool
{
public function __construct($min = 5, $max = 10)
{
global $GLOBAL_CONFIGS;
$poolconfig = $GLOBAL_CONFIGS["dbpool"]["default"];
parent::__construct($poolconfig['min'], $poolconfig['max']);
//\Swoole\Runtime::enableCoroutine(true);
}
protected function newDB()
{
global $GLOBAL_CONFIGS;
$default = $GLOBAL_CONFIGS["db"]["default"];
{
$driver = $default["driver"];
$host = $default["host"];
$dbname = $default['database'];
$username = $default["username"];
$password = $default["password"];
$dsn = "$driver:host=$host;dbname=$dbname";
}
2024-06-26 17:13:45 +08:00
$options = [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => false,
];
$options1 = array(
2024-05-22 14:49:06 +08:00
\PDO::ATTR_PERSISTENT => true
);
2024-06-26 17:13:45 +08:00
try {
$pdo = new \PDO($dsn, $username, $password);
return $pdo;
}catch (\Exception $e) {
echo $e->getMessage() . PHP_EOL;
throw new \Exception('数据库链接失败');
}
2024-05-21 14:33:56 +08:00
}
}