Format code.

This commit is contained in:
李铭昕 2019-11-02 14:29:10 +08:00
parent 8e32291fcd
commit c387bb8e12
14 changed files with 52 additions and 62 deletions

View File

@ -131,6 +131,7 @@
"Hyperf\\Consul\\": "src/consul/src/",
"Hyperf\\Contract\\": "src/contract/src/",
"Hyperf\\Crontab\\": "src/crontab/src/",
"Hyperf\\DB\\": "src/db/src/",
"Hyperf\\Database\\": "src/database/src/",
"Hyperf\\DbConnection\\": "src/db-connection/src/",
"Hyperf\\Devtool\\": "src/devtool/src/",
@ -197,6 +198,7 @@
"HyperfTest\\Constants\\": "src/constants/tests/",
"HyperfTest\\Consul\\": "src/consul/tests/",
"HyperfTest\\Crontab\\": "src/crontab/tests/",
"HyperfTest\\DB\\": "src/db/tests/",
"HyperfTest\\Database\\": "src/database/tests/",
"HyperfTest\\DbConnection\\": "src/db-connection/tests/",
"HyperfTest\\Di\\": "src/di/tests/",
@ -252,6 +254,7 @@
"Hyperf\\Constants\\ConfigProvider",
"Hyperf\\Consul\\ConfigProvider",
"Hyperf\\Crontab\\ConfigProvider",
"Hyperf\\DB\\ConfigProvider",
"Hyperf\\DbConnection\\ConfigProvider",
"Hyperf\\Devtool\\ConfigProvider",
"Hyperf\\Di\\ConfigProvider",

View File

@ -14,15 +14,16 @@
},
"autoload-dev": {
"psr-4": {
"HyperfTest\\": "tests"
"HyperfTest\\DB\\": "tests/"
}
},
"require": {
"php": ">=7.2",
"ext-swoole": ">=4.4",
"hyperf/config": "^1.1",
"hyperf/di": "1.1.*",
"hyperf/framework": "1.1.*",
"hyperf/config": "~1.1.0",
"hyperf/contract": "~1.1.0",
"hyperf/di": "~1.1.0",
"hyperf/framework": "~1.1.0",
"hyperf/pool": "~1.1.0",
"hyperf/utils": "~1.1.0",
"psr/container": "^1.0"

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
@ -31,6 +32,4 @@ abstract class AbstractConnection extends BaseConnection implements ConnectionIn
abstract public function prepare(string $sql, array $data = [], array $options = []): bool;
abstract public function query(string $sql): ?array;
}
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
@ -11,12 +12,11 @@ declare(strict_types=1);
namespace Hyperf\DB;
use Hyperf\DB\Pool\PoolFactory;
use Hyperf\Utils\Context;
/**
* Class DB
* Class DB.
* @method beginTransaction()
* @method commit()
* @method rollback()
@ -49,19 +49,16 @@ class DB
$count = 0;
if (Context::has('transactionConnection')) {
$connection = Context::get('transactionConnection');
} else {
$hasContextConnection = Context::has($this->getContextKey());
$connection = $this->getConnection($hasContextConnection);
}
if ($name == 'beginTransaction') {
Context::set('transactionConnection', $connection);
$count = $this->addTransactionConnectionCount();
}
if ($name == 'commit' || $name == 'rollback') {
$count = $this->cutTransactionConnectionCount();
if ($count == 0) {
@ -77,7 +74,6 @@ class DB
return $result;
}
/**
* Get a connection from coroutine context, or from mysql connectio pool.
* @param mixed $hasContextConnection
@ -88,7 +84,7 @@ class DB
if ($hasContextConnection) {
$connection = Context::get($this->getContextKey());
}
if (!$connection instanceof AbstractConnection) {
if (! $connection instanceof AbstractConnection) {
$pool = $this->factory->getPool($this->poolName);
$connection = $pool->get()->getConnection();
}
@ -116,4 +112,4 @@ class DB
Context::set('transactionConnectionCount', $count);
return $count;
}
}
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
@ -11,13 +12,12 @@ declare(strict_types=1);
namespace Hyperf\DB\Exception;
use Throwable;
class QueryException extends \PDOException
{
public function __construct(string $message = "", int $code = 0, Throwable $previous = null)
public function __construct(string $message = '', int $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
@ -18,7 +19,6 @@ use Psr\Container\ContainerInterface;
class PDOConnection extends AbstractConnection
{
/**
* @var PDO
*/
@ -71,7 +71,7 @@ class PDOConnection extends AbstractConnection
return $this;
}
if (!$this->reconnect()) {
if (! $this->reconnect()) {
throw new ConnectionException('Connection reconnect failed.');
}
@ -88,7 +88,7 @@ class PDOConnection extends AbstractConnection
$dbName = $this->config['database'];
$username = $this->config['username'];
$password = $this->config['password'];
$dsn = "$dbms:host=$host;dbname=$dbName";
$dsn = "{$dbms}:host={$host};dbname={$dbName}";
try {
$pdo = new \PDO($dsn, $username, $password, [PDO::ATTR_PERSISTENT => true]);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
@ -153,4 +153,4 @@ class PDOConnection extends AbstractConnection
{
return $this->connection->query($sql)->fetchAll();
}
}
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
@ -11,10 +12,8 @@ declare(strict_types=1);
namespace Hyperf\DB\Pool;
use Hyperf\Pool\Pool;
abstract class AbstractPool extends Pool
{
}
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
@ -11,7 +12,6 @@ declare(strict_types=1);
namespace Hyperf\DB\Pool;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\ConnectionInterface;
use Hyperf\DB\Frequency;
@ -21,7 +21,6 @@ use Psr\Container\ContainerInterface;
class PDOPool extends AbstractPool
{
/**
* @var string
*/
@ -60,5 +59,4 @@ class PDOPool extends AbstractPool
{
return new PDOConnection($this->container, $this, $this->config);
}
}
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
@ -17,7 +18,6 @@ use Psr\Container\ContainerInterface;
class PoolFactory
{
/**
* @var AbstractPool[]
*/

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
@ -11,7 +12,6 @@ declare(strict_types=1);
namespace Hyperf\DB\Pool;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\ConnectionInterface;
use Hyperf\DB\Frequency;
@ -21,7 +21,6 @@ use Psr\Container\ContainerInterface;
class SwooleMySQLPool extends AbstractPool
{
/**
* @var string
*/
@ -60,5 +59,4 @@ class SwooleMySQLPool extends AbstractPool
{
return new SwooleMySQLConnection($this->container, $this, $this->config);
}
}
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
@ -18,7 +19,6 @@ use Swoole\Coroutine\MySQL;
class SwooleMySQLConnection extends AbstractConnection
{
/**
* @var MySQL
*/
@ -71,7 +71,7 @@ class SwooleMySQLConnection extends AbstractConnection
return $this;
}
if (!$this->reconnect()) {
if (! $this->reconnect()) {
throw new ConnectionException('Connection reconnect failed.');
}
@ -111,7 +111,6 @@ class SwooleMySQLConnection extends AbstractConnection
return true;
}
public function beginTransaction()
{
$this->connection->begin();
@ -142,7 +141,6 @@ class SwooleMySQLConnection extends AbstractConnection
return $this->connection->insert_id;
}
public function prepare(string $sql, array $data = [], array $options = []): bool
{
return $this->connection->prepare($sql)->execute($data);
@ -152,4 +150,4 @@ class SwooleMySQLConnection extends AbstractConnection
{
return $this->connection->query($sql);
}
}
}

View File

@ -10,7 +10,7 @@ declare(strict_types=1);
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Cases;
namespace HyperfTest\DB\Cases;
use PHPUnit\Framework\TestCase;

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
@ -9,7 +10,7 @@ declare(strict_types=1);
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Cases;
namespace HyperfTest\DB\Cases;
use Hyperf\Config\Config;
use Hyperf\Contract\ConfigInterface;
@ -29,36 +30,35 @@ class PDODriverTest extends AbstractTestCase
public function testPDO()
{
$connect = $this->getPDODB();
$stmt = $connect->prepare("INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)", [1, 2, 3]);
$stmt = $connect->prepare('INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)', [1, 2, 3]);
$this->assertSame(true, $stmt);
$testList = $connect->query("SELECT * FROM `test`");
$testList = $connect->query('SELECT * FROM `test`');
$this->assertNotNull($testList);
// rollback test
$connect->beginTransaction();
$connect->prepare("INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)", [9, 9, 9]);
$connect->prepare('INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)', [9, 9, 9]);
$connect->rollback();
// commit test
$connect->beginTransaction();
$connect->prepare("INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)", [8, 8, 8]);
$connect->prepare('INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)', [8, 8, 8]);
$connect->commit();
// transaction Nesting test
$connect->beginTransaction();
$connect->prepare("INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)", [6, 6, 6]);
$connect->prepare('INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)', [6, 6, 6]);
$connect->beginTransaction();
$connect->prepare("INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)", [7, 7, 7]);
$connect->prepare('INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)', [7, 7, 7]);
$connect->rollback();
$connect->prepare("INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)", [5, 5, 5]);
$connect->prepare('INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)', [5, 5, 5]);
$connect->commit();
@ -88,7 +88,7 @@ class PDODriverTest extends AbstractTestCase
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => (float)env('DB_MAX_IDLE_TIME', 60),
'max_idle_time' => (float) env('DB_MAX_IDLE_TIME', 60),
],
],
],

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
@ -9,8 +10,7 @@ declare(strict_types=1);
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Cases;
namespace HyperfTest\DB\Cases;
use Hyperf\Config\Config;
use Hyperf\Contract\ConfigInterface;
@ -30,43 +30,41 @@ class SwooleMySQLDriverTest extends AbstractTestCase
public function testSwooleMySQL()
{
$connect = $this->getSwooleMySqlDB();
$stmt = $connect->prepare("INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)", [1, 2, 3]);
$stmt = $connect->prepare('INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)', [1, 2, 3]);
$this->assertSame(true, $stmt);
$testList = $connect->query("SELECT * FROM `test`");
$testList = $connect->query('SELECT * FROM `test`');
$this->assertNotNull($testList);
// rollback test
$connect->beginTransaction();
$connect->prepare("INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)", [9, 9, 9]);
$connect->prepare('INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)', [9, 9, 9]);
$connect->rollback();
// commit test
$connect->beginTransaction();
$connect->prepare("INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)", [8, 8, 8]);
$connect->prepare('INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)', [8, 8, 8]);
$connect->commit();
// transaction Nesting test
$connect->beginTransaction();
$connect->prepare("INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)", [6, 6, 6]);
$connect->prepare('INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)', [6, 6, 6]);
$connect->beginTransaction();
$connect->prepare("INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)", [7, 7, 7]);
$connect->prepare('INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)', [7, 7, 7]);
$connect->rollback();
$connect->prepare("INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)", [5, 5, 5]);
$connect->prepare('INSERT INTO `test`(`a`,`b`,`c`) VALUES (?,?,?)', [5, 5, 5]);
$connect->commit();
var_dump($connect->getLastInsertId());
var_dump($connect->getErrorCode());
var_dump($connect->getErrorInfo());
}
/**
@ -93,7 +91,7 @@ class SwooleMySQLDriverTest extends AbstractTestCase
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => (float)env('DB_MAX_IDLE_TIME', 60),
'max_idle_time' => (float) env('DB_MAX_IDLE_TIME', 60),
],
],
],