Added Hyperf\DB\DB::connection(). (#1371)

This commit is contained in:
pandaLIU 2020-03-04 16:44:26 +08:00 committed by GitHub
parent 9e0c21adb1
commit 4af1415dce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 3 deletions

View File

@ -3,6 +3,7 @@
## Added
- [#1362](https://github.com/hyperf/hyperf/pull/1362) Added `getPoolNames()` for `Hyperf\Pool\SimplePool\PoolFactory`.
- [#1371](https://github.com/hyperf/hyperf/pull/1371) Added `Hyperf\DB\DB::connection()`.
## Fixed

View File

@ -44,14 +44,15 @@ php bin/hyperf.php vendor:publish hyperf/db
具体接口可以查看 `Hyperf\DB\ConnectionInterface`
| 方法名 | 返回值类型 | 备注 |
|:----------------:|:--------------:|:---------------------------------------:|
|:----------------:|:--------------:|:------------------------------------:|
| beginTransaction | `void` | 开启事务 支持事务嵌套 |
| commit | `void` | 提交事务 支持事务嵌套 |
| rollBack | `void` | 回滚事务 支持事务嵌套 |
| insert | `int` | 插入数据,返回主键 ID非自增主键返回 0 |
| insert | `int` | 插入数据,返回主键 ID非自增主键返回 0 |
| execute | `int` | 执行 SQL返回受影响的行数 |
| query | `array` | 查询 SQL返回结果集列表 |
| fetch | `array,object` | 查询 SQL返回结果集的首行数据 |
| fetch | `array, object`| 查询 SQL返回结果集的首行数据 |
| connection | `self` | 指定连接的数据库 |
## 使用

View File

@ -79,6 +79,16 @@ class DB
return $db->{$name}(...$arguments);
}
/**
* Make a new connection with the pool name.
*/
public static function connection(string $poolName): self
{
return make(static::class, [
'poolName' => $poolName,
]);
}
/**
* Define the commands that needs same connection to execute.
* When these commands executed, the connection will storage to coroutine context.

View File

@ -55,6 +55,15 @@ abstract class AbstractTestCase extends TestCase
],
'options' => $options,
],
'pdo' => [
'driver' => 'pdo',
'password' => '',
'database' => 'hyperf',
'pool' => [
'max_connections' => 20,
],
'options' => $options,
],
],
]));
$container->shouldReceive('make')->with(PDOPool::class, Mockery::any())->andReturnUsing(function ($_, $args) {
@ -72,6 +81,9 @@ abstract class AbstractTestCase extends TestCase
});
$container->shouldReceive('get')->with(PoolFactory::class)->andReturn($factory = new PoolFactory($container));
$container->shouldReceive('get')->with(DB::class)->andReturn(new DB($factory, 'default'));
$container->shouldReceive('make')->with(DB::class, Mockery::any())->andReturnUsing(function ($_, $params) use ($factory) {
return new DB($factory, $params['poolName']);
});
$container->shouldReceive('has')->with(StdoutLoggerInterface::class)->andReturn(false);
ApplicationContext::setContainer($container);
return $container;

View File

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\DB\Cases;
use Hyperf\DB\DB;
/**
* @internal
* @coversNothing
*/
class DBTest extends AbstractTestCase
{
public function testDBConnection()
{
$container = $this->getContainer();
$db = $container->get(DB::class);
$db2 = DB::connection('pdo');
$this->assertInstanceOf(DB::class, $db);
$this->assertInstanceOf(DB::class, $db2);
$ref = new \ReflectionClass($db);
$property = $ref->getProperty('poolName');
$property->setAccessible(true);
$this->assertSame('default', $property->getValue($db));
$this->assertSame('pdo', $property->getValue($db2));
}
}