/** * @param MysqliDriver (mysqli) $driver * @param DbRequestInterface $request * @param DbResponseInterface $response * @return DbResponseInterface */ public function execute(MysqliDriver $driver, DbRequestInterface $request, DbResponseInterface $response) { $mode = MYSQLI_STORE_RESULT; if (!$request->isResultBuffer()) { $mode = MYSQLI_USE_RESULT; } switch ($request->getResultType()) { /* column names as keys in the result */ case 'name': $type = MYSQLI_ASSOC; break; /* column position as keys in the result */ /* column position as keys in the result */ case 'position': $type = MYSQLI_NUM; break; case 'name-pos': $type = MYSQLI_BOTH; break; default: $type = MYSQLI_ASSOC; } $sql = $request->getSql(); $filter = $request->getCallback(); try { $resultHandle = $driver->query($sql, $mode); } catch (\Exception $e) { $response->addError($e->getMessage(), $e->getCode()); return $response; } if (!$resultHandle instanceof mysqli_result) { $class = get_class($this); $response->addError("{$class} expected mysqli_result none given"); $text = "{$driver->error} {$driver->sqlstate}"; $response->addError($text, $driver->errno); return $response; } $result = new QueryResult($resultHandle); $errorStack = $response->getErrorStack(); $data = $result->fetchAllData($errorStack, $type, $filter); if (true === $data) { $data = null; } if (is_array($data)) { $response->setResultSet($data); } return $response; }
/** * @param DbRequestInterface $request * @return DbReponseInterface */ public function execute(MysqliDriver $driver, DbRequestInterface $request, DbResponseInterface $response) { $stmt = new PreparedStmt($driver->stmt_init()); $stmt->prepare($request->getSql()); if ($stmt->isError()) { $error = $stmt->getError(); $response->addError($error['error-text'], $error['error-nbr']); return $response; } /* normalize and bind parameters */ if ($request->isValues()) { $stmt->organizeParams($request->getValues()); if ($stmt->isError()) { $error = $stmt->getError(); $response->addError($error['error-text'], $error['error-nbr']); return $response; } } $stmt->execute(); if ($stmt->isError()) { $error = $stmt->getError(); $response->addError($error['error-text'], $error['error-nbr']); return $response; } $isOrganized = $stmt->organizeResults(); if ($stmt->isError()) { $error = $stmt->getError(); $response->addError($error['error-text'], $error['error-nbr']); return $response; } /* database executed the query successfully and * no results are needed */ if ($isOrganized && !$stmt->isResultset()) { return $response; } $stmt->storeResults(); $errorStack = $response->getErrorStack(); $data = $stmt->fetch($errorStack, $request->getCallback()); if (is_array($data)) { $response->setResultSet($data); } return $response; }
/** * @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; }