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 2 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
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.
50 changes: 50 additions & 0 deletions src/db/composer.json
@@ -0,0 +1,50 @@
{
"name": "hyperf/db",
"type": "library",
"license": "MIT",
"keywords": [
"php",
"hyperf"
],
"description": "",
"autoload": {
"psr-4": {
"Hyperf\\DB\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"HyperfTest\\": "tests"
}
},
"require": {
"php": ">=7.2",
"ext-swoole": ">=4.4",
"hyperf/config": "^1.1",
"hyperf/di": "1.1.*",
"hyperf/framework": "1.1.*",
"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"
}
}
}
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__,
],
],
],
];
}
}
67 changes: 67 additions & 0 deletions src/db/src/DB.php
@@ -0,0 +1,67 @@
<?php

namespace Hyperf\DB;


use Hyperf\DB\Pool\AbstractPool;
use Hyperf\DB\Pool\PoolFactory;
use Hyperf\DB\Pool\SwooleMySqlPool;
use Hyperf\Pool\Connection;
use Hyperf\Utils\Context;
use Swoole\Coroutine\MySQL;

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);

$result = $connection->{$name}(...$arguments);

return $result;
}


/**
* Get a connection from coroutine context, or from redis connectio pool.
* @param mixed $hasContextConnection
*/
private function getConnection($hasContextConnection): Connection
{
$connection = null;
if ($hasContextConnection) {
$connection = Context::get($this->getContextKey());
}
if (!$connection instanceof Connection) {
$pool = $this->factory->getPool($this->poolName);
$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);
}
}
15 changes: 15 additions & 0 deletions src/db/src/Exception/QueryException.php
@@ -0,0 +1,15 @@
<?php
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)
{
parent::__construct($message, $code, $previous);
}
}
19 changes: 19 additions & 0 deletions src/db/src/Frequency.php
@@ -0,0 +1,19 @@
<?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\Pool\Frequency as DefaultFrequency;

class Frequency extends DefaultFrequency
{
}
108 changes: 108 additions & 0 deletions src/db/src/PDOConnection.php
@@ -0,0 +1,108 @@
<?php
declare(strict_types=1);

namespace Hyperf\DB;

use Hyperf\Contract\ConnectionInterface;
use Hyperf\Pool\Connection as BaseConnection;
use Hyperf\Pool\Exception\ConnectionException;
use Hyperf\Pool\Pool;
use PDO;
use Psr\Container\ContainerInterface;

class PDOConnection extends BaseConnection implements ConnectionInterface
{

/**
* @var PDO
*/
protected $connection;

/**
* @var array
*/
protected $config = [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'test',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'pool' => [
'min_connections' => 1,
'max_connections' => 10,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => '60',
],
];

/**
* Current redis database.
* @var null|int
*/
protected $database;

public function __construct(ContainerInterface $container, Pool $pool, array $config)
{
parent::__construct($container, $pool);
$this->config = array_replace($this->config, $config);

$this->reconnect();
}

public function __call($name, $arguments)
{
return $this->connection->{$name}(...$arguments);
}

public function getActiveConnection()
{
if ($this->check()) {
return $this;
}

if (!$this->reconnect()) {
throw new ConnectionException('Connection reconnect failed.');
}

return $this;
}

/**
* Reconnect the connection.
*/
public function reconnect(): bool
{
$dbms = $this->config['driver'];
$host = $this->config['host'];
$dbName = $this->config['database'];
$username = $this->config['username'];
$password = $this->config['password'];
$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);
} catch (\PDOException $e) {
throw new ConnectionException('Connection reconnect failed.:' . $e->getMessage());
}

$this->connection = $pdo;
$this->lastUseTime = microtime(true);

return true;
}

/**
* Close the connection.
*/
public function close(): bool
{
unset($this->connection);

return true;
}
}
12 changes: 12 additions & 0 deletions src/db/src/Pool/AbstractPool.php
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace Hyperf\DB\Pool;


use Hyperf\Pool\Pool;

abstract class AbstractPool extends Pool
{

}