Skip to content

Commit cdff6fa

Browse files
committed
Add MysqlConfig::fromAuthority()
Using the new function to cleanup examples and test case creation of a MysqlConfig object.
1 parent eaad4a3 commit cdff6fa

11 files changed

+70
-40
lines changed

examples/1-connect.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88

99
use Amp\Mysql;
1010

11-
/* If you want ssl, pass as second argument an array with ssl options (an empty options array is valid too); if null is passed, ssl is not enabled either */
12-
$config = Mysql\MysqlConfig::fromString("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME);
11+
$config = Mysql\MysqlConfig::fromAuthority(DB_HOST, DB_USER, DB_PASS, DB_NAME);
1312

1413
/* use an alternative charset... Default is utf8mb4_general_ci */
15-
$config = $config->withCharset("latin1_general_ci");
14+
$config = $config->withCharset("ascii", "ascii_general_ci");
1615

1716
$db = Mysql\connect($config);
1817

examples/2-simple-query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Amp\Mysql\MysqlConfig;
66
use Amp\Mysql\MysqlPool;
77

8-
$db = new MysqlPool(MysqlConfig::fromString("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME));
8+
$db = new MysqlPool(MysqlConfig::fromAuthority(DB_HOST, DB_USER, DB_PASS, DB_NAME));
99

1010
$result = $db->query("SELECT 1 AS value");
1111

examples/3-generic-with-yield.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Amp\Mysql\MysqlPool;
88
use function Amp\async;
99

10-
$db = new MysqlPool(MysqlConfig::fromString("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME));
10+
$db = new MysqlPool(MysqlConfig::fromAuthority(DB_HOST, DB_USER, DB_PASS, DB_NAME));
1111

1212
$db->query("DROP TABLE IF EXISTS tmp");
1313

examples/4-multi-rows.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Amp\Mysql\MysqlResult;
1010
use function Amp\async;
1111

12-
$db = new MysqlPool(MysqlConfig::fromString("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME));
12+
$db = new MysqlPool(MysqlConfig::fromAuthority(DB_HOST, DB_USER, DB_PASS, DB_NAME));
1313

1414
/* create same table than in 3-generic-with-yield.php */
1515
createGenericTable($db);

examples/5-multi-stmts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Amp\Mysql\MysqlConfig;
77
use Amp\Mysql\MysqlPool;
88

9-
$db = new MysqlPool(MysqlConfig::fromString("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME));
9+
$db = new MysqlPool(MysqlConfig::fromAuthority(DB_HOST, DB_USER, DB_PASS, DB_NAME));
1010

1111
/* create same table than in 3-generic-with-yield.php */
1212
createGenericTable($db);

examples/6-transaction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Amp\Mysql\MysqlConfig;
77
use Amp\Mysql\MysqlPool;
88

9-
$db = new MysqlPool(MysqlConfig::fromString("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME));
9+
$db = new MysqlPool(MysqlConfig::fromAuthority(DB_HOST, DB_USER, DB_PASS, DB_NAME));
1010

1111
/* create same table than in 3-generic-with-yield.php */
1212
createGenericTable($db);

src/Internal/ConnectionProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@ public function connect(?Cancellation $cancellation = null): void
192192
if ($this->config->getCharset() !== MysqlConfig::DEFAULT_CHARSET
193193
|| $this->config->getCollation() !== MysqlConfig::DEFAULT_COLLATE
194194
) {
195-
$future = $future->map(function () {
195+
$future = $future->map(function (): void {
196196
$charset = $this->config->getCharset();
197197
$collate = $this->config->getCollation();
198198

199-
$this->query("SET NAMES '$charset'" . ($collate === "" ? "" : " COLLATE '$collate'"));
199+
$this->query("SET NAMES '$charset'" . ($collate === "" ? "" : " COLLATE '$collate'"))->await();
200200
});
201201
}
202202

src/MysqlConfig.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,32 @@ public static function fromString(string $connectionString, ConnectContext $cont
6060
);
6161
}
6262

63+
public static function fromAuthority(
64+
string $authority,
65+
string $user,
66+
string $password,
67+
?string $database = null,
68+
?ConnectContext $context = null
69+
): self {
70+
[$host, $port] = \explode(':', $authority, 2) + ['', (string) self::DEFAULT_PORT];
71+
72+
return new self(
73+
host: $host,
74+
port: (int) $port,
75+
user: $user,
76+
password: $password,
77+
database: $database,
78+
context: $context,
79+
);
80+
}
81+
6382
public function __construct(
6483
string $host,
6584
int $port = self::DEFAULT_PORT,
66-
string $user = null,
67-
string $password = null,
68-
string $database = null,
69-
ConnectContext $context = null,
85+
?string $user = null,
86+
?string $password = null,
87+
?string $database = null,
88+
?ConnectContext $context = null,
7089
string $charset = self::DEFAULT_CHARSET,
7190
string $collate = self::DEFAULT_COLLATE,
7291
bool $useCompression = false,
@@ -85,7 +104,9 @@ public function __construct(
85104

86105
public function getConnectionString(): string
87106
{
88-
return $this->getHost()[0] == "/" ? 'unix://' . $this->getHost() : 'tcp://' . $this->getHost() . ':' . $this->getPort();
107+
return $this->getHost()[0] == "/"
108+
? 'unix://' . $this->getHost()
109+
: 'tcp://' . $this->getHost() . ':' . $this->getPort();
89110
}
90111

91112
public function isCompressionEnabled(): bool
@@ -148,7 +169,7 @@ public function getCollation(): string
148169
return $this->collate;
149170
}
150171

151-
public function withCharset(string $charset = self::DEFAULT_CHARSET, string $collate = self::DEFAULT_COLLATE): self
172+
public function withCharset(string $charset, string $collate): self
152173
{
153174
$new = clone $this;
154175
$new->charset = $charset;

test/ConnectionTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@
33
namespace Amp\Mysql\Test;
44

55
use Amp\Mysql\DefaultMysqlConnector;
6-
use Amp\Mysql\MysqlConfig;
76
use Amp\Mysql\MysqlConnection;
87
use Amp\Mysql\MysqlLink;
98

109
class ConnectionTest extends LinkTest
1110
{
12-
protected function getLink(string $connectionString): MysqlLink
11+
protected function getLink(bool $useCompression = false): MysqlLink
1312
{
14-
return (new DefaultMysqlConnector)->connect(MysqlConfig::fromString($connectionString));
13+
return (new DefaultMysqlConnector)->connect($this->getConfig($useCompression));
1514
}
1615

1716
public function testConnect()
1817
{
1918
$connector = new DefaultMysqlConnector();
2019

21-
$db = $connector->connect(MysqlConfig::fromString("host=".DB_HOST." user=".DB_USER." pass=".DB_PASS." db=test"));
20+
$db = $connector->connect($this->getConfig());
2221

2322
$this->assertInstanceOf(MysqlConnection::class, $db);
2423

@@ -30,7 +29,7 @@ public function testConnect()
3029

3130
public function testDoubleClose()
3231
{
33-
$db = $this->getLink("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=test");
32+
$db = $this->getLink();
3433

3534
$db->close();
3635

test/LinkTest.php

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Amp\Mysql\Test;
44

55
use Amp\Mysql\MysqlColumnDefinition;
6+
use Amp\Mysql\MysqlConfig;
67
use Amp\Mysql\MysqlDataType;
78
use Amp\Mysql\MysqlLink;
89
use Amp\Mysql\MysqlResult;
@@ -14,11 +15,21 @@ abstract class LinkTest extends AsyncTestCase
1415
/**
1516
* Returns the Link class to be tested.
1617
*/
17-
abstract protected function getLink(string $connectionString): MysqlLink;
18+
abstract protected function getLink(bool $useCompression = false): MysqlLink;
19+
20+
protected function getConfig(bool $useCompression = false): MysqlConfig
21+
{
22+
$config = MysqlConfig::fromAuthority(DB_HOST, DB_USER, DB_PASS, 'test');
23+
if ($useCompression) {
24+
$config = $config->withCompression();
25+
}
26+
27+
return $config;
28+
}
1829

1930
public function testQuery()
2031
{
21-
$db = $this->getLink("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=test");
32+
$db = $this->getLink();
2233

2334
$resultset = $db->execute("SELECT ? AS a", [M_PI]);
2435
$this->assertInstanceOf(MysqlResult::class, $resultset);
@@ -34,7 +45,7 @@ public function testQuery()
3445

3546
public function testQueryFetchRow()
3647
{
37-
$db = $this->getLink("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=test");
48+
$db = $this->getLink();
3849

3950
$resultset = $db->query('SELECT a FROM main WHERE a < 4');
4051
$this->assertInstanceOf(MysqlResult::class, $resultset);
@@ -54,14 +65,14 @@ public function testQueryWithInvalidQuery()
5465
$this->expectException(QueryError::class);
5566
$this->expectExceptionMessage('You have an error in your SQL syntax');
5667

57-
$db = $this->getLink("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=test");
68+
$db = $this->getLink();
5869

5970
$db->query("SELECT & FROM main WHERE a = 1");
6071
}
6172

6273
public function testMultiStmt()
6374
{
64-
$db = $this->getLink("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=test;useCompression=true");
75+
$db = $this->getLink(true);
6576

6677
$resultset = $db->query("SELECT a FROM main; SELECT b FROM main WHERE a = 5; SELECT b AS d, a + 1 AS c FROM main WHERE b > 4");
6778
$this->assertInstanceOf(MysqlResult::class, $resultset);
@@ -100,7 +111,7 @@ public function testMultiStmt()
100111

101112
public function testPrepared()
102113
{
103-
$db = $this->getLink("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=test;useCompression=true");
114+
$db = $this->getLink(true);
104115

105116
$stmt = $db->prepare("SELECT id as no, a, b FROM main as table_alias WHERE a = ? OR b = :num");
106117
$base = [
@@ -168,7 +179,7 @@ public function testPrepareWithInvalidQuery()
168179
$this->expectException(QueryError::class);
169180
$this->expectExceptionMessage('You have an error in your SQL syntax');
170181

171-
$db = $this->getLink("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=test");
182+
$db = $this->getLink();
172183

173184
$statement = $db->prepare("SELECT & FROM main WHERE a = ?");
174185

@@ -180,7 +191,7 @@ public function testBindWithInvalidParamId()
180191
$this->expectException(\Error::class);
181192
$this->expectExceptionMessage('Parameter 1 is not defined for this prepared statement');
182193

183-
$db = $this->getLink("host=" . DB_HOST . ";user=" . DB_USER . ";pass=" . DB_PASS . ";db=test");
194+
$db = $this->getLink();
184195

185196
$statement = $db->prepare("SELECT * FROM main WHERE a = ?");
186197

@@ -194,7 +205,7 @@ public function testBindWithInvalidParamName()
194205
$this->expectException(\Error::class);
195206
$this->expectExceptionMessage('Parameter :b is not defined for this prepared statement');
196207

197-
$db = $this->getLink("host=" . DB_HOST . ";user=" . DB_USER . ";pass=" . DB_PASS . ";db=test");
208+
$db = $this->getLink();
198209

199210
$statement = $db->prepare("SELECT * FROM main WHERE a = :a");
200211

@@ -208,15 +219,15 @@ public function testStatementExecuteWithTooFewParams()
208219
$this->expectException(\Error::class);
209220
$this->expectExceptionMessage('Parameter 1 for prepared statement missing');
210221

211-
$db = $this->getLink("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=test");
222+
$db = $this->getLink();
212223

213224
$stmt = $db->prepare("SELECT * FROM main WHERE a = ? AND b = ?");
214225
$stmt->execute([1]);
215226
}
216227

217228
public function testExecute()
218229
{
219-
$db = $this->getLink("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=test");
230+
$db = $this->getLink();
220231

221232
$result = $db->execute("SELECT * FROM test.main WHERE a = ? OR b = ?", [2, 5]);
222233
$this->assertInstanceOf(MysqlResult::class, $result);
@@ -240,7 +251,7 @@ public function testExecuteWithInvalidQuery()
240251
$this->expectException(QueryError::class);
241252
$this->expectExceptionMessage('You have an error in your SQL syntax');
242253

243-
$db = $this->getLink("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=test");
254+
$db = $this->getLink();
244255

245256
$db->execute("SELECT & FROM main WHERE a = ?", [1]);
246257

@@ -252,7 +263,7 @@ public function testExecuteWithTooFewParams()
252263
$this->expectException(\Error::class);
253264
$this->expectExceptionMessage('Parameter 1 for prepared statement missing');
254265

255-
$db = $this->getLink("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=test");
266+
$db = $this->getLink();
256267

257268
$db->execute("SELECT * FROM main WHERE a = ? AND b = ?", [1]);
258269

@@ -261,7 +272,7 @@ public function testExecuteWithTooFewParams()
261272

262273
public function testPreparedWithNegativeValue()
263274
{
264-
$db = $this->getLink("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=test");
275+
$db = $this->getLink();
265276

266277
$db->query("DROP TABLE IF EXISTS tmp");
267278

@@ -279,7 +290,7 @@ public function testPreparedWithNegativeValue()
279290

280291
public function testTransaction()
281292
{
282-
$db = $this->getLink("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=test");
293+
$db = $this->getLink();
283294

284295
$transaction = $db->beginTransaction();
285296

@@ -324,7 +335,7 @@ public function testTransaction()
324335
*/
325336
public function testInsertSelect()
326337
{
327-
$db = $this->getLink("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=test");
338+
$db = $this->getLink();
328339

329340
$a = 1;
330341

@@ -350,7 +361,7 @@ public function testInsertSelect()
350361

351362
public function testJsonDecoding()
352363
{
353-
$db = $this->getLink("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=test");
364+
$db = $this->getLink();
354365

355366
$result = $db->execute("SELECT a FROM test.json");
356367

0 commit comments

Comments
 (0)