Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #827 from PandaLIU-1111/master
添加一个db的组件,databases配置文件添加一个port配置
- Loading branch information
Showing
31 changed files
with
1,551 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# 极简的DB组件 | ||
|
||
[hyperf/database](https://github.com/hyperf/database) 功能十分强大,但也不可否认效率上确实些许不足。这里提供一个极简的 `DB` 组件,支持 `PDO` 和 `SwooleMysql`。 | ||
|
||
> 压测对比 database 1800qps,db 6800qps。 | ||
## 组件配置 | ||
|
||
默认配置 `autoload/db.php` 如下,数据库支持多库配置,默认为 `default`。 | ||
|
||
| 配置项 | 类型 | 默认值 | 备注 | | ||
|:--------------------:|:------:|:------------------:|:--------------------------------:| | ||
| driver | string | 无 | 数据库引擎 支持 `pdo` 和 `mysql` | | ||
| host | string | `localhost` | 数据库地址 | | ||
| port | int | 3306 | 数据库地址 | | ||
| database | string | 无 | 数据库默认DB | | ||
| username | string | 无 | 数据库用户名 | | ||
| password | string | null | 数据库密码 | | ||
| charset | string | utf8 | 数据库编码 | | ||
| collation | string | utf8_unicode_ci | 数据库编码 | | ||
| fetch_mode | int | `PDO::FETCH_ASSOC` | PDO查询结果集类型 | | ||
| pool.min_connections | int | 1 | 连接池内最少连接数 | | ||
| pool.max_connections | int | 10 | 连接池内最大连接数 | | ||
| pool.connect_timeout | float | 10.0 | 连接等待超时时间 | | ||
| pool.wait_timeout | float | 3.0 | 超时时间 | | ||
| pool.heartbeat | int | -1 | 心跳 | | ||
| pool.max_idle_time | float | 60.0 | 最大闲置时间 | | ||
| options | array | | PDO 配置 | | ||
|
||
## 组件支持的方法 | ||
|
||
具体接口可以查看 `Hyperf\DB\ConnectionInterface`。 | ||
|
||
| 方法名 | 返回值类型 | 备注 | | ||
|:----------------:|:----------:|:--------------------------------------:| | ||
| beginTransaction | void | 开启事务 支持事务嵌套 | | ||
| commit | void | 提交事务 支持事务嵌套 | | ||
| rollBack | void | 回滚事务 支持事务嵌套 | | ||
| insert | int | 插入数据,返回主键ID,非自称主键返回 0 | | ||
| execute | int | 执行SQL,返回受影响的行数 | | ||
| query | array | 查询SQL | | ||
| fetch | array | object|查询SQL,返回首行数据 | | ||
|
||
## 使用 | ||
|
||
### 使用DB实例 | ||
|
||
```php | ||
<?php | ||
|
||
use Hyperf\Utils\ApplicationContext; | ||
use Hyperf\DB\DB; | ||
|
||
$db = ApplicationContext::getContainer()->get(DB::class); | ||
|
||
$res = $db->query('SELECT * FROM `user` WHERE gender = ?;',[1]); | ||
|
||
``` | ||
|
||
### 使用静态方法 | ||
|
||
```php | ||
<?php | ||
|
||
use Hyperf\DB\DB; | ||
|
||
$res = DB::query('SELECT * FROM `user` WHERE gender = ?;',[1]); | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/tests export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Hyperf | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"name": "hyperf/db", | ||
"type": "library", | ||
"license": "MIT", | ||
"keywords": [ | ||
"php", | ||
"hyperf" | ||
], | ||
"description": "", | ||
"autoload": { | ||
"psr-4": { | ||
"Hyperf\\DB\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"HyperfTest\\DB\\": "tests/" | ||
} | ||
}, | ||
"require": { | ||
"php": ">=7.2", | ||
"ext-swoole": ">=4.4", | ||
"hyperf/config": "~1.1.0", | ||
"hyperf/contract": "~1.1.0", | ||
"hyperf/pool": "~1.1.0", | ||
"hyperf/utils": "~1.1.0", | ||
"psr/container": "^1.0" | ||
}, | ||
"require-dev": { | ||
"friendsofphp/php-cs-fixer": "^2.14", | ||
"phpstan/phpstan": "^0.10.5", | ||
"hyperf/testing": "1.1.*", | ||
"mockery/mockery": "^1.0", | ||
"swoft/swoole-ide-helper": "dev-master" | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
}, | ||
"scripts": { | ||
"test": "co-phpunit -c phpunit.xml --colors=always", | ||
"analyze": "phpstan analyse --memory-limit 300M -l 0 ./src", | ||
"cs-fix": "php-cs-fixer fix $1" | ||
}, | ||
"extra": { | ||
"hyperf": { | ||
"config": "Hyperf\\DB\\ConfigProvider" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?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 | ||
*/ | ||
|
||
return [ | ||
'default' => [ | ||
'driver' => 'pdo', | ||
'host' => env('DB_HOST', 'localhost'), | ||
'port' => env('DB_PORT', 3306), | ||
'database' => env('DB_DATABASE', 'hyperf'), | ||
'username' => env('DB_USERNAME', 'root'), | ||
'password' => env('DB_PASSWORD', ''), | ||
'charset' => env('DB_CHARSET', 'utf8mb4'), | ||
'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'), | ||
'fetch_mode' => PDO::FETCH_ASSOC, | ||
'pool' => [ | ||
'min_connections' => 1, | ||
'max_connections' => 10, | ||
'connect_timeout' => 10.0, | ||
'wait_timeout' => 3.0, | ||
'heartbeat' => -1, | ||
'max_idle_time' => (float) env('DB_MAX_IDLE_TIME', 60), | ||
], | ||
'options' => [ | ||
PDO::ATTR_CASE => PDO::CASE_NATURAL, | ||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | ||
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, | ||
PDO::ATTR_STRINGIFY_FETCHES => false, | ||
PDO::ATTR_EMULATE_PREPARES => false, | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?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 Hyperf\DB; | ||
|
||
use Hyperf\Contract\StdoutLoggerInterface; | ||
use Hyperf\Pool\Connection; | ||
use Hyperf\Pool\Exception\ConnectionException; | ||
|
||
abstract class AbstractConnection extends Connection implements ConnectionInterface | ||
{ | ||
use DetectsLostConnections; | ||
use ManagesTransactions; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $config = []; | ||
|
||
public function getConfig(): array | ||
{ | ||
return $this->config; | ||
} | ||
|
||
public function release(): void | ||
{ | ||
if ($this->transactionLevel() > 0) { | ||
$this->rollBack(0); | ||
if ($this->container->has(StdoutLoggerInterface::class)) { | ||
$logger = $this->container->get(StdoutLoggerInterface::class); | ||
$logger->error('Maybe you\'ve forgotten to commit or rollback the MySQL transaction.'); | ||
} | ||
} | ||
$this->pool->release($this); | ||
} | ||
|
||
public function getActiveConnection() | ||
{ | ||
if ($this->check()) { | ||
return $this; | ||
} | ||
|
||
if (! $this->reconnect()) { | ||
throw new ConnectionException('Connection reconnect failed.'); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function retry(\Throwable $throwable, $name, $arguments) | ||
{ | ||
if ($this->causedByLostConnection($throwable)) { | ||
try { | ||
$this->reconnect(); | ||
return $this->{$name}(...$arguments); | ||
} catch (\Throwable $throwable) { | ||
if ($this->container->has(StdoutLoggerInterface::class)) { | ||
$logger = $this->container->get(StdoutLoggerInterface::class); | ||
$logger->error('Connection execute retry failed. message = ' . $throwable->getMessage()); | ||
} | ||
} | ||
} | ||
|
||
throw $throwable; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?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 Hyperf\DB; | ||
|
||
class ConfigProvider | ||
{ | ||
public function __invoke(): array | ||
{ | ||
return [ | ||
'dependencies' => [ | ||
], | ||
'commands' => [ | ||
], | ||
'annotations' => [ | ||
'scan' => [ | ||
'paths' => [ | ||
__DIR__, | ||
], | ||
], | ||
], | ||
'publish' => [ | ||
[ | ||
'id' => 'db', | ||
'description' => 'The config for db.', | ||
'source' => __DIR__ . '/../publish/db.php', | ||
'destination' => BASE_PATH . '/config/autoload/db.php', | ||
], | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.