/** * @param string $sql * @param string $type * @param string $stategy * @param mixed $values * @return DbRequest */ public function createRequest($sql, $type = null, $strategy = null, $values = null) { $request = new DbRequest($sql, $type, $strategy); if (null !== $values) { if (is_scalar($values)) { $values = array($values); } else { if (!is_array($values)) { $paramType = gettype($values); $err = "values must be a scalar of an array -({$paramType})"; throw new InvalidArgumentException($err); } } $request->setValues($values); } return $request; }
/** * 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); }
/** * DbRequest::isResultBuffer: true * DbRequest::getResultType: name (MYSQLI_ASSOC) * DbRequest::getCallback throws an exception * * @depends testInitialState * @return null */ public function testExecuteProfileF() { $driver = $this->getDriver(); $adapter = $this->getAdapter(); $sql = $this->getSql(); $request = new DbRequest($sql); $request->setValues(array(1, 2, 3)); $errMsg = "error has occured"; $func = function ($row) use($errMsg) { throw new Exception($errMsg, 99); }; $request->setCallback($func); $response = new DbResponse(); $result = $adapter->execute($driver, $request, $response); $this->assertSame($response, $result); $this->assertTrue($response->isError()); $stack = $response->getErrorStack(); $this->assertEquals(3, $stack->count()); $error = $stack->current(); $idx = 0; $expected = "error has occured -(0)"; $this->assertEquals(99, $error->getCode()); $this->assertEquals($expected, $error->getMessage()); $idx = 1; $expected = "error has occured -(1)"; $stack->next(); $error = $stack->current(); $this->assertEquals(99, $error->getCode()); $this->assertEquals($expected, $error->getMessage()); $expected = "error has occured -(2)"; $stack->next(); $error = $stack->current(); $this->assertEquals(99, $error->getCode()); $this->assertEquals($expected, $error->getMessage()); }
/** * DbRequest::isResultBuffer: true * DbRequest::getResultType: name (MYSQLI_ASSOC) * DbRequest::getCallback throws an exception * * @depends testInitialState * @return null */ public function testExecuteProfileF() { $driver = $this->getDriver(); $adapter = $this->getQueryAdapter(); $sql = $this->getSql(); $request = new DbRequest($sql); $errMsg = "error as occured"; $func = function ($row) use($errMsg) { if ('code_b' === $row['param_2']) { throw new Exception($errMsg, 99); } return array('new_param_2' => $row['param_2'], 'new_result' => $row['result']); }; $request->setCallback($func); $response = new DbResponse(); $result = $adapter->execute($driver, $request, $response); $this->assertSame($response, $result); $this->assertTrue($response->isError()); $stack = $response->getErrorStack(); $this->assertEquals(1, $stack->count()); $error = $stack->current(); $class = get_class($adapter); $expected = "{$errMsg} -(1)"; $results = $response->getResultSet(); $this->assertEquals(99, $error->getCode()); $this->assertEquals($expected, $error->getMessage()); }