qqq
This commit is contained in:
+53
-46
@@ -61,6 +61,9 @@ class Connection extends Component
|
|||||||
private ?PDO $_pdo = null;
|
private ?PDO $_pdo = null;
|
||||||
|
|
||||||
|
|
||||||
|
private Pool $connections;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
@@ -82,33 +85,12 @@ class Connection extends Component
|
|||||||
*/
|
*/
|
||||||
public function init(): void
|
public function init(): void
|
||||||
{
|
{
|
||||||
|
|
||||||
$eventProvider = Kiri::getDi()->get(EventProvider::class);
|
$eventProvider = Kiri::getDi()->get(EventProvider::class);
|
||||||
$eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
|
$eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
|
||||||
$eventProvider->on(Rollback::class, [$this, 'rollback'], 0);
|
$eventProvider->on(Rollback::class, [$this, 'rollback'], 0);
|
||||||
$eventProvider->on(Commit::class, [$this, 'commit'], 0);
|
$eventProvider->on(Commit::class, [$this, 'commit'], 0);
|
||||||
|
|
||||||
$this->initConnections();
|
$this->connections = Kiri::getDi()->get(Pool::class);
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
* @throws ReflectionException
|
|
||||||
*/
|
|
||||||
public function initConnections(): void
|
|
||||||
{
|
|
||||||
$connections = Kiri::getDi()->get(Pool::class);
|
|
||||||
$connections->initConnections($this->cds, $this->pool['max'] ?? 1, $this->gender([
|
|
||||||
'cds' => $this->cds,
|
|
||||||
'username' => $this->username,
|
|
||||||
'password' => $this->password,
|
|
||||||
'attributes' => $this->attributes,
|
|
||||||
'connect_timeout' => $this->connect_timeout,
|
|
||||||
'read_timeout' => $this->read_timeout,
|
|
||||||
'dbname' => $this->database,
|
|
||||||
'pool' => $this->pool
|
|
||||||
]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -165,8 +147,7 @@ class Connection extends Component
|
|||||||
*/
|
*/
|
||||||
public function getConnection(): PDO
|
public function getConnection(): PDO
|
||||||
{
|
{
|
||||||
$connections = Kiri::getDi()->get(Pool::class);
|
return $this->pool()->get($this->cds);
|
||||||
return $connections->get($this->cds);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -176,10 +157,13 @@ class Connection extends Component
|
|||||||
*/
|
*/
|
||||||
public function beginTransaction(): static
|
public function beginTransaction(): static
|
||||||
{
|
{
|
||||||
$pdo = $this->getTransactionClient();
|
|
||||||
if ($this->storey == 0) {
|
if ($this->storey == 0) {
|
||||||
|
/** @var PDO $pdo */
|
||||||
|
$pdo = Context::get($this->cds);
|
||||||
|
if ($pdo !== null && !$pdo->inTransaction()) {
|
||||||
$pdo->beginTransaction();
|
$pdo->beginTransaction();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
$this->storey++;
|
$this->storey++;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -191,25 +175,24 @@ class Connection extends Component
|
|||||||
*/
|
*/
|
||||||
public function getTransactionClient(): PDO
|
public function getTransactionClient(): PDO
|
||||||
{
|
{
|
||||||
if (!Db::inTransactionsActive()) {
|
|
||||||
return $this->getConnection();
|
|
||||||
}
|
|
||||||
$pdo = Context::get($this->cds);
|
$pdo = Context::get($this->cds);
|
||||||
if ($pdo === null) {
|
if ($pdo === null) {
|
||||||
/** @var PDO $pdo */
|
$pdo = $this->getConnection();
|
||||||
$pdo = Context::set($this->cds, $this->getConnection());
|
if ($this->storey > 0 && !$pdo->inTransaction()) {
|
||||||
|
$pdo->beginTransaction();
|
||||||
|
}
|
||||||
|
Context::set($this->cds, $pdo);
|
||||||
}
|
}
|
||||||
return $pdo;
|
return $pdo;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return $this|bool
|
* @return bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function inTransaction(): bool|static
|
public function inTransaction(): bool
|
||||||
{
|
{
|
||||||
$pdo = $this->getTransactionClient();
|
return $this->storey > 0;
|
||||||
return $pdo->inTransaction();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -220,12 +203,14 @@ class Connection extends Component
|
|||||||
{
|
{
|
||||||
$this->storey--;
|
$this->storey--;
|
||||||
if ($this->storey == 0) {
|
if ($this->storey == 0) {
|
||||||
$pdo = $this->getTransactionClient();
|
$pdo = Context::get($this->cds);
|
||||||
|
if ($pdo === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if ($pdo->inTransaction()) {
|
if ($pdo->inTransaction()) {
|
||||||
$pdo->rollback();
|
$pdo->rollback();
|
||||||
}
|
}
|
||||||
$connections = Kiri::getDi()->get(Pool::class);
|
$this->pool()->push($this->cds, $pdo);
|
||||||
$connections->push($this->cds, $pdo);
|
|
||||||
Context::remove($this->cds);
|
Context::remove($this->cds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -238,12 +223,14 @@ class Connection extends Component
|
|||||||
{
|
{
|
||||||
$this->storey--;
|
$this->storey--;
|
||||||
if ($this->storey == 0) {
|
if ($this->storey == 0) {
|
||||||
$pdo = $this->getTransactionClient();
|
$pdo = Context::get($this->cds);
|
||||||
|
if ($pdo === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if ($pdo->inTransaction()) {
|
if ($pdo->inTransaction()) {
|
||||||
$pdo->commit();
|
$pdo->commit();
|
||||||
}
|
}
|
||||||
$connections = Kiri::getDi()->get(Pool::class);
|
$this->pool()->push($this->cds, $pdo);
|
||||||
$connections->push($this->cds, $pdo);
|
|
||||||
Context::remove($this->cds);
|
Context::remove($this->cds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -269,8 +256,7 @@ class Connection extends Component
|
|||||||
*/
|
*/
|
||||||
public function release(PDO $PDO): void
|
public function release(PDO $PDO): void
|
||||||
{
|
{
|
||||||
$connections = Kiri::getDi()->get(Pool::class);
|
$this->pool()->push($this->cds, $PDO);
|
||||||
$connections->push($this->cds, $PDO);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -281,8 +267,7 @@ class Connection extends Component
|
|||||||
*/
|
*/
|
||||||
public function clear_connection(): void
|
public function clear_connection(): void
|
||||||
{
|
{
|
||||||
$connections = Kiri::getDi()->get(Pool::class);
|
$this->pool()->clean($this->cds);
|
||||||
$connections->clean($this->cds);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -291,8 +276,30 @@ class Connection extends Component
|
|||||||
*/
|
*/
|
||||||
public function disconnect(): void
|
public function disconnect(): void
|
||||||
{
|
{
|
||||||
$connections = Kiri::getDi()->get(Pool::class);
|
$this->pool()->clean($this->cds);
|
||||||
$connections->clean($this->cds);
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Pool
|
||||||
|
*/
|
||||||
|
private function pool(): Pool
|
||||||
|
{
|
||||||
|
if (!$this->connections->hasChannel($this->cds)) {
|
||||||
|
$params = [
|
||||||
|
'cds' => $this->cds,
|
||||||
|
'username' => $this->username,
|
||||||
|
'password' => $this->password,
|
||||||
|
'attributes' => $this->attributes,
|
||||||
|
'connect_timeout' => $this->connect_timeout,
|
||||||
|
'read_timeout' => $this->read_timeout,
|
||||||
|
'dbname' => $this->database,
|
||||||
|
'pool' => $this->pool
|
||||||
|
];
|
||||||
|
$itemCount = $this->pool['max'] ?? 1;
|
||||||
|
$this->connections->created($this->cds, $itemCount, $this->gender($params));
|
||||||
|
}
|
||||||
|
return $this->connections;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-40
@@ -7,8 +7,6 @@ namespace Database;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use Kiri;
|
use Kiri;
|
||||||
use Kiri\Abstracts\Providers;
|
use Kiri\Abstracts\Providers;
|
||||||
use Kiri\Config\ConfigProvider;
|
|
||||||
use Swoole\Timer;
|
|
||||||
use Kiri\Di\LocalService;
|
use Kiri\Di\LocalService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -22,7 +20,7 @@ class DatabasesProviders extends Providers
|
|||||||
/**
|
/**
|
||||||
* @param LocalService $application
|
* @param LocalService $application
|
||||||
* @return void
|
* @return void
|
||||||
* @throws \ReflectionException
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onImport(LocalService $application): void
|
public function onImport(LocalService $application): void
|
||||||
{
|
{
|
||||||
@@ -34,43 +32,7 @@ class DatabasesProviders extends Providers
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
foreach ($databases as $key => $database) {
|
foreach ($databases as $key => $database) {
|
||||||
$application->set('db.' . $key, $this->_settings($database));
|
$application->set('db.' . $key, Kiri::createObject($this->_settings($database)));
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public function start(): void
|
|
||||||
{
|
|
||||||
if (!Kiri\Di\Context::inCoroutine()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Timer::tick(60000, function () {
|
|
||||||
$databases = \config('databases.connections', []);
|
|
||||||
if (empty($databases)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$connection = Kiri::getDi()->get(Kiri\Pool\Pool::class);
|
|
||||||
foreach ($databases as $database) {
|
|
||||||
$connection->flush($database['cds'], $database['pool']['min'] ?? 1);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function exit(): void
|
|
||||||
{
|
|
||||||
Timer::clearAll();
|
|
||||||
$databases = \config('databases.connections', []);
|
|
||||||
if (!empty($databases)) {
|
|
||||||
$connection = Kiri::getDi()->get(Kiri\Pool\Pool::class);
|
|
||||||
foreach ($databases as $database) {
|
|
||||||
$connection->clean($database['cds']);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,16 +10,14 @@ declare(strict_types=1);
|
|||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
|
use Database\Affair\BeginTransaction;
|
||||||
use Database\Affair\Commit;
|
use Database\Affair\Commit;
|
||||||
use Database\Affair\Rollback;
|
use Database\Affair\Rollback;
|
||||||
use Database\Traits\QueryTrait;
|
use Database\Traits\QueryTrait;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Di\Context;
|
|
||||||
use Kiri\Events\EventDispatch;
|
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
use ReflectionException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Db
|
* Class Db
|
||||||
@@ -32,21 +30,12 @@ class Db implements ISqlBuilder
|
|||||||
|
|
||||||
private static bool $_inTransaction = false;
|
private static bool $_inTransaction = false;
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public static function inTransactionsActive(): bool
|
|
||||||
{
|
|
||||||
return Context::exists('transactions::status') && Context::get('transactions::status') === true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function beginTransaction(): void
|
public static function beginTransaction(): void
|
||||||
{
|
{
|
||||||
Context::set('transactions::status', true);
|
fire(new BeginTransaction());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -78,27 +67,20 @@ class Db implements ISqlBuilder
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ContainerExceptionInterface
|
* @return void
|
||||||
* @throws NotFoundExceptionInterface
|
|
||||||
*/
|
*/
|
||||||
public static function commit(): void
|
public static function commit(): void
|
||||||
{
|
{
|
||||||
$event = \Kiri::getDi()->get(EventDispatch::class);
|
fire(new Commit());
|
||||||
$event->dispatch(new Commit());
|
|
||||||
Context::remove('transactions::status');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
* @throws ContainerExceptionInterface
|
|
||||||
* @throws NotFoundExceptionInterface
|
|
||||||
*/
|
*/
|
||||||
public static function rollback(): void
|
public static function rollback(): void
|
||||||
{
|
{
|
||||||
$event = \Kiri::getDi()->get(EventDispatch::class);
|
fire(new Rollback());
|
||||||
$event->dispatch(new Rollback());
|
|
||||||
Context::remove('transactions::status');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -173,10 +155,10 @@ class Db implements ISqlBuilder
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Connection|null $connection
|
* @param Connection|null $connection
|
||||||
* @return mixed
|
* @return array|bool|null
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function find(Connection $connection = NULL): mixed
|
public function find(Connection $connection = NULL): array|bool|null
|
||||||
{
|
{
|
||||||
$connection = static::getDefaultConnection($connection);
|
$connection = static::getDefaultConnection($connection);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user