This commit is contained in:
2023-04-06 22:39:31 +08:00
parent 4843abfdfa
commit 3d8c01a42c
+55 -45
View File
@@ -21,6 +21,8 @@ use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Di\ContainerInterface; use Kiri\Di\ContainerInterface;
use Kiri\Di\Context; use Kiri\Di\Context;
use Kiri\Annotation\Inject;
use Kiri\Pool\Pool;
use Kiri\Events\EventProvider; use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException; use Kiri\Exception\NotFindClassException;
@@ -54,9 +56,6 @@ class Connection extends Component
public array $pool = ['max' => 10, 'min' => 1]; public array $pool = ['max' => 10, 'min' => 1];
private PoolConnection $connection;
/** /**
* @var bool * @var bool
* enable database cache * enable database cache
@@ -84,17 +83,16 @@ class Connection extends Component
/** /**
* @param EventProvider $eventProvider * @param EventProvider $eventProvider
* @param Kiri\Di\ContainerInterface $container * @param Pool $connections
* @param ContainerInterface $container
* @param array $config * @param array $config
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
public function __construct(public EventProvider $eventProvider, public ContainerInterface $container, array $config = []) public function __construct(public EventProvider $eventProvider, public Pool $connections, public ContainerInterface $container, array $config = [])
{ {
parent::__construct($config); parent::__construct($config);
$this->connection = $this->container->get(PoolConnection::class); $this->initConnections();
} }
@@ -108,17 +106,15 @@ class Connection extends Component
$this->eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0); $this->eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
$this->eventProvider->on(Rollback::class, [$this, 'rollback'], 0); $this->eventProvider->on(Rollback::class, [$this, 'rollback'], 0);
$this->eventProvider->on(Commit::class, [$this, 'commit'], 0); $this->eventProvider->on(Commit::class, [$this, 'commit'], 0);
$this->connectPoolInstance();
} }
/** /**
* @throws Exception * @return void
*/ */
public function connectPoolInstance() public function initConnections(): void
{ {
$this->connection->initConnections([ $this->connections->initConnections($this->cds, $this->pool['max'] ?? 1, $this->gender([
'cds' => $this->cds, 'cds' => $this->cds,
'username' => $this->username, 'username' => $this->username,
'password' => $this->password, 'password' => $this->password,
@@ -127,7 +123,36 @@ class Connection extends Component
'read_timeout' => $this->read_timeout, 'read_timeout' => $this->read_timeout,
'dbname' => $this->database, 'dbname' => $this->database,
'pool' => $this->pool 'pool' => $this->pool
], $this->pool['max']); ]));
}
/**
* @param array $config
* @return \Closure
*/
public function gender(array $config): \Closure
{
return static function () use ($config) {
$options = [
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_TIMEOUT => $config['connect_timeout'],
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ($config['charset'] ?? 'utf8mb4')
];
if (!Context::inCoroutine()) {
$options[PDO::ATTR_PERSISTENT] = true;
}
$link = new PDO('mysql:dbname=' . $config['dbname'] . ';host=' . $config['cds'],
$config['username'], $config['password'], $options);
foreach ($config['attributes'] as $key => $attribute) {
$link->setAttribute($key, $attribute);
}
return $link;
};
} }
@@ -155,10 +180,11 @@ class Connection extends Component
*/ */
public function getMasterClient(): PDO public function getMasterClient(): PDO
{ {
if (($client = $this->connection->get($this->cds)) instanceof PDO) { $client = $this->connections->get($this->cds);
return $client; if ($client === false) {
throw new Exception('waite db client timeout.');
} }
throw new Exception('waite db client timeout.'); return $client;
} }
/** /**
@@ -167,7 +193,7 @@ class Connection extends Component
*/ */
public function getSlaveClient(): PDO public function getSlaveClient(): PDO
{ {
$client = $this->connection->get($this->cds); $client = $this->connections->get($this->cds);
if ($client === false) { if ($client === false) {
throw new Exception('waite db client timeout.'); throw new Exception('waite db client timeout.');
} }
@@ -180,37 +206,21 @@ class Connection extends Component
*/ */
public function beginTransaction(): static public function beginTransaction(): static
{ {
$pdo = $this->getPdo(); $pdo = Context::get($this->cds);
if ($pdo === null) {
$pdo = $this->getMasterClient();
}
$pdo->beginTransaction(); $pdo->beginTransaction();
return $this; return $this;
} }
/**
* @param bool $restore
* @return PDO
* @throws Exception
*/
public function getPdo(bool $restore = false): PDO
{
return $this->getMasterClient();
if ($restore === true) {
return Context::set($this->cds, $this->getMasterClient());
}
if (!Context::exists($this->cds)) {
return Context::set($this->cds, $this->getMasterClient());
} else {
return Context::get($this->cds);
}
}
/** /**
* @return $this|bool * @return $this|bool
* @throws Exception * @throws Exception
*/ */
public function inTransaction(): bool|static public function inTransaction(): bool|static
{ {
return $this->getPdo()->inTransaction(); return Context::get($this->cds)->inTransaction();
} }
/** /**
@@ -219,7 +229,7 @@ class Connection extends Component
*/ */
public function rollback() public function rollback()
{ {
$pdo = $this->getPdo(); $pdo = Context::get($this->cds);
if ($pdo->inTransaction()) { if ($pdo->inTransaction()) {
$pdo->rollback(); $pdo->rollback();
} }
@@ -232,7 +242,7 @@ class Connection extends Component
*/ */
public function commit() public function commit()
{ {
$pdo = $this->getPdo(); $pdo = Context::get($this->cds);
if ($pdo->inTransaction()) { if ($pdo->inTransaction()) {
$pdo->commit(); $pdo->commit();
} }
@@ -248,7 +258,7 @@ class Connection extends Component
*/ */
public function createCommand($sql = null, array $attributes = []): Command public function createCommand($sql = null, array $attributes = []): Command
{ {
$command = new Command(['db' => $this, 'pdo' => $this->getMasterClient(), 'sql' => $sql]); $command = new Command(['db' => $this, 'sql' => $sql]);
return $command->bindValues($attributes); return $command->bindValues($attributes);
} }
@@ -261,7 +271,7 @@ class Connection extends Component
public function release(PDO $PDO) public function release(PDO $PDO)
{ {
if ($PDO->inTransaction() === false) { if ($PDO->inTransaction() === false) {
$this->connection->addItem($this->cds, $PDO); $this->connections->push($this->cds, $PDO);
} }
} }
@@ -273,7 +283,7 @@ class Connection extends Component
*/ */
public function clear_connection() public function clear_connection()
{ {
$this->connection->connection_clear($this->cds); $this->connections->clean($this->cds);
} }
@@ -282,7 +292,7 @@ class Connection extends Component
*/ */
public function disconnect() public function disconnect()
{ {
$this->connection->connection_clear($this->cds); $this->connections->clean($this->cds);
} }
} }