Skip to content

Commit 9e01898

Browse files
authored
Merge pull request #186 from clue-labs/mysqlclient
Simplify API, add new `MysqlClient` and remove `Factory` and `ConnectionInterface`
2 parents 6209d11 + c4dc415 commit 9e01898

15 files changed

+1118
-1151
lines changed

README.md

+73-187
Large diffs are not rendered by default.

examples/01-query.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
// $ php examples/01-query.php
44
// $ MYSQL_URI=test:test@localhost/test php examples/01-query.php "SELECT * FROM book"
55

6-
use React\MySQL\Factory;
7-
use React\MySQL\QueryResult;
8-
96
require __DIR__ . '/../vendor/autoload.php';
107

11-
$factory = new Factory();
12-
$connection = $factory->createLazyConnection(getenv('MYSQL_URI') ?: 'test:test@localhost/test');
8+
$mysql = new React\MySQL\MysqlClient(getenv('MYSQL_URI') ?: 'test:test@localhost/test');
139

1410
$query = isset($argv[1]) ? $argv[1] : 'select * from book';
15-
$connection->query($query)->then(function (QueryResult $command) {
11+
$mysql->query($query)->then(function (React\MySQL\QueryResult $command) {
1612
if (isset($command->resultRows)) {
1713
// this is a response to a SELECT etc. with some rows (0+)
1814
print_r($command->resultFields);

examples/02-query-stream.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
// $ php examples/02-query-stream.php "SHOW VARIABLES"
44
// $ MYSQL_URI=test:test@localhost/test php examples/02-query-stream.php "SELECT * FROM book"
55

6-
use React\MySQL\Factory;
7-
86
require __DIR__ . '/../vendor/autoload.php';
97

10-
$factory = new Factory();
11-
$connection = $factory->createLazyConnection(getenv('MYSQL_URI') ?: 'test:test@localhost/test');
8+
$mysql = new React\MySQL\MysqlClient(getenv('MYSQL_URI') ?: 'test:test@localhost/test');
129

1310
$query = isset($argv[1]) ? $argv[1] : 'select * from book';
14-
$stream = $connection->queryStream($query);
11+
$stream = $mysql->queryStream($query);
1512

1613
$stream->on('data', function ($row) {
1714
echo json_encode($row, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;

examples/11-interactive.php

+56-70
Original file line numberDiff line numberDiff line change
@@ -3,87 +3,73 @@
33
// $ php examples/11-interactive.php
44
// $ MYSQL_URI=test:test@localhost/test php examples/11-interactive.php
55

6-
use React\MySQL\ConnectionInterface;
7-
use React\MySQL\QueryResult;
8-
use React\MySQL\Factory;
9-
use React\Stream\ReadableResourceStream;
10-
116
require __DIR__ . '/../vendor/autoload.php';
127

13-
$factory = new Factory();
14-
$uri = getenv('MYSQL_URI') ?: 'test:test@localhost/test';
8+
$mysql = new React\MySQL\MysqlClient(getenv('MYSQL_URI') ?: 'test:test@localhost/test');
159

1610
// open a STDIN stream to read keyboard input (not supported on Windows)
17-
$stdin = new ReadableResourceStream(STDIN);
18-
$stdin->pause();
19-
20-
//create a mysql connection for executing queries
21-
$factory->createConnection($uri)->then(function (ConnectionInterface $connection) use ($stdin) {
22-
echo 'Connection success.' . PHP_EOL;
23-
$stdin->resume();
11+
$stdin = new React\Stream\ReadableResourceStream(STDIN);
2412

25-
$stdin->on('data', function ($line) use ($connection) {
26-
$query = trim($line);
13+
$stdin->on('data', function ($line) use ($mysql) {
14+
$query = trim($line);
2715

28-
if ($query === '') {
29-
// skip empty commands
30-
return;
31-
}
32-
if ($query === 'exit') {
33-
// exit command should close the connection
34-
echo 'bye.' . PHP_EOL;
35-
$connection->quit();
36-
return;
37-
}
16+
if ($query === '') {
17+
// skip empty commands
18+
return;
19+
}
20+
if ($query === 'exit') {
21+
// exit command should close the connection
22+
echo 'bye.' . PHP_EOL;
23+
$mysql->quit();
24+
return;
25+
}
3826

39-
$time = microtime(true);
40-
$connection->query($query)->then(function (QueryResult $command) use ($time) {
41-
if (isset($command->resultRows)) {
42-
// this is a response to a SELECT etc. with some rows (0+)
43-
echo implode("\t", array_column($command->resultFields, 'name')) . PHP_EOL;
44-
foreach ($command->resultRows as $row) {
45-
echo implode("\t", $row) . PHP_EOL;
46-
}
47-
48-
printf(
49-
'%d row%s in set (%.03f sec)%s',
50-
count($command->resultRows),
51-
count($command->resultRows) === 1 ? '' : 's',
52-
microtime(true) - $time,
53-
PHP_EOL
54-
);
55-
} else {
56-
// this is an OK message in response to an UPDATE etc.
57-
// the insertId will only be set if this is
58-
if ($command->insertId !== 0) {
59-
var_dump('last insert ID', $command->insertId);
60-
}
27+
$time = microtime(true);
28+
$mysql->query($query)->then(function (React\MySQL\QueryResult $command) use ($time) {
29+
if (isset($command->resultRows)) {
30+
// this is a response to a SELECT etc. with some rows (0+)
31+
echo implode("\t", array_column($command->resultFields, 'name')) . PHP_EOL;
32+
foreach ($command->resultRows as $row) {
33+
echo implode("\t", $row) . PHP_EOL;
34+
}
6135

62-
printf(
63-
'Query OK, %d row%s affected (%.03f sec)%s',
64-
$command->affectedRows,
65-
$command->affectedRows === 1 ? '' : 's',
66-
microtime(true) - $time,
67-
PHP_EOL
68-
);
36+
printf(
37+
'%d row%s in set (%.03f sec)%s',
38+
count($command->resultRows),
39+
count($command->resultRows) === 1 ? '' : 's',
40+
microtime(true) - $time,
41+
PHP_EOL
42+
);
43+
} else {
44+
// this is an OK message in response to an UPDATE etc.
45+
// the insertId will only be set if this is
46+
if ($command->insertId !== 0) {
47+
var_dump('last insert ID', $command->insertId);
6948
}
70-
}, function (Exception $error) {
71-
// the query was not executed successfully
72-
echo 'Error: ' . $error->getMessage() . PHP_EOL;
73-
});
74-
});
7549

76-
// close connection when STDIN closes (EOF or CTRL+D)
77-
$stdin->on('close', function () use ($connection) {
78-
$connection->quit();
50+
printf(
51+
'Query OK, %d row%s affected (%.03f sec)%s',
52+
$command->affectedRows,
53+
$command->affectedRows === 1 ? '' : 's',
54+
microtime(true) - $time,
55+
PHP_EOL
56+
);
57+
}
58+
}, function (Exception $error) {
59+
// the query was not executed successfully
60+
echo 'Error: ' . $error->getMessage() . PHP_EOL;
7961
});
62+
});
8063

81-
// close STDIN (stop reading) when connection closes
82-
$connection->on('close', function () use ($stdin) {
83-
$stdin->close();
84-
echo 'Disconnected.' . PHP_EOL;
85-
});
86-
}, function (Exception $e) use ($stdin) {
87-
echo 'Connection error: ' . $e->getMessage() . PHP_EOL;
64+
// close connection when STDIN closes (EOF or CTRL+D)
65+
$stdin->on('close', function () use ($mysql) {
66+
$mysql->quit();
67+
});
68+
69+
// close STDIN (stop reading) when connection closes
70+
$mysql->on('close', function () use ($stdin) {
8871
$stdin->close();
72+
echo 'Disconnected.' . PHP_EOL;
8973
});
74+
75+
echo '# Entering interactive mode ready, hit CTRL-D to quit' . PHP_EOL;

examples/12-slow-stream.php

+36-36
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
// $ MYSQL_URI=test:test@localhost/test php examples/12-slow-stream.php "SELECT * FROM book"
55

66
use React\EventLoop\Loop;
7-
use React\MySQL\ConnectionInterface;
8-
use React\MySQL\Factory;
97

108
require __DIR__ . '/../vendor/autoload.php';
119

12-
$factory = new Factory();
13-
$uri = getenv('MYSQL_URI') ?: 'test:test@localhost/test';
10+
$mysql = new React\MySQL\MysqlClient(getenv('MYSQL_URI') ?: 'test:test@localhost/test');
1411

1512
$query = isset($argv[1]) ? $argv[1] : 'select * from book';
13+
$stream = $mysql->queryStream($query);
1614

17-
//create a mysql connection for executing query
18-
$factory->createConnection($uri)->then(function (ConnectionInterface $connection) use ($query) {
19-
// The protocol parser reads rather large chunked from the underlying connection
15+
$ref = new ReflectionProperty($mysql, 'connecting');
16+
$ref->setAccessible(true);
17+
$promise = $ref->getValue($mysql);
18+
assert($promise instanceof React\Promise\PromiseInterface);
19+
20+
$promise->then(function (React\MySQL\Io\Connection $connection) {
21+
// The protocol parser reads rather large chunks from the underlying connection
2022
// and as such can yield multiple (dozens to hundreds) rows from a single data
2123
// chunk. We try to artificially limit the stream chunk size here to try to
2224
// only ever read a single row so we can demonstrate throttling this stream.
@@ -28,11 +30,13 @@
2830
$ref = new ReflectionProperty($connection, 'stream');
2931
$ref->setAccessible(true);
3032
$conn = $ref->getValue($connection);
33+
assert($conn instanceof React\Socket\ConnectionInterface);
3134

3235
// access private "input" (instanceof React\Stream\DuplexStreamInterface)
3336
$ref = new ReflectionProperty($conn, 'input');
3437
$ref->setAccessible(true);
3538
$stream = $ref->getValue($conn);
39+
assert($stream instanceof React\Stream\DuplexStreamInterface);
3640

3741
// reduce private bufferSize to just a few bytes to slow things down
3842
$ref = new ReflectionProperty($stream, 'bufferSize');
@@ -41,38 +45,34 @@
4145
} catch (Exception $e) {
4246
echo 'Warning: Unable to reduce buffer size: ' . $e->getMessage() . PHP_EOL;
4347
}
48+
});
4449

45-
$stream = $connection->queryStream($query);
46-
47-
$throttle = null;
48-
$stream->on('data', function ($row) use (&$throttle, $stream) {
49-
echo json_encode($row, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
50-
51-
// simple throttle mechanism: explicitly pause the result stream and
52-
// resume it again after some time.
53-
if ($throttle === null) {
54-
$throttle = Loop::addTimer(1.0, function () use ($stream, &$throttle) {
55-
$throttle = null;
56-
$stream->resume();
57-
});
58-
$stream->pause();
59-
}
60-
});
61-
62-
$stream->on('error', function (Exception $e) {
63-
echo 'Error: ' . $e->getMessage() . PHP_EOL;
64-
});
65-
66-
$stream->on('close', function () use (&$throttle) {
67-
echo 'CLOSED' . PHP_EOL;
50+
$throttle = null;
51+
$stream->on('data', function ($row) use (&$throttle, $stream) {
52+
echo json_encode($row, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
6853

69-
if ($throttle) {
70-
Loop::cancelTimer($throttle);
54+
// simple throttle mechanism: explicitly pause the result stream and
55+
// resume it again after some time.
56+
if ($throttle === null) {
57+
$throttle = Loop::addTimer(1.0, function () use ($stream, &$throttle) {
7158
$throttle = null;
72-
}
73-
});
59+
$stream->resume();
60+
});
61+
$stream->pause();
62+
}
63+
});
7464

75-
$connection->quit();
76-
}, function (Exception $e) {
65+
$stream->on('error', function (Exception $e) {
7766
echo 'Error: ' . $e->getMessage() . PHP_EOL;
7867
});
68+
69+
$stream->on('close', function () use (&$throttle) {
70+
echo 'CLOSED' . PHP_EOL;
71+
72+
if ($throttle) {
73+
Loop::cancelTimer($throttle);
74+
$throttle = null;
75+
}
76+
});
77+
78+
$mysql->quit();

0 commit comments

Comments
 (0)