Skip to content

Commit

Permalink
Update DB.php
Browse files Browse the repository at this point in the history
  • Loading branch information
GXhua authored May 20, 2021
1 parent 8a1f883 commit 5bc9520
Showing 1 changed file with 70 additions and 25 deletions.
95 changes: 70 additions & 25 deletions src/DB.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?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;
Expand All @@ -17,16 +18,16 @@

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 @@ -36,7 +37,13 @@ public function __construct($config = null)
public function quote(string $string, int $parameter_type = PDO::PARAM_STR)
{
$this->realGetConn();
$ret = $this->pdo->quote($string, $parameter_type);
try {
$ret = $this->pdo->quote($string, $parameter_type);
} catch (\Exception $exc) {
$this->release($this->pdo);
throw $exc;
}

$this->release($this->pdo);
return $ret;
}
Expand All @@ -47,7 +54,12 @@ public function beginTransaction(): void
throw new RuntimeException('do not support nested transaction now');
}
$this->realGetConn();
$this->pdo->beginTransaction();
try {
$this->pdo->beginTransaction();
} catch (\Exception $exc) {
$this->release($this->pdo);
throw $exc;
}
$this->in_transaction = true;
Coroutine::defer(function () {
if ($this->in_transaction) {
Expand All @@ -58,29 +70,46 @@ public function beginTransaction(): void

public function commit(): void
{
$this->pdo->commit();
$this->in_transaction = false;
try {
$this->pdo->commit();
} catch (\Exception $exc) {
$this->release($this->pdo);
throw $exc;
}
$this->release($this->pdo);
}

public function rollBack(): void
{
$this->pdo->rollBack();
$this->in_transaction = false;

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

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

public function query(string $query, array $bindings = []): array
{
$this->realGetConn();
try {

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

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

$statement->execute();
$statement->execute();

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

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

Expand All @@ -97,14 +126,19 @@ public function fetch(string $query, array $bindings = [])
public function execute(string $query, array $bindings = []): int
{
$this->realGetConn();
try {

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

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

$statement->execute();
$statement->execute();

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

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

Expand All @@ -114,8 +148,14 @@ public function execute(string $query, array $bindings = []): int
public function exec(string $sql): int
{
$this->realGetConn();
try {

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

$ret = $this->pdo->exec($sql);

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

Expand All @@ -126,13 +166,19 @@ public function insert(string $query, array $bindings = []): int
{
$this->realGetConn();

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

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

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

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

$ret = (int) $this->pdo->lastInsertId();

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

Expand All @@ -145,7 +191,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 @@ -157,17 +203,16 @@ 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 5bc9520

Please sign in to comment.