77class Connection
88{
99 /**
10- * @var \mysqli
10+ * @var LoopInterface
1111 */
12- protected $ mysqli ;
12+ public $ loop ;
1313
1414 /**
15- * @var LoopInterface
15+ * @var \mysqli
1616 */
17- protected $ loop ;
17+ protected $ mysqli ;
1818
1919 /**
2020 * @var float
2121 */
2222 protected $ pollInterval = 0.01 ;
2323
24+ /**
25+ * @var bool|string
26+ */
27+ protected $ currentQuery = false ;
28+
2429 public function __construct (\mysqli $ mysqli , LoopInterface $ loop )
2530 {
2631 $ this ->mysqli = $ mysqli ;
2732 $ this ->loop = $ loop ;
2833 }
2934
30- public function escape ($ data )
35+ /**
36+ * Proxy to the mysqli connection object.
37+ *
38+ * @param $string
39+ * @return string
40+ */
41+ public function escape ($ string )
3142 {
32- if (is_array ($ data ))
33- {
34- $ data = array_map ([
35- $ this ,
36- 'escape ' ,
37- ], $ data );
38- }
39- else
40- {
41- $ data = $ this ->mysqli ->real_escape_string ($ data );
42- }
43-
44- return $ data ;
43+ return $ this ->mysqli ->real_escape_string ($ string );
44+ }
45+
46+ /**
47+ * Close the mysqli connection.
48+ */
49+ public function close ()
50+ {
51+ $ this ->mysqli ->close ();
4552 }
4653
4754 public function execute (Command $ command )
4855 {
49- $ query = $ command ->getPreparedQuery ($ this );
56+ if ($ this ->currentQuery )
57+ {
58+ throw new \Exception ('Another query is already pending for this connection. ' );
59+ }
60+
61+ $ this ->currentQuery = $ command ->getPreparedQuery ($ this );
5062
51- $ status = $ this ->mysqli ->query ($ query , MYSQLI_ASYNC );
63+ $ status = $ this ->mysqli ->query ($ this -> currentQuery , MYSQLI_ASYNC );
5264 if ($ status === false )
5365 {
5466 throw new \Exception ($ this ->mysqli ->error );
@@ -74,7 +86,7 @@ function (TimerInterface $timer) use ($deferred)
7486 $ result = $ this ->mysqli ->reap_async_query ();
7587 if ($ result === false )
7688 {
77- $ deferred ->reject (new \ Exception ( $ this ->mysqli ->error ) );
89+ $ deferred ->reject ($ this ->mysqli ->error );
7890 }
7991 else
8092 {
@@ -86,20 +98,21 @@ function (TimerInterface $timer) use ($deferred)
8698 {
8799 if ($ error )
88100 {
89- $ deferred ->reject (new \ Exception ( $ this ->mysqli ->error ) );
101+ $ deferred ->reject ($ this ->mysqli ->error );
90102 }
91103 else
92104 {
93105 if ($ reject )
94106 {
95- $ deferred ->reject (new \ Exception ( $ this ->mysqli ->error ) );
107+ $ deferred ->reject ($ this ->mysqli ->error );
96108 }
97109 }
98110 }
99111
100112 // If poll yielded something for this connection, we're done!
101113 if ($ read || $ error || $ reject )
102114 {
115+ $ this ->currentQuery = false ;
103116 $ timer ->cancel ();
104117 }
105118 }
0 commit comments