/** * When used with multi query the many results will be an array * of response objects * * @depends testInterface * @return null */ public function testIteratorResponses() { $interface = 'Appfuel\\DataSource\\Db\\DbResponseInterface'; $response1 = $this->getMock($interface); $response2 = $this->getMock($interface); $response3 = $this->getMock($interface); $resultset = array($response1, $response2, $response3); $this->response->setResultSet($resultset); $this->assertEquals(3, $this->response->count()); $this->assertEquals(0, $this->response->key()); $this->assertTrue($this->response->valid()); $this->assertEquals($resultset[0], $this->response->current()); $this->assertNull($this->response->next()); $this->assertEquals(1, $this->response->key()); $this->assertTrue($this->response->valid()); $this->assertEquals($resultset[1], $this->response->current()); $this->assertNull($this->response->next()); $this->assertEquals(2, $this->response->key()); $this->assertTrue($this->response->valid()); $this->assertEquals($resultset[2], $this->response->current()); /* out of range */ $this->assertNull($this->response->next()); $this->assertNull($this->response->key()); $this->assertFalse($this->response->valid()); $this->assertFalse($this->response->current()); /* rewind to the beginning */ $this->assertNull($this->response->rewind()); $this->assertEquals(0, $this->response->key()); $this->assertTrue($this->response->valid()); $this->assertEquals($resultset[0], $this->response->current()); }
/** * @param DbRequestInterface $request * @return DbReponseInterface */ public function execute(MysqliDriver $driver, DbRequestInterface $request, DbResponseInterface $mainResponse) { $sql = $request->getSql(); $options = $request->getMultiResultOptions(); /* * -1 key indicated the loop never ran and this most likely a * syntax error. */ if (!$driver->multi_query($sql)) { $error = $this->createErrorItem(-1, $driver); $response->addError($error); return $response; } /* index for each query, this is mapped to the result keys */ $idx = 0; $data = array(); do { $resultResponse = new DbResponse(); /* * check for the existence of all available options */ $isOption = isset($options[$idx]); $isResultKey = $isOption && isset($options[$idx]['result-key']); $isCallback = $isOption && isset($options[$idx]['callback']); $resultKey = $idx; if ($isResultKey) { $resultKey = $options[$idx]['result-key']; } $callback = null; if ($isCallback) { $callback = $options[$idx]['callback']; } $driverResult = $driver->store_result(); if (!$driverResult) { $error = $this->createErrorItem($resultKey, $driver); /* * Each query in a multi query has its own response but * we also want the main response to know about each error * so we give it a copy as well */ $resultResponse->addError($error); $mainResponse->addError($error); $data[$resultKey] = $resultResponse; } else { $result = new QueryResult($driverResult); $stack = $resultResponse->getErrorStack(); $dbReturn = $result->fetchAllData($stack, MYSQLI_ASSOC, $callback); /* * merge a copy of the error items into the main response */ if ($stack->isError()) { $mainResponse->getErrorStack()->mergeStack($stack); } $resultResponse->setResultSet($dbReturn); $data[$resultKey] = $resultResponse; } $isMore = $driver->more_results(); if ($isMore) { $driver->next_result(); $idx++; } } while ($isMore); $mainResponse->setResultSet($data); return $mainResponse; }
/** * DbRequest::isResultBuffer: true * DbRequest::getCallback map column names * DbRequest::setMultiResultOptions * * @depends testInitialState * @return null */ public function testExecuteProfileB() { $driver = $this->getDriver(); $adapter = $this->getAdapter(); $sql = $this->getSql(); $request = new DbRequest($sql); $func1 = function ($row) { return array('a_col_1' => $row['param_2'], 'a_col_2' => $row['result']); }; $func2 = function ($row) { return array('b_col_1' => $row['param_2'], 'b_col_2' => $row['result']); }; $func3 = function ($row) { return array('c_col_1' => $row['param_2'], 'c_col_2' => $row['result']); }; $options = array(array('result-key' => 'first_query', 'callback' => $func1), array('result-key' => 'second_query', 'callback' => $func2), array('result-key' => 'third_query', 'callback' => $func3)); $request->setMultiResultOptions($options); $response = new DbResponse(); $return = $adapter->execute($driver, $request, $response); $this->assertSame($response, $return); $this->assertEquals(3, $response->count()); $result = $response->getResult('first_query'); $expected = new DbResponse(); $expected->setResultSet(array(array('a_col_1' => 'code_a', 'a_col_2' => 'query issued'))); $this->assertEquals($expected, $result); $result = $response->getResult('second_query'); $expected->setResultSet(array(array('b_col_1' => 'code_b', 'b_col_2' => 'query 2 issued'))); $this->assertEquals($expected, $result); $result = $response->getResult('third_query'); $expected->setResultSet(array(array('c_col_1' => 'code_c', 'c_col_2' => 'query 3 issued'))); $this->assertEquals($expected, $result); }