|
3 | 3 | // $ php examples/11-interactive.php |
4 | 4 | // $ MYSQL_URI=test:test@localhost/test php examples/11-interactive.php |
5 | 5 |
|
6 | | -use React\MySQL\ConnectionInterface; |
7 | | -use React\MySQL\QueryResult; |
8 | | -use React\MySQL\Factory; |
9 | | -use React\Stream\ReadableResourceStream; |
10 | | - |
11 | 6 | require __DIR__ . '/../vendor/autoload.php'; |
12 | 7 |
|
13 | | -$factory = new Factory(); |
14 | | -$uri = getenv('MYSQL_URI') ?: 'test:test@localhost/test'; |
| 8 | +$mysql = new React\MySQL\MysqlClient(getenv('MYSQL_URI') ?: 'test:test@localhost/test'); |
15 | 9 |
|
16 | 10 | // open a STDIN stream to read keyboard input (not supported on Windows) |
17 | | -$stdin = new ReadableResourceStream(STDIN); |
18 | | -$stdin->pause(); |
19 | | - |
20 | | -//create a mysql connection for executing queries |
21 | | -$factory->createConnection($uri)->then(function (ConnectionInterface $connection) use ($stdin) { |
22 | | - echo 'Connection success.' . PHP_EOL; |
23 | | - $stdin->resume(); |
| 11 | +$stdin = new React\Stream\ReadableResourceStream(STDIN); |
24 | 12 |
|
25 | | - $stdin->on('data', function ($line) use ($connection) { |
26 | | - $query = trim($line); |
| 13 | +$stdin->on('data', function ($line) use ($mysql) { |
| 14 | + $query = trim($line); |
27 | 15 |
|
28 | | - if ($query === '') { |
29 | | - // skip empty commands |
30 | | - return; |
31 | | - } |
32 | | - if ($query === 'exit') { |
33 | | - // exit command should close the connection |
34 | | - echo 'bye.' . PHP_EOL; |
35 | | - $connection->quit(); |
36 | | - return; |
37 | | - } |
| 16 | + if ($query === '') { |
| 17 | + // skip empty commands |
| 18 | + return; |
| 19 | + } |
| 20 | + if ($query === 'exit') { |
| 21 | + // exit command should close the connection |
| 22 | + echo 'bye.' . PHP_EOL; |
| 23 | + $mysql->quit(); |
| 24 | + return; |
| 25 | + } |
38 | 26 |
|
39 | | - $time = microtime(true); |
40 | | - $connection->query($query)->then(function (QueryResult $command) use ($time) { |
41 | | - if (isset($command->resultRows)) { |
42 | | - // this is a response to a SELECT etc. with some rows (0+) |
43 | | - echo implode("\t", array_column($command->resultFields, 'name')) . PHP_EOL; |
44 | | - foreach ($command->resultRows as $row) { |
45 | | - echo implode("\t", $row) . PHP_EOL; |
46 | | - } |
47 | | - |
48 | | - printf( |
49 | | - '%d row%s in set (%.03f sec)%s', |
50 | | - count($command->resultRows), |
51 | | - count($command->resultRows) === 1 ? '' : 's', |
52 | | - microtime(true) - $time, |
53 | | - PHP_EOL |
54 | | - ); |
55 | | - } else { |
56 | | - // this is an OK message in response to an UPDATE etc. |
57 | | - // the insertId will only be set if this is |
58 | | - if ($command->insertId !== 0) { |
59 | | - var_dump('last insert ID', $command->insertId); |
60 | | - } |
| 27 | + $time = microtime(true); |
| 28 | + $mysql->query($query)->then(function (React\MySQL\QueryResult $command) use ($time) { |
| 29 | + if (isset($command->resultRows)) { |
| 30 | + // this is a response to a SELECT etc. with some rows (0+) |
| 31 | + echo implode("\t", array_column($command->resultFields, 'name')) . PHP_EOL; |
| 32 | + foreach ($command->resultRows as $row) { |
| 33 | + echo implode("\t", $row) . PHP_EOL; |
| 34 | + } |
61 | 35 |
|
62 | | - printf( |
63 | | - 'Query OK, %d row%s affected (%.03f sec)%s', |
64 | | - $command->affectedRows, |
65 | | - $command->affectedRows === 1 ? '' : 's', |
66 | | - microtime(true) - $time, |
67 | | - PHP_EOL |
68 | | - ); |
| 36 | + printf( |
| 37 | + '%d row%s in set (%.03f sec)%s', |
| 38 | + count($command->resultRows), |
| 39 | + count($command->resultRows) === 1 ? '' : 's', |
| 40 | + microtime(true) - $time, |
| 41 | + PHP_EOL |
| 42 | + ); |
| 43 | + } else { |
| 44 | + // this is an OK message in response to an UPDATE etc. |
| 45 | + // the insertId will only be set if this is |
| 46 | + if ($command->insertId !== 0) { |
| 47 | + var_dump('last insert ID', $command->insertId); |
69 | 48 | } |
70 | | - }, function (Exception $error) { |
71 | | - // the query was not executed successfully |
72 | | - echo 'Error: ' . $error->getMessage() . PHP_EOL; |
73 | | - }); |
74 | | - }); |
75 | 49 |
|
76 | | - // close connection when STDIN closes (EOF or CTRL+D) |
77 | | - $stdin->on('close', function () use ($connection) { |
78 | | - $connection->quit(); |
| 50 | + printf( |
| 51 | + 'Query OK, %d row%s affected (%.03f sec)%s', |
| 52 | + $command->affectedRows, |
| 53 | + $command->affectedRows === 1 ? '' : 's', |
| 54 | + microtime(true) - $time, |
| 55 | + PHP_EOL |
| 56 | + ); |
| 57 | + } |
| 58 | + }, function (Exception $error) { |
| 59 | + // the query was not executed successfully |
| 60 | + echo 'Error: ' . $error->getMessage() . PHP_EOL; |
79 | 61 | }); |
| 62 | +}); |
80 | 63 |
|
81 | | - // close STDIN (stop reading) when connection closes |
82 | | - $connection->on('close', function () use ($stdin) { |
83 | | - $stdin->close(); |
84 | | - echo 'Disconnected.' . PHP_EOL; |
85 | | - }); |
86 | | -}, function (Exception $e) use ($stdin) { |
87 | | - echo 'Connection error: ' . $e->getMessage() . PHP_EOL; |
| 64 | +// close connection when STDIN closes (EOF or CTRL+D) |
| 65 | +$stdin->on('close', function () use ($mysql) { |
| 66 | + $mysql->quit(); |
| 67 | +}); |
| 68 | + |
| 69 | +// close STDIN (stop reading) when connection closes |
| 70 | +$mysql->on('close', function () use ($stdin) { |
88 | 71 | $stdin->close(); |
| 72 | + echo 'Disconnected.' . PHP_EOL; |
89 | 73 | }); |
| 74 | + |
| 75 | +echo '# Entering interactive mode ready, hit CTRL-D to quit' . PHP_EOL; |
0 commit comments