|
1 | | -# react-mysql |
2 | | -Nuclear MySQL Reactor |
| 1 | +# ReactMysql |
3 | 2 |
|
| 3 | +Non-blocking MySQLi database access with PHP. |
| 4 | +Designed to work with [reactphp/react](https://github.com/reactphp/react). |
4 | 5 |
|
5 | | -# Examples |
6 | 6 |
|
7 | | -Connection::init($loop); |
| 7 | +## Working |
8 | 8 |
|
9 | | -Connection::query('SELECT * FROM `table` WHERE `column` = ? AND `column2` = ?;', ['red', 'white']) |
10 | | - ->then(function($result) { ... }); |
| 9 | +This __is__ working. But it is nowhere near complete. |
11 | 10 |
|
12 | | -Connection::query returns a promise. This has all of the normal promise interface options. |
| 11 | + $ ./run |
| 12 | + Starting loop... |
| 13 | + DB Created. |
| 14 | + Run Query: 0 |
| 15 | + Found rows: 0 |
| 16 | + Run Query: 1 |
| 17 | + Found rows: 1 |
| 18 | + Current memory usage: 735.117K |
| 19 | + Run Query: 2 |
| 20 | + Found rows: 0 |
| 21 | + Run Query: 3 |
| 22 | + Found rows: 1 |
| 23 | + Run Query: 4 |
| 24 | + Found rows: 1 |
| 25 | + Current memory usage: 735.117K |
| 26 | + Run Query: 5 |
| 27 | + Found rows: 0 |
| 28 | + Current memory usage: 733.602K |
| 29 | + Current memory usage: 733.602K |
| 30 | + Current memory usage: 733.602K |
| 31 | + Loop finished, all timers halted. |
13 | 32 |
|
14 | | -# Credits |
15 | | -Inspiration from: |
16 | | - - https://github.com/kaja47/async-mysql |
17 | | - - https://github.com/bixuehujin/reactphp-mysql |
| 33 | +This won't work out of the box without the database configured. |
| 34 | +As of this point, database configuration is hard coded. |
| 35 | +Still need to pull out the configs. You will also need to |
| 36 | +set up a database with some data to query. Check back later |
| 37 | +for more! |
| 38 | + |
| 39 | +## TODO |
| 40 | + |
| 41 | +A lot. |
| 42 | + |
| 43 | +This is not production ready. Still tons to do on the query builder. |
| 44 | +While I hate to reinvent the wheel, I have not found a lightweight |
| 45 | +injectable query builder that is not tied to a massive framework. |
| 46 | + |
| 47 | +## Plans (Future Examples) |
| 48 | + |
| 49 | +These are just plans for now. It may change wildly as we develop. |
| 50 | + |
| 51 | +### Current Development Example |
| 52 | + |
| 53 | +Here is an example of what is currently working for the most part. |
| 54 | + |
| 55 | + $loop = React\EventLoop\Factory::create(); |
| 56 | + |
| 57 | + ConnectionFactory::init($loop); |
| 58 | + |
| 59 | + $db = new \DustinGraham\ReactMysql\Database(); |
| 60 | + |
| 61 | + $db->createCommand("SELECT * FROM `table` WHERE id = :id;", [':id' => $id]) |
| 62 | + ->execute()->then( |
| 63 | + function($result) |
| 64 | + { |
| 65 | + $rows = $result->fetch_all(MYSQLI_ASSOC); |
| 66 | + $result->close(); |
| 67 | + |
| 68 | + // Do something with $rows. |
| 69 | + } |
| 70 | + ); |
| 71 | + |
| 72 | + |
| 73 | +### Original Big Picture Plans |
| 74 | + |
| 75 | +Here are some examples of how it may be, eventually. |
| 76 | +It would be nice to hide away some of the current boilerplate. |
| 77 | + |
| 78 | + Connection::init($loop); |
| 79 | + |
| 80 | + Connection::query( |
| 81 | + 'SELECT * FROM `table` WHERE `column` = ? AND `column2` = ?;', |
| 82 | + ['red', 'white'] |
| 83 | + )->then(function($result) { ... }); |
| 84 | + |
| 85 | + Connection::query(...) returns a promise. |
| 86 | + |
| 87 | + $db = new Database(); |
| 88 | + $db->createCommand('SELECT * FROM table WHERE id = :id', [':id' => 1]) |
| 89 | + ->execute() |
| 90 | + ->then(function($results) { |
| 91 | + echo $results[0]->name; |
| 92 | + }); |
| 93 | + |
| 94 | + |
| 95 | +And another idea... |
| 96 | + |
| 97 | + DB::loadModel('id', ' =', '3')->then(function($model) use ($socket) { |
| 98 | + $socket->send('Your name is '.$model->name); |
| 99 | + }); |
| 100 | + |
| 101 | +## Difficulties |
| 102 | + |
| 103 | +There were many difficulties. |
| 104 | + |
| 105 | +At this point, I can not find any libraries that handle parameterized queries |
| 106 | +without using PDO or prepared statements. |
| 107 | + |
| 108 | +MYSQLI_ASYNC does not support prepared statements and parameter binding. So we had to write it ourselves. |
| 109 | + |
| 110 | +The mysqli::real_escape_string requires a link. But, the link is one of many. |
| 111 | +Last minute escaping once the command and connection were married from the pool. |
| 112 | +Could potentially have one dedicated link for escaping. |
| 113 | + |
| 114 | +## Credits |
| 115 | + |
| 116 | +Much appreciation to the hard work over at [reactphp/react](https://github.com/reactphp/react). |
| 117 | + |
| 118 | +Inspired by similar projects: |
| 119 | + - [kaja47/async-mysql](https://github.com/kaja47/async-mysql) |
| 120 | + - [bixuehujin/reactphp-mysql](https://github.com/bixuehujin/reactphp-mysql) |
| 121 | + |
| 122 | +## License |
| 123 | + |
| 124 | +DustinGraham/ReactMysql is released under the [MIT](https://github.com/dustingraham/react-mysql/blob/master/LICENSE) license. |
0 commit comments