@@ -15,6 +15,7 @@ It is written in pure PHP and does not require any extensions.
1515* [ Usage] ( #usage )
1616 * [ Factory] ( #factory )
1717 * [ createConnection()] ( #createconnection )
18+ * [ createLazyConnection()] ( #createlazyconnection )
1819 * [ ConnectionInterface] ( #connectioninterface )
1920 * [ query()] ( #query )
2021 * [ queryStream()] ( #querystream )
@@ -35,20 +36,20 @@ $loop = React\EventLoop\Factory::create();
3536$factory = new Factory($loop);
3637
3738$uri = 'test:test@localhost/test';
38- $factory->createConnection ($uri)->then(function (ConnectionInterface $connection) {
39- $connection->query('SELECT * FROM book')->then(
40- function (QueryResult $command) {
41- print_r( $command->resultFields);
42- print_r($command->resultRows );
43- echo count ($command->resultRows) . ' row(s) in set' . PHP_EOL ;
44- },
45- function (Exception $error) {
46- echo 'Error: ' . $error->getMessage() . PHP_EOL;
47- }
48- );
49-
50- $connection->quit();
51- } );
39+ $connection = $ factory->createLazyConnection ($uri);
40+
41+ $connection->query('SELECT * FROM book')->then(
42+ function (QueryResult $command) {
43+ print_r($command->resultFields );
44+ print_r ($command->resultRows);
45+ echo count($command->resultRows) . ' row(s) in set' . PHP_EOL;
46+ },
47+ function (Exception $error) {
48+ echo 'Error: ' . $error->getMessage() . PHP_EOL;
49+ }
50+ );
51+
52+ $connection->quit( );
5253
5354$loop->run();
5455```
@@ -154,6 +155,69 @@ authentication. You can explicitly pass a custom timeout value in seconds
154155$factory->createConnection('localhost?timeout=0.5');
155156```
156157
158+ #### createLazyConnection()
159+
160+ Creates a new connection.
161+
162+ It helps with establishing a TCP/IP connection to your MySQL database
163+ and issuing the initial authentication handshake.
164+
165+ ``` php
166+ $connection = $factory->createLazyConnection($url);
167+
168+ $connection->query(…);
169+ ```
170+
171+ This method immediately returns a "virtual" connection implementing the
172+ [ ` ConnectionInterface ` ] ( #connectioninterface ) that can be used to
173+ interface with your MySQL database. Internally, it lazily creates the
174+ underlying database connection (which may take some time) and will
175+ queue all outstanding requests until the underlying connection is ready.
176+
177+ From a consumer side this means that you can start sending queries to the
178+ database right away while the connection may still be pending. It will
179+ ensure that all commands will be executed in the order they are enqueued
180+ once the connection is ready. If the database connection fails, it will
181+ emit an ` error ` event, reject all outstanding commands and ` close ` the
182+ connection as described in the ` ConnectionInterface ` . In other words, it
183+ behaves just like a real connection and frees you from having to deal
184+ with its async resolution.
185+
186+ Depending on your particular use case, you may prefer this method or the
187+ underlying ` createConnection() ` which resolves with a promise. For many
188+ simple use cases it may be easier to create a lazy connection.
189+
190+ The ` $url ` parameter must contain the database host, optional
191+ authentication, port and database to connect to:
192+
193+ ``` php
194+ $factory->createLazyConnection('user:secret@localhost:3306/database');
195+ ```
196+
197+ You can omit the port if you're connecting to default port ` 3306 ` :
198+
199+ ``` php
200+ $factory->createLazyConnection('user:secret@localhost/database');
201+ ```
202+
203+ If you do not include authentication and/or database, then this method
204+ will default to trying to connect as user ` root ` with an empty password
205+ and no database selected. This may be useful when initially setting up a
206+ database, but likely to yield an authentication error in a production system:
207+
208+ ``` php
209+ $factory->createLazyConnection('localhost');
210+ ```
211+
212+ This method respects PHP's ` default_socket_timeout ` setting (default 60s)
213+ as a timeout for establishing the connection and waiting for successful
214+ authentication. You can explicitly pass a custom timeout value in seconds
215+ (or use a negative number to not apply a timeout) like this:
216+
217+ ``` php
218+ $factory->createLazyConnection('localhost?timeout=0.5');
219+ ```
220+
157221### ConnectionInterface
158222
159223The ` ConnectionInterface ` represents a connection that is responsible for
0 commit comments