Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加一个db的组件,databases配置文件添加一个port配置 #827

Merged
merged 30 commits into from Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
98c2ace
Added Hyperf DB
PandaLIU-1111 Oct 31, 2019
cd5d44b
databases 配置文件添加一个port配置
PandaLIU-1111 Oct 31, 2019
209e00a
Rename to MySQL
PandaLIU-1111 Nov 1, 2019
1ceb6ad
添加接口
PandaLIU-1111 Nov 1, 2019
d9ac64c
删除注释
PandaLIU-1111 Nov 1, 2019
8e32291
Merge branch 'master' into pr/827
limingxinleo Nov 2, 2019
c387bb8
Format code.
limingxinleo Nov 2, 2019
bc5984d
Renamed config database to db.
limingxinleo Nov 2, 2019
15a415c
解决事务嵌套
PandaLIU-1111 Nov 2, 2019
3956aee
Added ConnectionInterface.
limingxinleo Nov 2, 2019
7b3e55f
Merge branch 'master' of https://github.com/PandaLIU-1111/hyperf into…
limingxinleo Nov 2, 2019
5534115
Format code.
limingxinleo Nov 2, 2019
26142b7
Remove swooleMySQL file, Remove ver_dump
PandaLIU-1111 Nov 2, 2019
f322346
Deleted useless code.
limingxinleo Nov 2, 2019
c1431fc
Merge branch 'master' of https://github.com/PandaLIU-1111/hyperf into…
limingxinleo Nov 2, 2019
88a51c0
Added ManagesTransactions.
limingxinleo Nov 2, 2019
550c42d
Deleted useless code.
limingxinleo Nov 2, 2019
33a219b
Deleted dependency `hyperf/database`.
limingxinleo Nov 2, 2019
69599ab
Optimized code and added testing.
limingxinleo Nov 2, 2019
e3a1455
Fixed travis
limingxinleo Nov 2, 2019
fed531b
Optimized MySQLConnection.
limingxinleo Nov 2, 2019
ba8e920
Added db config file
PandaLIU-1111 Nov 2, 2019
c620df9
Updated db config file
PandaLIU-1111 Nov 2, 2019
a505ea4
Optimized code.
limingxinleo Nov 4, 2019
b155cfb
Update ConnectionInterface.php
limingxinleo Nov 4, 2019
625d3c5
Added document.
limingxinleo Nov 4, 2019
ccfa341
Update changelog.md
limingxinleo Nov 4, 2019
31f44ca
Merge branch 'master' into pr/827
limingxinleo Nov 4, 2019
d83e764
Merge branch 'master' into pr/827
limingxinleo Nov 9, 2019
d35ce4f
Optimized code.
limingxinleo Nov 9, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Expand Up @@ -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/",
Expand Down Expand Up @@ -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/",
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/db-connection/publish/databases.php
Expand Up @@ -14,6 +14,7 @@
'default' => [
'driver' => env('DB_DRIVER', 'mysql'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'hyperf'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
Expand Down
1 change: 1 addition & 0 deletions src/db/.gitattributes
@@ -0,0 +1 @@
/tests export-ignore
21 changes: 21 additions & 0 deletions src/db/LICENSE
@@ -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.
52 changes: 52 additions & 0 deletions src/db/composer.json
@@ -0,0 +1,52 @@
{
"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/database": "~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"
},
"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"
}
}
}
30 changes: 30 additions & 0 deletions src/db/src/AbstractConnection.php
@@ -0,0 +1,30 @@
<?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\Database\DetectsDeadlocks;
use Hyperf\Database\DetectsLostConnections;
use Hyperf\Pool\Connection;

abstract class AbstractConnection extends Connection implements ConnectionInterface
{
use DetectsDeadlocks;
use DetectsLostConnections;

/**
* The number of active transactions.
*
* @var int
*/
protected $transactions = 0;
}
33 changes: 33 additions & 0 deletions src/db/src/ConfigProvider.php
@@ -0,0 +1,33 @@
<?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__,
],
],
],
];
}
}
62 changes: 62 additions & 0 deletions src/db/src/ConnectionInterface.php
@@ -0,0 +1,62 @@
<?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;

interface ConnectionInterface
{
/**
* Start a new database transaction.
*/
public function beginTransaction(): void;

/**
* Commit the active database transaction.
*/
public function commit(): void;

/**
* Rollback the active database transaction.
*/
public function rollBack(): void;

/**
* Run an insert statement against the database.
*
* @return int|string last insert id
*/
public function insert(string $query, array $bindings = []);

/**
* Run an execute statement against the database.
*
* @return int affected rows
*/
public function execute(string $query, array $bindings = []): int;

/**
* Execute an SQL statement and return the number of affected rows.
*
* @return int affected rows
*/
public function exec(string $sql): int;

/**
* Run a select statement against the database.
*/
public function query(string $query, array $bindings = []): array;

/**
* Run a select statement and return a single result.
*/
public function fetch(string $query, array $bindings = []): array;
}
99 changes: 99 additions & 0 deletions src/db/src/DB.php
@@ -0,0 +1,99 @@
<?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\DB\Pool\PoolFactory;
use Hyperf\Utils\Context;

/**
* Class DB.
* @method beginTransaction()
* @method commit()
* @method rollback()
* @method getErrorCode()
* @method getErrorInfo()
* @method getLastInsertId()
* @method prepare(string $sql, array $data = [], array $options = [])
* @method query(string $sql)
*/
class DB
{
/**
* @var PoolFactory
*/
protected $factory;

/**
* @var string
*/
protected $poolName = 'default';

public function __construct(PoolFactory $factory)
{
$this->factory = $factory;
}

public function __call($name, $arguments)
{
// Get a connection from coroutine context or connection pool.
$hasContextConnection = Context::has($this->getContextKey());
$connection = $this->getConnection($hasContextConnection);
switch ($name) {
case 'beginTransaction':
Context::set($this->getContextKey(), $connection);
$transctionManager = new TransactionManager();
$result = $transctionManager->beginTransaction();
break;
case 'commit':
case 'rollback':
$transctionManager = new TransactionManager();
$result = $transctionManager->{$name}();
break;
default:
$result = $connection->{$name}(...$arguments);
}

return $result;
}

/**
* Get a connection from coroutine context, or from mysql connectio pool.
* @param mixed $hasContextConnection
*/
private function getConnection($hasContextConnection): AbstractConnection
{
$connection = null;
if ($hasContextConnection) {
$connection = Context::get($this->getContextKey());
}
if (! $connection instanceof AbstractConnection) {
$pool = $this->factory->getPool($this->poolName);
Context::set('poolId', $pool->getCurrentConnections());
$connection = $pool->get()->getConnection();
}
return $connection;
}

/**
* The key to identify the connection object in coroutine context.
*/
private function getContextKey(): string
{
return sprintf('database.%s', $this->poolName);
}

private function getPoolId()
{
return Context::get('poolId');
}
}
17 changes: 17 additions & 0 deletions src/db/src/Exception/DriverNotFoundException.php
@@ -0,0 +1,17 @@
<?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\Exception;

class DriverNotFoundException extends RuntimeException
{
}
17 changes: 17 additions & 0 deletions src/db/src/Exception/QueryException.php
@@ -0,0 +1,17 @@
<?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\Exception;

class QueryException extends \PDOException
{
}
17 changes: 17 additions & 0 deletions src/db/src/Exception/RuntimeException.php
@@ -0,0 +1,17 @@
<?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\Exception;

class RuntimeException extends \RuntimeException
{
}