变更
This commit is contained in:
@@ -99,15 +99,11 @@ class Connection extends Component
|
|||||||
if (!$this->pool->hasChannel($config['cds'])) {
|
if (!$this->pool->hasChannel($config['cds'])) {
|
||||||
$this->pool->initConnections($config['cds'], $config['pool']['max']);
|
$this->pool->initConnections($config['cds'], $config['pool']['max']);
|
||||||
}
|
}
|
||||||
if (Context::hasContext($config['cds'])) {
|
if (!Context::hasContext($config['cds'])) {
|
||||||
|
return Context::setContext($config['cds'], $this->pool->get($config['cds'], $this->generate($config)));
|
||||||
|
} else {
|
||||||
return Context::getContext($config['cds']);
|
return Context::getContext($config['cds']);
|
||||||
}
|
}
|
||||||
if ($this->pool->hasItem($config['cds'])) {
|
|
||||||
$connect = $this->pool->get($config['cds']);
|
|
||||||
} else {
|
|
||||||
$connect = $this->generate($config);
|
|
||||||
}
|
|
||||||
return Context::setContext($config['cds'], $connect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -115,27 +111,29 @@ class Connection extends Component
|
|||||||
* @param array $config
|
* @param array $config
|
||||||
* @return Closure
|
* @return Closure
|
||||||
*/
|
*/
|
||||||
public function generate(array $config): \PDO
|
public function generate(array $config): Closure
|
||||||
{
|
{
|
||||||
Kiri::getDi()->get(Kiri\Error\StdoutLoggerInterface::class)->alert('create database connect(' . $config['cds'] . ')');
|
return static function () use ($config) {
|
||||||
|
Kiri::getDi()->get(Kiri\Error\StdoutLoggerInterface::class)->alert('create database connect(' . $config['cds'] . ')');
|
||||||
|
|
||||||
$link = new \PDO('mysql:dbname=' . $config['dbname'] . ';host=' . $config['cds'], $config['username'], $config['password'], [
|
$link = new \PDO('mysql:dbname=' . $config['dbname'] . ';host=' . $config['cds'], $config['username'], $config['password'], [
|
||||||
\PDO::ATTR_EMULATE_PREPARES => false,
|
\PDO::ATTR_EMULATE_PREPARES => false,
|
||||||
\PDO::ATTR_CASE => \PDO::CASE_NATURAL,
|
\PDO::ATTR_CASE => \PDO::CASE_NATURAL,
|
||||||
\PDO::ATTR_PERSISTENT => true,
|
\PDO::ATTR_PERSISTENT => true,
|
||||||
\PDO::ATTR_TIMEOUT => $config['connect_timeout'],
|
\PDO::ATTR_TIMEOUT => $config['connect_timeout'],
|
||||||
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ($config['charset'] ?? 'utf8mb4')
|
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ($config['charset'] ?? 'utf8mb4')
|
||||||
]);
|
]);
|
||||||
$link->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
$link->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||||
$link->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
|
$link->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
|
||||||
$link->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_EMPTY_STRING);
|
$link->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_EMPTY_STRING);
|
||||||
foreach ($config['attributes'] as $key => $attribute) {
|
foreach ($config['attributes'] as $key => $attribute) {
|
||||||
$link->setAttribute($key, $attribute);
|
$link->setAttribute($key, $attribute);
|
||||||
}
|
}
|
||||||
if (Db::inTransactionsActive()) {
|
if (Db::inTransactionsActive()) {
|
||||||
$link->beginTransaction();
|
$link->beginTransaction();
|
||||||
}
|
}
|
||||||
return $link;
|
return $link;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -155,9 +155,13 @@ class Pool extends Component
|
|||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function get($name): mixed
|
public function get($name, $callback): mixed
|
||||||
{
|
{
|
||||||
return $this->channel($name)->pop();
|
$channel = $this->channel($name);
|
||||||
|
if (!$channel->isEmpty()) {
|
||||||
|
return $channel->pop();
|
||||||
|
}
|
||||||
|
return $callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user