Skip to content

Commit c4dc415

Browse files
committed
Simplify API, remove ConnectionInterface
1 parent 9a3976b commit c4dc415

File tree

8 files changed

+282
-313
lines changed

8 files changed

+282
-313
lines changed

README.md

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ It is written in pure PHP and does not require any extensions.
2424
* [Usage](#usage)
2525
* [MysqlClient](#mysqlclient)
2626
* [__construct()](#__construct)
27-
* [ConnectionInterface](#connectioninterface)
2827
* [query()](#query)
2928
* [queryStream()](#querystream)
3029
* [ping()](#ping)
3130
* [quit()](#quit)
3231
* [close()](#close)
33-
* [Events](#events)
32+
* [error event](#error-event)
33+
* [close event](#close-event)
3434
* [Install](#install)
3535
* [Tests](#tests)
3636
* [License](#license)
@@ -68,21 +68,21 @@ The `MysqlClient` is responsible for exchanging messages with your MySQL server
6868
and keeps track of pending queries.
6969

7070
```php
71-
$connection = new React\MySQL\MysqlClient($uri);
71+
$mysql = new React\MySQL\MysqlClient($uri);
7272

73-
$connection->query(…);
73+
$mysql->query(…);
7474
```
7575

76-
This method immediately returns a "virtual" connection implementing the
77-
[`ConnectionInterface`](#connectioninterface) that can be used to
78-
interface with your MySQL database. Internally, it lazily creates the
79-
underlying database connection only on demand once the first request is
80-
invoked on this instance and will queue all outstanding requests until
81-
the underlying connection is ready. This underlying connection will be
82-
reused for all requests until it is closed. By default, idle connections
83-
will be held open for 1ms (0.001s) when not used. The next request will
84-
either reuse the existing connection or will automatically create a new
85-
underlying connection if this idle time is expired.
76+
This class represents a connection that is responsible for communicating
77+
with your MySQL server instance, managing the connection state and sending
78+
your database queries. Internally, it creates the underlying database
79+
connection only on demand once the first request is invoked on this
80+
instance and will queue all outstanding requests until the underlying
81+
connection is ready. This underlying connection will be reused for all
82+
requests until it is closed. By default, idle connections will be held
83+
open for 1ms (0.001s) when not used. The next request will either reuse
84+
the existing connection or will automatically create a new underlying
85+
connection if this idle time is expired.
8686

8787
From a consumer side this means that you can start sending queries to the
8888
database right away while the underlying connection may still be
@@ -100,7 +100,7 @@ longer than the idle period.
100100
Note that creating the underlying connection will be deferred until the
101101
first request is invoked. Accordingly, any eventual connection issues
102102
will be detected once this instance is first used. You can use the
103-
`quit()` method to ensure that the "virtual" connection will be soft-closed
103+
`quit()` method to ensure that the connection will be soft-closed
104104
and no further commands can be enqueued. Similarly, calling `quit()` on
105105
this instance when not currently connected will succeed immediately and
106106
will not have to wait for an actual underlying connection.
@@ -200,12 +200,6 @@ here in order to use the [default loop](https://github.com/reactphp/event-loop#l
200200
This value SHOULD NOT be given unless you're sure you want to explicitly use a
201201
given event loop instance.
202202

203-
### ConnectionInterface
204-
205-
The `ConnectionInterface` represents a connection that is responsible for
206-
communicating with your MySQL server instance, managing the connection state
207-
and sending your database queries.
208-
209203
#### query()
210204

211205
The `query(string $query, array $params = []): PromiseInterface<QueryResult>` method can be used to
@@ -218,8 +212,8 @@ and outstanding queries will be put into a queue to be executed once the
218212
previous queries are completed.
219213

220214
```php
221-
$connection->query('CREATE TABLE test ...');
222-
$connection->query('INSERT INTO test (id) VALUES (1)');
215+
$mysql->query('CREATE TABLE test ...');
216+
$mysql->query('INSERT INTO test (id) VALUES (1)');
223217
```
224218

225219
If this SQL statement returns a result set (such as from a `SELECT`
@@ -231,7 +225,7 @@ unknown or known to be too large to fit into memory, you should use the
231225
[`queryStream()`](#querystream) method instead.
232226

233227
```php
234-
$connection->query($query)->then(function (QueryResult $command) {
228+
$mysql->query($query)->then(function (QueryResult $command) {
235229
if (isset($command->resultRows)) {
236230
// this is a response to a SELECT etc. with some rows (0+)
237231
print_r($command->resultFields);
@@ -254,7 +248,7 @@ You can optionally pass an array of `$params` that will be bound to the
254248
query like this:
255249

256250
```php
257-
$connection->query('SELECT * FROM user WHERE id > ?', [$id]);
251+
$mysql->query('SELECT * FROM user WHERE id > ?', [$id]);
258252
```
259253

260254
The given `$sql` parameter MUST contain a single statement. Support
@@ -275,7 +269,7 @@ into memory. If you know your result set to not exceed a few dozens or
275269
hundreds of rows, you may want to use the [`query()`](#query) method instead.
276270

277271
```php
278-
$stream = $connection->queryStream('SELECT * FROM user');
272+
$stream = $mysql->queryStream('SELECT * FROM user');
279273
$stream->on('data', function ($row) {
280274
echo $row['name'] . PHP_EOL;
281275
});
@@ -288,7 +282,7 @@ You can optionally pass an array of `$params` that will be bound to the
288282
query like this:
289283

290284
```php
291-
$stream = $connection->queryStream('SELECT * FROM user WHERE id > ?', [$id]);
285+
$stream = $mysql->queryStream('SELECT * FROM user WHERE id > ?', [$id]);
292286
```
293287

294288
This method is specifically designed for queries that return a result set
@@ -303,7 +297,7 @@ rows to a [`WritableStreamInterface`](https://github.com/reactphp/stream#writabl
303297
like this:
304298

305299
```php
306-
$connection->queryStream('SELECT * FROM user')->pipe($formatter)->pipe($logger);
300+
$mysql->queryStream('SELECT * FROM user')->pipe($formatter)->pipe($logger);
307301
```
308302

309303
Note that as per the underlying stream definition, calling `pause()` and
@@ -331,7 +325,7 @@ and outstanding command will be put into a queue to be executed once the
331325
previous queries are completed.
332326

333327
```php
334-
$connection->ping()->then(function () {
328+
$mysql->ping()->then(function () {
335329
echo 'OK' . PHP_EOL;
336330
}, function (Exception $e) {
337331
echo 'Error: ' . $e->getMessage() . PHP_EOL;
@@ -350,8 +344,8 @@ and outstanding commands will be put into a queue to be executed once the
350344
previous commands are completed.
351345

352346
```php
353-
$connection->query('CREATE TABLE test ...');
354-
$connection->quit();
347+
$mysql->query('CREATE TABLE test ...');
348+
$mysql->quit();
355349
```
356350

357351
#### close()
@@ -363,26 +357,21 @@ Unlike the `quit()` method, this method will immediately force-close the
363357
connection and reject all outstanding commands.
364358

365359
```php
366-
$connection->close();
360+
$mysql->close();
367361
```
368362

369363
Forcefully closing the connection will yield a warning in the server logs
370364
and should generally only be used as a last resort. See also
371365
[`quit()`](#quit) as a safe alternative.
372366

373-
#### Events
374-
375-
Besides defining a few methods, this interface also implements the
376-
`EventEmitterInterface` which allows you to react to certain events:
377-
378-
##### error event
367+
#### error event
379368

380369
The `error` event will be emitted once a fatal error occurs, such as
381370
when the connection is lost or is invalid.
382371
The event receives a single `Exception` argument for the error instance.
383372

384373
```php
385-
$connection->on('error', function (Exception $e) {
374+
$mysql->on('error', function (Exception $e) {
386375
echo 'Error: ' . $e->getMessage() . PHP_EOL;
387376
});
388377
```
@@ -391,12 +380,12 @@ This event will only be triggered for fatal errors and will be followed
391380
by closing the connection. It is not to be confused with "soft" errors
392381
caused by invalid SQL queries.
393382

394-
##### close event
383+
#### close event
395384

396385
The `close` event will be emitted once the connection closes (terminates).
397386

398387
```php
399-
$connection->on('close', function () {
388+
$mysql->on('close', function () {
400389
echo 'Connection closed' . PHP_EOL;
401390
});
402391
```

0 commit comments

Comments
 (0)