@@ -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,76 @@ 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) only once the
175+ first request is invoked on this instance and will queue all outstanding
176+ requests until the underlying connection is ready.
177+
178+ From a consumer side this means that you can start sending queries to the
179+ database right away while the actual connection may still be outstanding.
180+ It will ensure that all commands will be executed in the order they are
181+ enqueued once the connection is ready. If the database connection fails,
182+ it will emit an ` error ` event, reject all outstanding commands and ` close `
183+ the connection as described in the ` ConnectionInterface ` . In other words,
184+ it behaves just like a real connection and frees you from having to deal
185+ with its async resolution.
186+
187+ Note that creating the underlying connection will be deferred until the
188+ first request is invoked. Accordingly, any eventual connection issues
189+ will be detected once this instance is first used. Similarly, calling
190+ ` quit() ` on this instance before invoking any requests will succeed
191+ immediately and will not wait for an actual underlying connection.
192+
193+ Depending on your particular use case, you may prefer this method or the
194+ underlying ` createConnection() ` which resolves with a promise. For many
195+ simple use cases it may be easier to create a lazy connection.
196+
197+ The ` $url ` parameter must contain the database host, optional
198+ authentication, port and database to connect to:
199+
200+ ``` php
201+ $factory->createLazyConnection('user:secret@localhost:3306/database');
202+ ```
203+
204+ You can omit the port if you're connecting to default port ` 3306 ` :
205+
206+ ``` php
207+ $factory->createLazyConnection('user:secret@localhost/database');
208+ ```
209+
210+ If you do not include authentication and/or database, then this method
211+ will default to trying to connect as user ` root ` with an empty password
212+ and no database selected. This may be useful when initially setting up a
213+ database, but likely to yield an authentication error in a production system:
214+
215+ ``` php
216+ $factory->createLazyConnection('localhost');
217+ ```
218+
219+ This method respects PHP's ` default_socket_timeout ` setting (default 60s)
220+ as a timeout for establishing the underlying connection and waiting for
221+ successful authentication. You can explicitly pass a custom timeout value
222+ in seconds (or use a negative number to not apply a timeout) like this:
223+
224+ ``` php
225+ $factory->createLazyConnection('localhost?timeout=0.5');
226+ ```
227+
157228### ConnectionInterface
158229
159230The ` ConnectionInterface ` represents a connection that is responsible for
0 commit comments