@@ -24,13 +24,13 @@ It is written in pure PHP and does not require any extensions.
24
24
* [ Usage] ( #usage )
25
25
* [ MysqlClient] ( #mysqlclient )
26
26
* [ __ construct()] ( #__construct )
27
- * [ ConnectionInterface] ( #connectioninterface )
28
27
* [ query()] ( #query )
29
28
* [ queryStream()] ( #querystream )
30
29
* [ ping()] ( #ping )
31
30
* [ quit()] ( #quit )
32
31
* [ close()] ( #close )
33
- * [ Events] ( #events )
32
+ * [ error event] ( #error-event )
33
+ * [ close event] ( #close-event )
34
34
* [ Install] ( #install )
35
35
* [ Tests] ( #tests )
36
36
* [ License] ( #license )
@@ -68,21 +68,21 @@ The `MysqlClient` is responsible for exchanging messages with your MySQL server
68
68
and keeps track of pending queries.
69
69
70
70
``` php
71
- $connection = new React\MySQL\MysqlClient($uri);
71
+ $mysql = new React\MySQL\MysqlClient($uri);
72
72
73
- $connection ->query(…);
73
+ $mysql ->query(…);
74
74
```
75
75
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.
86
86
87
87
From a consumer side this means that you can start sending queries to the
88
88
database right away while the underlying connection may still be
@@ -100,7 +100,7 @@ longer than the idle period.
100
100
Note that creating the underlying connection will be deferred until the
101
101
first request is invoked. Accordingly, any eventual connection issues
102
102
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
104
104
and no further commands can be enqueued. Similarly, calling ` quit() ` on
105
105
this instance when not currently connected will succeed immediately and
106
106
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
200
200
This value SHOULD NOT be given unless you're sure you want to explicitly use a
201
201
given event loop instance.
202
202
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
-
209
203
#### query()
210
204
211
205
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
218
212
previous queries are completed.
219
213
220
214
``` 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)');
223
217
```
224
218
225
219
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
231
225
[ ` queryStream() ` ] ( #querystream ) method instead.
232
226
233
227
``` php
234
- $connection ->query($query)->then(function (QueryResult $command) {
228
+ $mysql ->query($query)->then(function (QueryResult $command) {
235
229
if (isset($command->resultRows)) {
236
230
// this is a response to a SELECT etc. with some rows (0+)
237
231
print_r($command->resultFields);
@@ -254,7 +248,7 @@ You can optionally pass an array of `$params` that will be bound to the
254
248
query like this:
255
249
256
250
``` php
257
- $connection ->query('SELECT * FROM user WHERE id > ?', [$id]);
251
+ $mysql ->query('SELECT * FROM user WHERE id > ?', [$id]);
258
252
```
259
253
260
254
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
275
269
hundreds of rows, you may want to use the [ ` query() ` ] ( #query ) method instead.
276
270
277
271
``` php
278
- $stream = $connection ->queryStream('SELECT * FROM user');
272
+ $stream = $mysql ->queryStream('SELECT * FROM user');
279
273
$stream->on('data', function ($row) {
280
274
echo $row['name'] . PHP_EOL;
281
275
});
@@ -288,7 +282,7 @@ You can optionally pass an array of `$params` that will be bound to the
288
282
query like this:
289
283
290
284
``` php
291
- $stream = $connection ->queryStream('SELECT * FROM user WHERE id > ?', [$id]);
285
+ $stream = $mysql ->queryStream('SELECT * FROM user WHERE id > ?', [$id]);
292
286
```
293
287
294
288
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
303
297
like this:
304
298
305
299
``` php
306
- $connection ->queryStream('SELECT * FROM user')->pipe($formatter)->pipe($logger);
300
+ $mysql ->queryStream('SELECT * FROM user')->pipe($formatter)->pipe($logger);
307
301
```
308
302
309
303
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
331
325
previous queries are completed.
332
326
333
327
``` php
334
- $connection ->ping()->then(function () {
328
+ $mysql ->ping()->then(function () {
335
329
echo 'OK' . PHP_EOL;
336
330
}, function (Exception $e) {
337
331
echo 'Error: ' . $e->getMessage() . PHP_EOL;
@@ -350,8 +344,8 @@ and outstanding commands will be put into a queue to be executed once the
350
344
previous commands are completed.
351
345
352
346
``` php
353
- $connection ->query('CREATE TABLE test ...');
354
- $connection ->quit();
347
+ $mysql ->query('CREATE TABLE test ...');
348
+ $mysql ->quit();
355
349
```
356
350
357
351
#### close()
@@ -363,26 +357,21 @@ Unlike the `quit()` method, this method will immediately force-close the
363
357
connection and reject all outstanding commands.
364
358
365
359
``` php
366
- $connection ->close();
360
+ $mysql ->close();
367
361
```
368
362
369
363
Forcefully closing the connection will yield a warning in the server logs
370
364
and should generally only be used as a last resort. See also
371
365
[ ` quit() ` ] ( #quit ) as a safe alternative.
372
366
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
379
368
380
369
The ` error ` event will be emitted once a fatal error occurs, such as
381
370
when the connection is lost or is invalid.
382
371
The event receives a single ` Exception ` argument for the error instance.
383
372
384
373
``` php
385
- $connection ->on('error', function (Exception $e) {
374
+ $mysql ->on('error', function (Exception $e) {
386
375
echo 'Error: ' . $e->getMessage() . PHP_EOL;
387
376
});
388
377
```
@@ -391,12 +380,12 @@ This event will only be triggered for fatal errors and will be followed
391
380
by closing the connection. It is not to be confused with "soft" errors
392
381
caused by invalid SQL queries.
393
382
394
- ##### close event
383
+ #### close event
395
384
396
385
The ` close ` event will be emitted once the connection closes (terminates).
397
386
398
387
``` php
399
- $connection ->on('close', function () {
388
+ $mysql ->on('close', function () {
400
389
echo 'Connection closed' . PHP_EOL;
401
390
});
402
391
```
0 commit comments