@@ -22,9 +22,8 @@ It is written in pure PHP and does not require any extensions.
22
22
23
23
* [ Quickstart example] ( #quickstart-example )
24
24
* [ Usage] ( #usage )
25
- * [ Factory] ( #factory )
26
- * [ createConnection()] ( #createconnection )
27
- * [ createLazyConnection()] ( #createlazyconnection )
25
+ * [ MysqlClient] ( #mysqlclient )
26
+ * [ __ construct()] ( #__construct )
28
27
* [ ConnectionInterface] ( #connectioninterface )
29
28
* [ query()] ( #query )
30
29
* [ queryStream()] ( #querystream )
@@ -45,11 +44,10 @@ This example runs a simple `SELECT` query and dumps all the records from a `book
45
44
46
45
require __DIR__ . '/vendor/autoload.php';
47
46
48
- $factory = new React\MySQL\Factory();
49
- $connection = $factory->createLazyConnection('user:pass@localhost/bookstore');
47
+ $mysql = new React\MySQL\MysqlClient('user:pass@localhost/bookstore');
50
48
51
- $connection ->query('SELECT * FROM book')->then(
52
- function (QueryResult $command) {
49
+ $mysql ->query('SELECT * FROM book')->then(
50
+ function (React\MySQL\ QueryResult $command) {
53
51
print_r($command->resultFields);
54
52
print_r($command->resultRows);
55
53
echo count($command->resultRows) . ' row(s) in set' . PHP_EOL;
@@ -64,137 +62,13 @@ See also the [examples](examples).
64
62
65
63
## Usage
66
64
67
- ### Factory
65
+ ### MysqlClient
68
66
69
- The ` Factory ` is responsible for creating your [ ` ConnectionInterface ` ] ( #connectioninterface ) instance.
67
+ The ` MysqlClient ` is responsible for exchanging messages with your MySQL server
68
+ and keeps track of pending queries.
70
69
71
70
``` php
72
- $factory = new React\MySQL\Factory();
73
- ```
74
-
75
- This class takes an optional ` LoopInterface|null $loop ` parameter that can be used to
76
- pass the event loop instance to use for this object. You can use a ` null ` value
77
- here in order to use the [ default loop] ( https://github.com/reactphp/event-loop#loop ) .
78
- This value SHOULD NOT be given unless you're sure you want to explicitly use a
79
- given event loop instance.
80
-
81
- If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
82
- proxy servers etc.), you can explicitly pass a custom instance of the
83
- [ ` ConnectorInterface ` ] ( https://github.com/reactphp/socket#connectorinterface ) :
84
-
85
- ``` php
86
- $connector = new React\Socket\Connector([
87
- 'dns' => '127.0.0.1',
88
- 'tcp' => [
89
- 'bindto' => '192.168.10.1:0'
90
- ],
91
- 'tls' => [
92
- 'verify_peer' => false,
93
- 'verify_peer_name' => false
94
- )
95
- ]);
96
-
97
- $factory = new React\MySQL\Factory(null, $connector);
98
- ```
99
-
100
- #### createConnection()
101
-
102
- The ` createConnection(string $url): PromiseInterface<ConnectionInterface> ` method can be used to
103
- create a new [ ` ConnectionInterface ` ] ( #connectioninterface ) .
104
-
105
- It helps with establishing a TCP/IP connection to your MySQL database
106
- and issuing the initial authentication handshake.
107
-
108
- ``` php
109
- $factory->createConnection($url)->then(
110
- function (ConnectionInterface $connection) {
111
- // client connection established (and authenticated)
112
- },
113
- function (Exception $e) {
114
- // an error occurred while trying to connect or authorize client
115
- }
116
- );
117
- ```
118
-
119
- The method returns a [ Promise] ( https://github.com/reactphp/promise ) that
120
- will resolve with a [ ` ConnectionInterface ` ] ( #connectioninterface )
121
- instance on success or will reject with an ` Exception ` if the URL is
122
- invalid or the connection or authentication fails.
123
-
124
- The returned Promise is implemented in such a way that it can be
125
- cancelled when it is still pending. Cancelling a pending promise will
126
- reject its value with an Exception and will cancel the underlying TCP/IP
127
- connection attempt and/or MySQL authentication.
128
-
129
- ``` php
130
- $promise = $factory->createConnection($url);
131
-
132
- Loop::addTimer(3.0, function () use ($promise) {
133
- $promise->cancel();
134
- });
135
- ```
136
-
137
- The ` $url ` parameter must contain the database host, optional
138
- authentication, port and database to connect to:
139
-
140
- ``` php
141
- $factory->createConnection('user:secret@localhost:3306/database');
142
- ```
143
-
144
- Note that both the username and password must be URL-encoded (percent-encoded)
145
- if they contain special characters:
146
-
147
- ``` php
148
- $user = 'he:llo';
149
- $pass = 'p@ss';
150
-
151
- $promise = $factory->createConnection(
152
- rawurlencode($user) . ':' . rawurlencode($pass) . '@localhost:3306/db'
153
- );
154
- ```
155
-
156
- You can omit the port if you're connecting to default port ` 3306 ` :
157
-
158
- ``` php
159
- $factory->createConnection('user:secret@localhost/database');
160
- ```
161
-
162
- If you do not include authentication and/or database, then this method
163
- will default to trying to connect as user ` root ` with an empty password
164
- and no database selected. This may be useful when initially setting up a
165
- database, but likely to yield an authentication error in a production system:
166
-
167
- ``` php
168
- $factory->createConnection('localhost');
169
- ```
170
-
171
- This method respects PHP's ` default_socket_timeout ` setting (default 60s)
172
- as a timeout for establishing the connection and waiting for successful
173
- authentication. You can explicitly pass a custom timeout value in seconds
174
- (or use a negative number to not apply a timeout) like this:
175
-
176
- ``` php
177
- $factory->createConnection('localhost?timeout=0.5');
178
- ```
179
-
180
- By default, the connection provides full UTF-8 support (using the
181
- ` utf8mb4 ` charset encoding). This should usually not be changed for most
182
- applications nowadays, but for legacy reasons you can change this to use
183
- a different ASCII-compatible charset encoding like this:
184
-
185
- ``` php
186
- $factory->createConnection('localhost?charset=utf8mb4');
187
- ```
188
-
189
- #### createLazyConnection()
190
-
191
- Creates a new connection.
192
-
193
- It helps with establishing a TCP/IP connection to your MySQL database
194
- and issuing the initial authentication handshake.
195
-
196
- ``` php
197
- $connection = $factory->createLazyConnection($url);
71
+ $connection = new React\MySQL\MysqlClient($uri);
198
72
199
73
$connection->query(…);
200
74
```
@@ -215,9 +89,6 @@ database right away while the underlying connection may still be
215
89
outstanding. Because creating this underlying connection may take some
216
90
time, it will enqueue all outstanding commands and will ensure that all
217
91
commands will be executed in correct order once the connection is ready.
218
- In other words, this "virtual" connection behaves just like a "real"
219
- connection as described in the ` ConnectionInterface ` and frees you from
220
- having to deal with its async resolution.
221
92
222
93
If the underlying database connection fails, it will reject all
223
94
outstanding commands and will return to the initial "idle" state. This
@@ -234,15 +105,16 @@ and no further commands can be enqueued. Similarly, calling `quit()` on
234
105
this instance when not currently connected will succeed immediately and
235
106
will not have to wait for an actual underlying connection.
236
107
237
- Depending on your particular use case, you may prefer this method or the
238
- underlying ` createConnection() ` which resolves with a promise. For many
239
- simple use cases it may be easier to create a lazy connection.
108
+ #### __ construct()
109
+
110
+ The ` new MysqlClient(string $uri, ConnectorInterface $connector = null, LoopInterface $loop = null) ` constructor can be used to
111
+ create a new ` MysqlClient ` instance.
240
112
241
- The ` $url ` parameter must contain the database host, optional
113
+ The ` $uri ` parameter must contain the database host, optional
242
114
authentication, port and database to connect to:
243
115
244
116
``` php
245
- $factory->createLazyConnection ('user:secret@localhost:3306/database');
117
+ $mysql = new React\MySQL\MysqlClient ('user:secret@localhost:3306/database');
246
118
```
247
119
248
120
Note that both the username and password must be URL-encoded (percent-encoded)
@@ -252,15 +124,15 @@ if they contain special characters:
252
124
$user = 'he:llo';
253
125
$pass = 'p@ss';
254
126
255
- $connection = $factory->createLazyConnection (
127
+ $mysql = new React\MySQL\MysqlClient (
256
128
rawurlencode($user) . ':' . rawurlencode($pass) . '@localhost:3306/db'
257
129
);
258
130
```
259
131
260
132
You can omit the port if you're connecting to default port ` 3306 ` :
261
133
262
134
``` php
263
- $factory->createLazyConnection ('user:secret@localhost/database');
135
+ $mysql = new React\MySQL\MysqlClient ('user:secret@localhost/database');
264
136
```
265
137
266
138
If you do not include authentication and/or database, then this method
@@ -269,7 +141,7 @@ and no database selected. This may be useful when initially setting up a
269
141
database, but likely to yield an authentication error in a production system:
270
142
271
143
``` php
272
- $factory->createLazyConnection ('localhost');
144
+ $mysql = new React\MySQL\MysqlClient ('localhost');
273
145
```
274
146
275
147
This method respects PHP's ` default_socket_timeout ` setting (default 60s)
@@ -278,7 +150,7 @@ successful authentication. You can explicitly pass a custom timeout value
278
150
in seconds (or use a negative number to not apply a timeout) like this:
279
151
280
152
``` php
281
- $factory->createLazyConnection ('localhost?timeout=0.5');
153
+ $mysql = new React\MySQL\MysqlClient ('localhost?timeout=0.5');
282
154
```
283
155
284
156
By default, idle connections will be held open for 1ms (0.001s) when not
@@ -291,7 +163,7 @@ pass a custom idle timeout value in seconds (or use a negative number to
291
163
not apply a timeout) like this:
292
164
293
165
``` php
294
- $factory->createLazyConnection ('localhost?idle=10.0');
166
+ $mysql = new React\MySQL\MysqlClient ('localhost?idle=10.0');
295
167
```
296
168
297
169
By default, the connection provides full UTF-8 support (using the
@@ -300,9 +172,34 @@ applications nowadays, but for legacy reasons you can change this to use
300
172
a different ASCII-compatible charset encoding like this:
301
173
302
174
``` php
303
- $factory->createLazyConnection('localhost?charset=utf8mb4');
175
+ $mysql = new React\MySQL\MysqlClient('localhost?charset=utf8mb4');
176
+ ```
177
+
178
+ If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
179
+ proxy servers etc.), you can explicitly pass a custom instance of the
180
+ [ ` ConnectorInterface ` ] ( https://github.com/reactphp/socket#connectorinterface ) :
181
+
182
+ ``` php
183
+ $connector = new React\Socket\Connector([
184
+ 'dns' => '127.0.0.1',
185
+ 'tcp' => [
186
+ 'bindto' => '192.168.10.1:0'
187
+ ],
188
+ 'tls' => [
189
+ 'verify_peer' => false,
190
+ 'verify_peer_name' => false
191
+ )
192
+ ]);
193
+
194
+ $mysql = new React\MySQL\MysqlClient('user:secret@localhost:3306/database', $connector);
304
195
```
305
196
197
+ This class takes an optional ` LoopInterface|null $loop ` parameter that can be used to
198
+ pass the event loop instance to use for this object. You can use a ` null ` value
199
+ here in order to use the [ default loop] ( https://github.com/reactphp/event-loop#loop ) .
200
+ This value SHOULD NOT be given unless you're sure you want to explicitly use a
201
+ given event loop instance.
202
+
306
203
### ConnectionInterface
307
204
308
205
The ` ConnectionInterface ` represents a connection that is responsible for
0 commit comments