From f6d4b0fbcabb85fde00fec84666762a028f45a2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Sun, 16 Apr 2023 01:45:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Annotation/Get.php | 4 +- Annotation/Relation.php | 3 +- Annotation/Set.php | 4 +- BackupCommand.php | 3 +- Command.php | 14 +- Connection.php | 8 - DatabasesProviders.php | 15 +- Mysql/PDO.php | 386 ---------------------------------------- SqlBuilder.php | 1 + Traits/Builder.php | 1 + Traits/Relation.php | 1 + Traits/When.php | 1 + 12 files changed, 24 insertions(+), 417 deletions(-) delete mode 100644 Mysql/PDO.php diff --git a/Annotation/Get.php b/Annotation/Get.php index d7fcb0f..f085a30 100644 --- a/Annotation/Get.php +++ b/Annotation/Get.php @@ -5,8 +5,6 @@ namespace Database\Annotation; use Attribute; -use Database\Base\Getter; -use Kiri\Annotation\AbstractAttribute; /** @@ -14,7 +12,7 @@ use Kiri\Annotation\AbstractAttribute; * @package Annotation\Model * @deprecated */ -#[Attribute(Attribute::TARGET_METHOD)] class Get extends AbstractAttribute +#[Attribute(Attribute::TARGET_METHOD)] class Get { diff --git a/Annotation/Relation.php b/Annotation/Relation.php index a4c2316..02a581c 100644 --- a/Annotation/Relation.php +++ b/Annotation/Relation.php @@ -4,7 +4,6 @@ namespace Database\Annotation; -use Kiri\Annotation\AbstractAttribute; use Exception; @@ -12,7 +11,7 @@ use Exception; * Class Relation * @package Annotation\Model */ -#[\Attribute(\Attribute::TARGET_METHOD)] class Relation extends AbstractAttribute +#[\Attribute(\Attribute::TARGET_METHOD)] class Relation { diff --git a/Annotation/Set.php b/Annotation/Set.php index e6e55fc..8389829 100644 --- a/Annotation/Set.php +++ b/Annotation/Set.php @@ -4,14 +4,12 @@ namespace Database\Annotation; -use Database\Base\Setter; -use Kiri\Annotation\AbstractAttribute; /** * @deprecated */ -#[\Attribute(\Attribute::TARGET_METHOD)] class Set extends AbstractAttribute +#[\Attribute(\Attribute::TARGET_METHOD)] class Set { diff --git a/BackupCommand.php b/BackupCommand.php index 8e1064c..6f007ac 100644 --- a/BackupCommand.php +++ b/BackupCommand.php @@ -1,13 +1,12 @@ eventProvider->on(OnWorkerExit::class, [$this, 'clear_connection'], 9999); $this->eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0); $this->eventProvider->on(Rollback::class, [$this, 'rollback'], 0); $this->eventProvider->on(Commit::class, [$this, 'commit'], 0); diff --git a/DatabasesProviders.php b/DatabasesProviders.php index 0c0af2c..07c141c 100644 --- a/DatabasesProviders.php +++ b/DatabasesProviders.php @@ -9,15 +9,12 @@ use Kiri; use Kiri\Abstracts\Config; use Kiri\Abstracts\Providers; use Kiri\Pool\Connection as PoolConnection; -use Kiri\Exception\ConfigException; use Kiri\Events\EventProvider; -use Kiri\Annotation\Inject; -use Kiri\Server\Events\OnWorkerStart; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; -use Kiri\Server\Events\OnWorkerExit; use Swoole\Timer; use Kiri\Di\LocalService; +use Kiri\Di\Inject\Container; /** * Class DatabasesProviders @@ -29,7 +26,7 @@ class DatabasesProviders extends Providers /** * @var EventProvider */ - #[Inject(EventProvider::class)] + #[Container(EventProvider::class)] public EventProvider $provider; @@ -49,15 +46,13 @@ class DatabasesProviders extends Providers if (empty($databases)) { return; } - $this->provider->on(OnWorkerExit::class, [$this, 'exit'], 9999); - $this->provider->on(OnWorkerStart::class, [$this, 'start']); foreach ($databases as $key => $database) { $application->set($key, $this->_settings($database)); } } - public function start(OnWorkerStart $start) + public function start() { if (!Kiri\Di\Context::inCoroutine()) { return; @@ -81,12 +76,10 @@ class DatabasesProviders extends Providers /** - * @param OnWorkerExit $exit * @return void - * @throws ConfigException * @throws Exception */ - public function exit(OnWorkerExit $exit): void + public function exit(): void { Timer::clearAll(); $databases = Config::get('databases.connections', []); diff --git a/Mysql/PDO.php b/Mysql/PDO.php deleted file mode 100644 index 713e649..0000000 --- a/Mysql/PDO.php +++ /dev/null @@ -1,386 +0,0 @@ -dbname = $config['dbname']; - $this->cds = $config['cds']; - $this->username = $config['username']; - $this->password = $config['password']; - $this->connect_timeout = $config['connect_timeout'] ?? 30; - $this->read_timeout = $config['read_timeout'] ?? 10; - $this->charset = $config['charset'] ?? 'utf8mb4'; - $this->attributes = $config['attributes'] ?? []; - - $this->group = new WaitGroup(); - } - - - /** - * @return void - * @throws Exception - */ - public function init(): void - { - $eventProvider = Kiri::getDi()->get(EventProvider::class); - $eventProvider->on(OnWorkerExit::class, [$this, 'onWorkerExit']); - $this->_timerId = Timer::tick(60000, [$this, 'check']); - } - - - /** - * @return bool - */ - public function inTransaction(): bool - { - return $this->_transaction > 0; - } - - - /** - * @param Kiri\Server\Events\OnWorkerExit $exit - * @return void - */ - public function onWorkerExit(OnWorkerExit $exit): void - { - $this->stopHeartbeatCheck(); - } - - - /** - * @param string $name - * @param array $arguments - * @return mixed - */ - public function __call(string $name, array $arguments) - { - return $this->_pdo()->{$name}(...$arguments); - } - - - /** - * @param string $sql - * @return PDOStatement|bool - */ - public function prepare(string $sql): PDOStatement|bool - { - return $this->_pdo()->prepare($sql); - } - - - /** - * - */ - public function stopHeartbeatCheck(): void - { - $this->pdo = null; - if ($this->_timerId > -1) { - Timer::clear($this->_timerId); - $this->_timerId = -1; - } - } - - - /** - * - */ - public function beginTransaction() - { - if ($this->_transaction == 0) { - $this->_pdo()->beginTransaction(); - } - $this->_transaction++; - } - - - /** - * - */ - public function commit() - { - $this->_transaction--; - if ($this->_transaction == 0) { - $this->_pdo()->commit(); - } - } - - - /** - * - */ - public function rollback() - { - $this->_transaction--; - if ($this->_transaction == 0) { - $this->_pdo()->rollBack(); - } - } - - - /** - * @param string $sql - * @param array $params - * @return bool|array|null - * @throws Exception - */ - public function fetchAll(string $sql, array $params = []): bool|null|array - { - $pdo = $this->queryPrev($sql, $params); - - // $pdo->closeCursor(); - return $pdo->fetchAll(\PDO::FETCH_ASSOC); - } - - - /** - * @param string $sql - * @param array $params - * @return bool|array|null - * @throws Exception - */ - public function fetch(string $sql, array $params = []): bool|null|array - { - $pdo = $this->queryPrev($sql, $params); - - // $pdo->closeCursor(); - return $pdo->fetch(\PDO::FETCH_ASSOC); - } - - - /** - * @param string $sql - * @param array $params - * @return bool|array|null - * @throws \Exception - */ - public function fetchColumn(string $sql, array $params = []): bool|null|array - { - $pdo = $this->queryPrev($sql, $params); - - // $pdo->closeCursor(); - return $pdo->fetchColumn(\PDO::FETCH_ASSOC); - } - - - /** - * @param string $sql - * @param array $params - * @return int - * @throws Exception - */ - public function rowCount(string $sql, array $params = []): int - { - $pdo = $this->queryPrev($sql, $params); - - // $pdo->closeCursor(); - return $pdo->rowCount(); - } - - - /** - * @param string $sql - * @param array $params - * @return PDOStatement - * @throws Exception - */ - private function queryPrev(string $sql, array $params = []): PDOStatement - { - try { -// if ($this->_timerId === -1) { -// $this->_timerId = Timer::tick(6000, [$this, 'check']); -// } -// $this->_last = time(); - if (($statement = $this->_pdo()->query($sql, \PDO::FETCH_ASSOC)) === false) { - throw new Exception($this->_pdo()->errorInfo()[1]); - } - return $this->bindValue($statement, $params); - } catch (\PDOException|\Throwable $throwable) { - if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { - $this->pdo = null; - - return $this->queryPrev($sql, $params); - } - throw new Exception($throwable->getMessage()); - } - } - - - /** - * @return bool - */ - public function check(): bool - { - return true; -// try { -// if ($this->_last == 0) $this->_last = time(); -// if (time() - $this->_last >= 600) { -// return $result = false; -// } else if (!($this->pdo instanceof \PDO)) { -// return $result = false; -// } -// $this->_pdo()->getAttribute(\PDO::ATTR_SERVER_INFO); -// $result = true; -// } catch (\Throwable $throwable) { -// if (!str_contains($throwable->getMessage(), 'Idle dis')) { -// Kiri::getLogger()->error($throwable->getMessage()); -// } -// $result = false; -// } finally { -// return $this->afterCheck($result); -// } - } - - - /** - * @param bool $result - * @return bool - */ - private function afterCheck(bool $result): bool - { - $container = Kiri::getDi()->get(WorkerStatus::class); - if (!$result || $container->is(StatusEnum::EXIT)) { - $this->pdo = null; -// $result = Timer::clear($this->_timerId); -// $this->_timerId = -1; - } - return $result; - } - - - /** - * @param PDOStatement $statement - * @param array $params - * @return PDOStatement - */ - private function bindValue(PDOStatement $statement, array $params = []): PDOStatement - { -// if (empty($params)) return $statement; -// foreach ($params as $key => $param) { -// $statement->bindValue($key, $param); -// } - $statement->execute($params); - return $statement; - } - - - /** - * @param string $sql - * @param array $params - * @return int - * @throws Exception - */ - public function execute(string $sql, array $params = []): int - { - $pdo = $this->_pdo(); -// if ($this->_timerId === -1) { -// $this->_timerId = Timer::tick(6000, [$this, 'check']); -// } -// $this->_last = time(); - if (!(($prepare = $pdo->prepare($sql)) instanceof PDOStatement)) { - throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE); - } - if ($prepare->execute($params) === false) { - throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE); - } - - $result = (int)$pdo->lastInsertId(); - $prepare->closeCursor(); - - return $result == 0 ? true : $result; - } - - - /** - * @return array - */ - public function errorInfo(): array - { - return $this->pdo->errorInfo(); - } - - - /** - * @return \PDO - */ - public function _pdo(): \PDO - { - if (!($this->pdo instanceof \PDO)) { - $this->newClient(); - } - return $this->pdo; - } - - - /** - * @return void - */ - private function newClient(): void - { - $link = new \PDO('mysql:dbname=' . $this->dbname . ';host=' . $this->cds, $this->username, $this->password, [ - \PDO::ATTR_EMULATE_PREPARES => false, - \PDO::ATTR_CASE => \PDO::CASE_NATURAL, - \PDO::ATTR_PERSISTENT => true, - \PDO::ATTR_TIMEOUT => $this->connect_timeout, - \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset - ]); - $link->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); - $link->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false); - $link->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_EMPTY_STRING); - foreach ($this->attributes as $key => $attribute) { - $link->setAttribute($key, $attribute); - } - if (Db::inTransactionsActive()) { - $link->beginTransaction(); - } - $this->pdo = $link; - } - -} diff --git a/SqlBuilder.php b/SqlBuilder.php index 0bc440a..17ebe96 100644 --- a/SqlBuilder.php +++ b/SqlBuilder.php @@ -1,5 +1,6 @@