Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records committed May 20, 2021
1 parent 5bc9520 commit b083647
Showing 1 changed file with 33 additions and 37 deletions.
70 changes: 33 additions & 37 deletions src/DB.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);
/**
* This file is part of Simps.
*
* @link https://simps.io
* @document https://doc.simps.io
* @license https://github.com/simple-swoole/simps/blob/master/LICENSE
*/

namespace Simps\DB;

use PDO;
use RuntimeException;
use Swoole\Coroutine;
use Swoole\Database\PDOStatementProxy;
use Throwable;

class DB
{

protected $pool;

/** @var PDO */
protected $pdo;

private $in_transaction = false;

public function __construct($config = null)
{
if (!empty($config)) {
if (! empty($config)) {
$this->pool = \Simps\DB\PDO::getInstance($config);
} else {
$this->pool = \Simps\DB\PDO::getInstance();
Expand All @@ -39,9 +39,9 @@ public function quote(string $string, int $parameter_type = PDO::PARAM_STR)
$this->realGetConn();
try {
$ret = $this->pdo->quote($string, $parameter_type);
} catch (\Exception $exc) {
$this->release($this->pdo);
throw $exc;
} catch (Throwable $th) {
$this->release();
throw $th;
}

$this->release($this->pdo);
Expand All @@ -56,9 +56,9 @@ public function beginTransaction(): void
$this->realGetConn();
try {
$this->pdo->beginTransaction();
} catch (\Exception $exc) {
$this->release($this->pdo);
throw $exc;
} catch (Throwable $th) {
$this->release();
throw $th;
}
$this->in_transaction = true;
Coroutine::defer(function () {
Expand All @@ -73,9 +73,9 @@ public function commit(): void
$this->in_transaction = false;
try {
$this->pdo->commit();
} catch (\Exception $exc) {
$this->release($this->pdo);
throw $exc;
} catch (Throwable $th) {
$this->release();
throw $th;
}
$this->release($this->pdo);
}
Expand All @@ -86,9 +86,9 @@ public function rollBack(): void

try {
$this->pdo->rollBack();
} catch (\Exception $exc) {
$this->release($this->pdo);
throw $exc;
} catch (Throwable $th) {
$this->release();
throw $th;
}

$this->release($this->pdo);
Expand All @@ -98,17 +98,16 @@ public function query(string $query, array $bindings = []): array
{
$this->realGetConn();
try {

$statement = $this->pdo->prepare($query);

$this->bindValues($statement, $bindings);

$statement->execute();

$ret = $statement->fetchAll();
} catch (\Exception $exc) {
$this->release($this->pdo);
throw $exc;
} catch (Throwable $th) {
$this->release();
throw $th;
}

$this->release($this->pdo);
Expand All @@ -127,17 +126,16 @@ public function execute(string $query, array $bindings = []): int
{
$this->realGetConn();
try {

$statement = $this->pdo->prepare($query);

$this->bindValues($statement, $bindings);

$statement->execute();

$ret = $statement->rowCount();
} catch (\Exception $exc) {
$this->release($this->pdo);
throw $exc;
} catch (Throwable $th) {
$this->release();
throw $th;
}

$this->release($this->pdo);
Expand All @@ -149,14 +147,12 @@ public function exec(string $sql): int
{
$this->realGetConn();
try {

$ret = $this->pdo->exec($sql);
} catch (\Exception $exc) {
$this->release($this->pdo);
throw $exc;
} catch (Throwable $th) {
$this->release();
throw $th;
}


$this->release($this->pdo);

return $ret;
Expand All @@ -174,12 +170,11 @@ public function insert(string $query, array $bindings = []): int
$statement->execute();

$ret = (int) $this->pdo->lastInsertId();
} catch (\Exception $exc) {
$this->release($this->pdo);
throw $exc;
} catch (Throwable $th) {
$this->release();
throw $th;
}


$this->release($this->pdo);

return $ret;
Expand All @@ -191,7 +186,7 @@ public function release($connection = null)
$this->in_transaction = false;
}

if (!$this->in_transaction) {
if (! $this->in_transaction) {
$this->pool->close($connection);
return true;
}
Expand All @@ -203,16 +198,17 @@ protected function bindValues(PDOStatementProxy $statement, array $bindings): vo
{
foreach ($bindings as $key => $value) {
$statement->bindValue(
is_string($key) ? $key : $key + 1, $value, is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR
is_string($key) ? $key : $key + 1,
$value,
is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR
);
}
}

private function realGetConn()
{
if (!$this->in_transaction) {
if (! $this->in_transaction) {
$this->pdo = $this->pool->getConnection();
}
}

}

0 comments on commit b083647

Please sign in to comment.