-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4798336
Showing
14 changed files
with
732 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
composer.lock | ||
vendor |
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,73 @@ | ||
### 基于Swoole 封装的`连接池`以及适配了`illuminate/database` | ||
###### 目前基于PDO规范 支持MySQL,SQL Server,Postgres和SQLite。 | ||
|
||
#### 1. 引用本包 | ||
``` | ||
composer require clover-swoole/database:dev-master | ||
``` | ||
#### 2. 创建`配置`实例 | ||
```php | ||
/** | ||
* 创建配置 | ||
*/ | ||
(new \itxiao6\SwooleDatabase\PDOConfig())-> | ||
withDriver('mysql')-> // 驱动类型 | ||
withHost('127.0.0.1')-> // 主机地址 | ||
withDbname('test')-> // 数据库名 | ||
withUsername('root')-> // 用户名 | ||
withPassword('123456')-> // 密码 | ||
withCharset('utf8mb4')-> // 字符集编码 | ||
setConfig('default'); // 设置全局访问(默认为default) | ||
``` | ||
#### 3. 设置 | ||
```php | ||
\itxiao6\SwooleDatabase\PoolManager::addPool(64,'default'); // 设置指定连接池尺寸(连接名称默认为 default) | ||
``` | ||
|
||
#### 4. 使用协程环境模拟Swoole 的任务执行 | ||
```php | ||
/** | ||
* 开启协程(如果框架内已经开启可忽略) | ||
*/ | ||
\Swoole\Runtime::enableCoroutine(); | ||
/** | ||
* 记录开始时间 | ||
*/ | ||
$s = microtime(true); | ||
\Swoole\Coroutine\run(function () { | ||
/** | ||
* 循环创建协程(模拟HTTP 请求 执行任务) | ||
*/ | ||
for ($i = 0; $i < 20; $i++) { | ||
\Swoole\Coroutine::create(function () { | ||
/** | ||
* 设置 回收数据库连接 | ||
*/ | ||
Swoole\Coroutine::defer(function () { | ||
$lists = \itxiao6\SwooleDatabase\Utils\Context::get(\itxiao6\SwooleDatabase\PoolManager::class . '_connection') === null ? [] : \itxiao6\SwooleDatabase\Utils\Context::get(\itxiao6\SwooleDatabase\PoolManager::class . '_connection'); | ||
foreach ($lists as $item) { | ||
\itxiao6\SwooleDatabase\PoolManager::getPool($item['name'])->put($item['pdo']); | ||
} | ||
}); | ||
/** | ||
* 创建表 | ||
*/ | ||
// \itxiao6\SwooleDatabase\Manager::schema()->create('test',function(\Illuminate\Database\Schema\Blueprint $table){ | ||
// $table->increments('id'); | ||
// $table->string('name')->nullable()->default(1); | ||
// $table->timestamps(); | ||
// }); | ||
/** | ||
* 模型查询 | ||
*/ | ||
// $lists = \itxiao6\SwooleDatabase\Model::query()->first(); | ||
/** | ||
* Connection 直接查询 | ||
*/ | ||
$lists = \itxiao6\SwooleDatabase\Adapter\Manager::table('bd_live_plan')->first(); | ||
var_dump(boolval($lists)); | ||
}); | ||
} | ||
}); | ||
echo '所有任务用了:' . (microtime(true) - $s) . '秒'; | ||
``` |
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,31 @@ | ||
{ | ||
"name": "itxiao6/database", | ||
"description": "The Swoole Pool Database package.", | ||
"license": "MIT", | ||
"homepage": "https://www.itxiao6.top", | ||
"keywords": [ | ||
"Laravel", | ||
"Swoole", | ||
"Databses", | ||
"Sql", | ||
"ORM" | ||
], | ||
"authors": [ | ||
{ | ||
"name": "itxiao6", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"illuminate/database": "v8.8.0", | ||
"symfony/console": "^5.1" | ||
}, | ||
"require-dev": { | ||
"swoole/ide-helper": "@dev" | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"itxiao6\\SwooleDatabase\\": "src/" | ||
} | ||
} | ||
} |
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 | ||
|
||
|
||
namespace itxiao6\SwooleDatabase\Adapter; | ||
|
||
use Illuminate\Database\Connection; | ||
use InvalidArgumentException; | ||
|
||
|
||
use Illuminate\Database\Connectors\MySqlConnector; | ||
use Illuminate\Database\Connectors\PostgresConnector; | ||
use Illuminate\Database\Connectors\SQLiteConnector; | ||
use Illuminate\Database\Connectors\SqlServerConnector; | ||
use itxiao6\SwooleDatabase\Adapter\Connectors\MySqlConnection; | ||
use itxiao6\SwooleDatabase\Adapter\Connectors\PostgresConnection; | ||
use itxiao6\SwooleDatabase\Adapter\Connectors\SQLiteConnection; | ||
use itxiao6\SwooleDatabase\Adapter\Connectors\SqlServerConnection; | ||
use itxiao6\SwooleDatabase\PDOConfig; | ||
use itxiao6\SwooleDatabase\PoolManager; | ||
use itxiao6\SwooleDatabase\Utils\Context; | ||
|
||
/** | ||
* 链接工厂类 | ||
* Class ConnectionFactory | ||
* @package itxiao6\SwooleDatabase\Adapter | ||
*/ | ||
class ConnectionFactory | ||
{ | ||
/** | ||
* 获取连接 | ||
* @param string $name | ||
* @return mixed|null | ||
*/ | ||
public static function getConnection($name='default') | ||
{ | ||
$conifg = PDOConfig::getConfig($name); | ||
if(!(($connection = Context::get(Connection::class.$name.$conifg->getDriver())) instanceof \Illuminate\Database\Connection)){ | ||
$pool = PoolManager::getPool($name); | ||
$pdo = $pool->get(); | ||
$lists = Context::get(PoolManager::class.'_connection')===null?[]:Context::get(PoolManager::class.'_connection'); | ||
$lists[] = [ | ||
'name'=>$name, | ||
'pdo'=>$pdo | ||
]; | ||
Context::put(PoolManager::class.'_connection',$lists); | ||
$connection = self::createConnection($pdo,$conifg); | ||
Context::put(Connection::class.$name.$conifg->getDriver(),$connection); | ||
} | ||
return $connection; | ||
} | ||
/** | ||
* @param $pod | ||
* @param PDOConfig $config | ||
* @return MySqlConnection|PostgresConnection|SQLiteConnection|SqlServerConnection | ||
*/ | ||
public static function createConnection($pod, PDOConfig $config) | ||
{ | ||
if (!in_array($config->getDriver(), ['mysql', 'pgsql', 'sqlite', 'sqlsrv'])) { | ||
throw new InvalidArgumentException('A driver must be specified.'); | ||
} | ||
|
||
switch ($config->getDriver()) { | ||
case 'mysql': | ||
return new MySqlConnection($pod, $config->getDbname(), $config->getTablePrefix(), []); | ||
case 'pgsql': | ||
return new PostgresConnection($pod, $config->getDbname(), $config->getTablePrefix(), []); | ||
case 'sqlite': | ||
return new SQLiteConnection($pod, $config->getDbname(), $config->getTablePrefix(), []); | ||
case 'sqlsrv': | ||
return new SqlServerConnection($pod, $config->getDbname(), $config->getTablePrefix(), []); | ||
} | ||
|
||
throw new InvalidArgumentException("Unsupported driver [{$config['driver']}]."); | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace itxiao6\SwooleDatabase\Adapter\Connectors; | ||
|
||
|
||
use Illuminate\Database\Events\StatementPrepared; | ||
use itxiao6\SwooleDatabase\Adapter\QueryBuilder; | ||
/** | ||
* Mysql 连接适配器 | ||
* Class MySqlConnection | ||
* @package itxiao6\SwooleDatabase\Adapter\Connectors | ||
*/ | ||
class MySqlConnection extends \Illuminate\Database\MySqlConnection | ||
{ | ||
/** | ||
* Configure the PDO prepared statement. | ||
* | ||
* @param \PDOStatement $statement | ||
* @return \PDOStatement | ||
*/ | ||
protected function prepared($statement) | ||
{ | ||
$statement->setFetchMode($this->fetchMode); | ||
|
||
$this->event(new StatementPrepared( | ||
$this, $statement | ||
)); | ||
|
||
return $statement; | ||
} | ||
|
||
/** | ||
* 查询构造器 | ||
* @return QueryBuilder | ||
*/ | ||
public function query() | ||
{ | ||
return new QueryBuilder( | ||
$this, $this->getQueryGrammar(), $this->getPostProcessor() | ||
); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
|
||
namespace itxiao6\SwooleDatabase\Adapter\Connectors; | ||
|
||
/** | ||
* Class PostgresConnection | ||
* @package itxiao6\SwooleDatabase\Adapter\Connectors | ||
*/ | ||
class PostgresConnection extends \Illuminate\Database\PostgresConnection | ||
{ | ||
|
||
} |
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,13 @@ | ||
<?php | ||
|
||
|
||
namespace itxiao6\SwooleDatabase\Adapter\Connectors; | ||
|
||
/** | ||
* Class SQLiteConnection | ||
* @package itxiao6\SwooleDatabase\Adapter\Connectors | ||
*/ | ||
class SQLiteConnection extends \Illuminate\Database\SQLiteConnection | ||
{ | ||
|
||
} |
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,13 @@ | ||
<?php | ||
|
||
|
||
namespace itxiao6\SwooleDatabase\Adapter\Connectors; | ||
|
||
/** | ||
* Class SqlServerConnection | ||
* @package itxiao6\SwooleDatabase\Adapter\Connectors | ||
*/ | ||
class SqlServerConnection extends \Illuminate\Database\SqlServerConnection | ||
{ | ||
|
||
} |
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,62 @@ | ||
<?php | ||
|
||
|
||
namespace itxiao6\SwooleDatabase\Adapter; | ||
|
||
use Illuminate\Database\Connection; | ||
use itxiao6\SwooleDatabase\PDOConfig; | ||
use itxiao6\SwooleDatabase\Utils\Context; | ||
|
||
/** | ||
* Class Manager | ||
* @package itxiao6\SwooleDatabase\Adapter | ||
*/ | ||
class Manager extends \Illuminate\Database\Capsule\Manager | ||
{ | ||
/** | ||
* Get a fluent query builder instance. | ||
* | ||
* @param \Closure|\Illuminate\Database\Query\Builder|string $table | ||
* @param string|null $as | ||
* @param string|null $connection | ||
* @return QueryBuilder | ||
*/ | ||
public static function table($table, $as = null, $connection = null) | ||
{ | ||
return static::connection($connection)->table($table, $as); | ||
} | ||
/** | ||
* Get a schema builder instance. | ||
* | ||
* @param string|null $connection | ||
* @return \Illuminate\Database\Schema\Builder | ||
*/ | ||
public static function schema($connection = null) | ||
{ | ||
return static::connection($connection)->getSchemaBuilder(); | ||
} | ||
/** | ||
* Get a connection instance from the global manager. | ||
* | ||
* @param string|null $connection | ||
* @return \Illuminate\Database\Connection | ||
*/ | ||
public static function connection($name = null) | ||
{ | ||
if($name === null){ | ||
$name = 'default'; | ||
} | ||
return ConnectionFactory::getConnection($name); | ||
} | ||
/** | ||
* Dynamically pass methods to the default connection. | ||
* | ||
* @param string $method | ||
* @param array $parameters | ||
* @return mixed | ||
*/ | ||
public static function __callStatic($method, $parameters) | ||
{ | ||
return static::connection()->$method(...$parameters); | ||
} | ||
} |
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 | ||
|
||
namespace itxiao6\SwooleDatabase\Adapter; | ||
/** | ||
* 模型基类 | ||
* Class Model | ||
* @package itxiao6\SwooleDatabase\Adapter | ||
*/ | ||
class Model extends \Illuminate\Database\Eloquent\Model | ||
{ | ||
/** | ||
* Resolve a connection instance. | ||
* | ||
* @param string|null $connection | ||
* @return \Illuminate\Database\Connection | ||
*/ | ||
public static function resolveConnection($connection = null) | ||
{ | ||
return Manager::connection($connection); | ||
} | ||
|
||
/** | ||
* Begin querying the model. | ||
* | ||
* @return QueryBuilder | ||
*/ | ||
public static function query() | ||
{ | ||
return (new static)->newQuery(); | ||
} | ||
|
||
/** | ||
* Get the database connection for the model. | ||
* | ||
* @return \Illuminate\Database\Connection | ||
*/ | ||
public function getConnection() | ||
{ | ||
return Manager::connection($this->getConnectionName()); | ||
} | ||
} |
Oops, something went wrong.