Skip to content

Commit bd44c19

Browse files
limingxinleoswoft-bot
authored andcommitted
feat: Added strict mode and fetch mode for Mysql client (swoft-cloud/swoft-component#208)
* 增加DB严格模式配置 * 增加配置读取单测 * Update SqlTest.php * Update DbPoolProperties.php * Remoe useless comments.
1 parent a61928c commit bd44c19

File tree

10 files changed

+118
-16
lines changed

10 files changed

+118
-16
lines changed

src/Driver/Mysql/MysqlConnection.php

+22-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Swoft\Db\AbstractDbConnection;
1515
use Swoft\Db\Bean\Annotation\Connection;
1616
use Swoft\Db\Exception\MysqlException;
17+
use Swoft\Db\Pool\Config\DbPoolProperties;
1718
use Swoole\Coroutine\Mysql;
1819

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

63+
/** @var DbPoolProperties $config */
64+
$config = $this->pool->getPoolConfig();
65+
$strictType = $config->isStrictType();
66+
$fetchMode = $config->isFetchMode();
67+
68+
$serverConfig = [
69+
'host' => $options['host'],
70+
'port' => $options['port'],
71+
'user' => $options['user'],
72+
'password' => $options['password'],
73+
'database' => $options['database'],
74+
'timeout' => $options['timeout'],
75+
'charset' => $options['charset'],
76+
'strict_type' => $strictType,
77+
];
78+
79+
if (version_compare(swoole_version(), '4.0', '>=')) {
80+
$serverConfig['fetch_mode'] = $fetchMode;
81+
}
82+
6283
// init
6384
$mysql = new MySQL();
64-
$mysql->connect([
65-
'host' => $options['host'],
66-
'port' => $options['port'],
67-
'user' => $options['user'],
68-
'password' => $options['password'],
69-
'database' => $options['database'],
70-
'timeout' => $options['timeout'],
71-
'charset' => $options['charset'],
72-
]);
85+
$mysql->connect($serverConfig);
7386

7487
// error
7588
if ($mysql->connected === false) {

src/Driver/Mysql/SyncMysqlConnection.php

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Swoft\Db\Bean\Annotation\Connection;
1616
use Swoft\Db\Driver\DriverType;
1717
use Swoft\Db\Exception\MysqlException;
18+
use Swoft\Db\Pool\Config\DbPoolProperties;
1819

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

51+
/** @var DbPoolProperties $config */
52+
$config = $this->pool->getPoolConfig();
53+
5054
$user = $options['user'];
5155
$passwd = $options['password'];
5256
$host = $options['host'];
@@ -60,6 +64,12 @@ public function createConnection()
6064
\PDO::ATTR_TIMEOUT => $timeout,
6165
\PDO::ATTR_PERSISTENT => true,
6266
];
67+
68+
if ($config->isStrictType()) {
69+
$pdoOptions[\PDO::ATTR_STRINGIFY_FETCHES] = false;
70+
$pdoOptions[\PDO::ATTR_EMULATE_PREPARES] = false;
71+
}
72+
6373
$dsn = "mysql:host=$host;port=$port;dbname=$dbName;charset=$charset";
6474
$this->connection = new \PDO($dsn, $user, $passwd, $pdoOptions);
6575
$this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

src/Pool/Config/DbPoolConfig.php

+12
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,16 @@ class DbPoolConfig extends DbPoolProperties
108108
* @var string
109109
*/
110110
protected $driver = Driver::MYSQL;
111+
112+
/**
113+
* @Value(name="${config.db.master.strictType}", env="${DB_STRICT_TYPE}")
114+
* @var bool
115+
*/
116+
protected $strictType = false;
117+
118+
/**
119+
* @Value(name="${config.db.master.fetchMode}", env="${DB_FETCH_MODE}")
120+
* @var bool
121+
*/
122+
protected $fetchMode = true;
111123
}

src/Pool/Config/DbPoolProperties.php

+23-7
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414

1515
/**
1616
* The pool properties of database
17-
*
18-
* @uses DbPoolProperties
19-
* @version 2018年01月27日
20-
* @author stelin <phpcrazy@126.com>
21-
* @copyright Copyright 2010-2016 swoft software
22-
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
2317
*/
2418
class DbPoolProperties extends PoolProperties
2519
{
@@ -31,10 +25,32 @@ class DbPoolProperties extends PoolProperties
3125
protected $driver = Driver::MYSQL;
3226

3327
/**
34-
* @return string
28+
* 开启严格模式,返回的字段将自动转为数字类型
29+
*
30+
* @var bool
3531
*/
32+
protected $strictType = false;
33+
34+
/**
35+
* 开启 Fetch 模式, 可类似于 PDO 一样使用 fetch/fetchAll 逐行获取或获取全部结果集
36+
*
37+
* @since Swoole 4.0
38+
* @var bool
39+
*/
40+
protected $fetchMode = true;
41+
3642
public function getDriver(): string
3743
{
3844
return $this->driver;
3945
}
46+
47+
public function isStrictType(): bool
48+
{
49+
return $this->strictType;
50+
}
51+
52+
public function isFetchMode(): bool
53+
{
54+
return $this->fetchMode;
55+
}
4056
}

src/Pool/Config/DbSlavePoolConfig.php

+12
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,16 @@ class DbSlavePoolConfig extends DbPoolProperties
108108
* @var string
109109
*/
110110
protected $driver = Driver::MYSQL;
111+
112+
/**
113+
* @Value(name="${config.db.slave.strictType}", env="${DB_SLAVE_STRICT_TYPE}")
114+
* @var bool
115+
*/
116+
protected $strictType = false;
117+
118+
/**
119+
* @Value(name="${config.db.slave.fetchMode}", env="${DB_SLAVE_FETCH_MODE}")
120+
* @var bool
121+
*/
122+
protected $fetchMode = true;
111123
}

test/Cases/Mysql/SqlTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,21 @@ public function testTableNameIsDbKeywordByCo()
194194
$this->testTableNameIsDbKeyword();
195195
});
196196
}
197+
198+
public function testSqlQueryStrictType()
199+
{
200+
$result = Db::query('SELECT * FROM user LIMIT 1;', [], 'other')->getResult();
201+
$id = $result[0]['id'];
202+
$name = $result[0]['name'];
203+
204+
$this->assertTrue(is_int($id));
205+
$this->assertTrue(is_string($name));
206+
}
207+
208+
public function testSqlQueryStrictTypeByCo()
209+
{
210+
go(function () {
211+
$this->testSqlQueryStrictType();
212+
});
213+
}
197214
}

test/Cases/PoolTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use SwoftTest\Db\Testing\Pool\DbPptPoolConfig;
1515
use SwoftTest\Db\Testing\Pool\DbSlaveEnvPoolConfig;
1616
use SwoftTest\Db\Testing\Pool\DbSlavePptConfig;
17+
use SwoftTest\Db\Testing\Pool\OtherDbConfig;
1718
use SwoftTest\Db\Testing\Pool\OtherDbPool;
1819

1920
/**
@@ -91,6 +92,13 @@ public function testDbSlaveEnv()
9192
$this->assertEquals($pConfig->getMaxWait(), 10);
9293
}
9394

95+
public function testOtherConfig()
96+
{
97+
$config = bean(OtherDbConfig::class);
98+
$this->assertTrue($config->isStrictType());
99+
$this->assertTrue($config->isFetchMode());
100+
}
101+
94102
public function testMaxIdleTime()
95103
{
96104
$pool = App::getPool('idle.master');

test/Testing/Pool/OtherDbConfig.php

+6
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,10 @@ class OtherDbConfig extends DbPoolProperties
8585
* @var string
8686
*/
8787
protected $driver = Driver::MYSQL;
88+
89+
/**
90+
* @Value(name="${config.db.other.master.strictType}", env="${DB_OTHER_STRICT_TYPE}")
91+
* @var bool
92+
*/
93+
protected $strictType = false;
8894
}

test/Testing/Pool/OtherDbSlaveConfig.php

+6
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,10 @@ class OtherDbSlaveConfig extends DbPoolProperties
8585
* @var string
8686
*/
8787
protected $driver = Driver::MYSQL;
88+
89+
/**
90+
* @Value(name="${config.db.other.slave.strictType}", env="${DB_OTHER_SLAVE_STRICT_TYPE}")
91+
* @var bool
92+
*/
93+
protected $strictType = false;
8894
}

test/config/properties/db.php

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
'maxActive' => 1,
5050
'maxWait' => 1,
5151
'timeout' => 1,
52+
'strictType' => true,
5253
],
5354

5455
'slave' => [
@@ -61,6 +62,7 @@
6162
'maxActive' => 1,
6263
'maxWait' => 1,
6364
'timeout' => 1,
65+
'strictType' => true,
6466
],
6567
],
6668
];

0 commit comments

Comments
 (0)