Skip to content

Commit

Permalink
feat: Added strict mode and fetch mode for Mysql client (swoft-cloud/…
Browse files Browse the repository at this point in the history
…swoft-component#208)

* 增加DB严格模式配置

* 增加配置读取单测

* Update SqlTest.php

* Update DbPoolProperties.php

* Remoe useless comments.
  • Loading branch information
limingxinleo authored and swoft-bot committed Oct 6, 2018
1 parent a61928c commit bd44c19
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 16 deletions.
31 changes: 22 additions & 9 deletions src/Driver/Mysql/MysqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Swoft\Db\AbstractDbConnection;
use Swoft\Db\Bean\Annotation\Connection;
use Swoft\Db\Exception\MysqlException;
use Swoft\Db\Pool\Config\DbPoolProperties;
use Swoole\Coroutine\Mysql;

/**
Expand Down Expand Up @@ -59,17 +60,29 @@ public function createConnection()
$options = $this->parseUri($uri);
$options['timeout'] = $this->pool->getTimeout();

/** @var DbPoolProperties $config */
$config = $this->pool->getPoolConfig();
$strictType = $config->isStrictType();
$fetchMode = $config->isFetchMode();

$serverConfig = [
'host' => $options['host'],
'port' => $options['port'],
'user' => $options['user'],
'password' => $options['password'],
'database' => $options['database'],
'timeout' => $options['timeout'],
'charset' => $options['charset'],
'strict_type' => $strictType,
];

if (version_compare(swoole_version(), '4.0', '>=')) {
$serverConfig['fetch_mode'] = $fetchMode;
}

// init
$mysql = new MySQL();
$mysql->connect([
'host' => $options['host'],
'port' => $options['port'],
'user' => $options['user'],
'password' => $options['password'],
'database' => $options['database'],
'timeout' => $options['timeout'],
'charset' => $options['charset'],
]);
$mysql->connect($serverConfig);

// error
if ($mysql->connected === false) {
Expand Down
10 changes: 10 additions & 0 deletions src/Driver/Mysql/SyncMysqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Swoft\Db\Bean\Annotation\Connection;
use Swoft\Db\Driver\DriverType;
use Swoft\Db\Exception\MysqlException;
use Swoft\Db\Pool\Config\DbPoolProperties;

/**
* Mysql sync connection
Expand Down Expand Up @@ -47,6 +48,9 @@ public function createConnection()
$options = $this->parseUri($uri);
$options['timeout'] = $this->pool->getTimeout();

/** @var DbPoolProperties $config */
$config = $this->pool->getPoolConfig();

$user = $options['user'];
$passwd = $options['password'];
$host = $options['host'];
Expand All @@ -60,6 +64,12 @@ public function createConnection()
\PDO::ATTR_TIMEOUT => $timeout,
\PDO::ATTR_PERSISTENT => true,
];

if ($config->isStrictType()) {
$pdoOptions[\PDO::ATTR_STRINGIFY_FETCHES] = false;
$pdoOptions[\PDO::ATTR_EMULATE_PREPARES] = false;
}

$dsn = "mysql:host=$host;port=$port;dbname=$dbName;charset=$charset";
$this->connection = new \PDO($dsn, $user, $passwd, $pdoOptions);
$this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
Expand Down
12 changes: 12 additions & 0 deletions src/Pool/Config/DbPoolConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,16 @@ class DbPoolConfig extends DbPoolProperties
* @var string
*/
protected $driver = Driver::MYSQL;

/**
* @Value(name="${config.db.master.strictType}", env="${DB_STRICT_TYPE}")
* @var bool
*/
protected $strictType = false;

/**
* @Value(name="${config.db.master.fetchMode}", env="${DB_FETCH_MODE}")
* @var bool
*/
protected $fetchMode = true;
}
30 changes: 23 additions & 7 deletions src/Pool/Config/DbPoolProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@

/**
* The pool properties of database
*
* @uses DbPoolProperties
* @version 2018年01月27日
* @author stelin <[email protected]>
* @copyright Copyright 2010-2016 swoft software
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
*/
class DbPoolProperties extends PoolProperties
{
Expand All @@ -31,10 +25,32 @@ class DbPoolProperties extends PoolProperties
protected $driver = Driver::MYSQL;

/**
* @return string
* 开启严格模式,返回的字段将自动转为数字类型
*
* @var bool
*/
protected $strictType = false;

/**
* 开启 Fetch 模式, 可类似于 PDO 一样使用 fetch/fetchAll 逐行获取或获取全部结果集
*
* @since Swoole 4.0
* @var bool
*/
protected $fetchMode = true;

public function getDriver(): string
{
return $this->driver;
}

public function isStrictType(): bool
{
return $this->strictType;
}

public function isFetchMode(): bool
{
return $this->fetchMode;
}
}
12 changes: 12 additions & 0 deletions src/Pool/Config/DbSlavePoolConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,16 @@ class DbSlavePoolConfig extends DbPoolProperties
* @var string
*/
protected $driver = Driver::MYSQL;

/**
* @Value(name="${config.db.slave.strictType}", env="${DB_SLAVE_STRICT_TYPE}")
* @var bool
*/
protected $strictType = false;

/**
* @Value(name="${config.db.slave.fetchMode}", env="${DB_SLAVE_FETCH_MODE}")
* @var bool
*/
protected $fetchMode = true;
}
17 changes: 17 additions & 0 deletions test/Cases/Mysql/SqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,21 @@ public function testTableNameIsDbKeywordByCo()
$this->testTableNameIsDbKeyword();
});
}

public function testSqlQueryStrictType()
{
$result = Db::query('SELECT * FROM user LIMIT 1;', [], 'other')->getResult();
$id = $result[0]['id'];
$name = $result[0]['name'];

$this->assertTrue(is_int($id));
$this->assertTrue(is_string($name));
}

public function testSqlQueryStrictTypeByCo()
{
go(function () {
$this->testSqlQueryStrictType();
});
}
}
8 changes: 8 additions & 0 deletions test/Cases/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use SwoftTest\Db\Testing\Pool\DbPptPoolConfig;
use SwoftTest\Db\Testing\Pool\DbSlaveEnvPoolConfig;
use SwoftTest\Db\Testing\Pool\DbSlavePptConfig;
use SwoftTest\Db\Testing\Pool\OtherDbConfig;
use SwoftTest\Db\Testing\Pool\OtherDbPool;

/**
Expand Down Expand Up @@ -91,6 +92,13 @@ public function testDbSlaveEnv()
$this->assertEquals($pConfig->getMaxWait(), 10);
}

public function testOtherConfig()
{
$config = bean(OtherDbConfig::class);
$this->assertTrue($config->isStrictType());
$this->assertTrue($config->isFetchMode());
}

public function testMaxIdleTime()
{
$pool = App::getPool('idle.master');
Expand Down
6 changes: 6 additions & 0 deletions test/Testing/Pool/OtherDbConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ class OtherDbConfig extends DbPoolProperties
* @var string
*/
protected $driver = Driver::MYSQL;

/**
* @Value(name="${config.db.other.master.strictType}", env="${DB_OTHER_STRICT_TYPE}")
* @var bool
*/
protected $strictType = false;
}
6 changes: 6 additions & 0 deletions test/Testing/Pool/OtherDbSlaveConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ class OtherDbSlaveConfig extends DbPoolProperties
* @var string
*/
protected $driver = Driver::MYSQL;

/**
* @Value(name="${config.db.other.slave.strictType}", env="${DB_OTHER_SLAVE_STRICT_TYPE}")
* @var bool
*/
protected $strictType = false;
}
2 changes: 2 additions & 0 deletions test/config/properties/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'maxActive' => 1,
'maxWait' => 1,
'timeout' => 1,
'strictType' => true,
],

'slave' => [
Expand All @@ -61,6 +62,7 @@
'maxActive' => 1,
'maxWait' => 1,
'timeout' => 1,
'strictType' => true,
],
],
];

0 comments on commit bd44c19

Please sign in to comment.