Exemplo n.º 1
0
 /**
  * Executes a series of queries joined togather by a ';'. Once executed
  * we look into the options array for each dataset using its index
  * (basically the number it appeared in the concatenated string) looking
  * for a key to replace the index with and a callback to use on each row
  * of the dataset
  *
  * @param	mysqli	$drv		mysqli driver
  * @param	string	$sql		sql statements joined into one string
  * @param	array	$options	map used to convert dataset indexes into
  *								meaningful keys and hold callbacks
  * @return array	list of DbResponseInterface
  */
 public function execute(mysqliDriver $drv, $sql, array $createResponse, array $options = array())
 {
     if (!is_callable($createResponse)) {
         $errCode = 'AF_ERR_BAD_CALLBACK';
         $errTxt = 'reponse callback is not valid';
         $error = new Error(-1, $errCode, $errTxt);
         return call_user_func($createResponse);
     }
     /* 
      * -1 key indicated the loop never ran and this most likely a 
      * syntax error. 
      */
     if (!$drv->multi_query($sql)) {
         $error = new Error(-1, $drv->errno, $drv->error, $drv->sqlstate);
         return call_user_func($createResponse, $error);
     }
     /* index for each query, this is mapped to the result keys */
     $idx = 0;
     $data = array();
     do {
         /*
          * 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'];
         }
         /*
          * Returns a buffered result object or false if an error occured
          */
         $driverResult = $drv->store_result();
         if (!$driverResult) {
             $error = new Error($resultKey, $drv->errno, $drv->error, $drv->sqlstate);
             $data[$resultKey] = call_user_func($createResponse, $error);
             return $data;
         }
         $result = new Result($driverResult);
         $fetched = $result->fetchAllData(MYSQLI_ASSOC, $callback);
         $data[$resultKey] = call_user_func($createResponse, $fetched);
         $isMore = $drv->more_results();
         if ($isMore) {
             $drv->next_result();
             $idx++;
         }
     } while ($isMore);
     return $data;
 }
Exemplo n.º 2
0
 /**
  * Produces a result object as either buffered or unbuffered depending
  * on the result mode.
  * Buffered set of data allows you to navigate the results or determine 
  * the number of rows returned etc.. This comes at a cost in memory but 
  * easier to work with.
  * 
  * Unbuffered resultset retrieving data as needed from the
  * server. This has better performance on larger datasets, however no
  * other queries can be run until the resultset is freed
  *
  *
  * @return Select, show, decribe and explain return a result object
  *		   other queries return true and false on failure
  */
 public function execute(MysqliDriver $driver, $sql, $mode = MYSQLI_STORE_RESULT, $type = MYSQLI_ASSOC, $filter = null)
 {
     $errCode = 'AF_ERR_MYSQLI_QUERY_STMT';
     if (empty($sql)) {
         $err = 'Invalid execute: sql is empty';
         return new DbError($errCode, $err);
     }
     $valid = array(MYSQLI_USE_RESULT, MYSQLI_STORE_RESULT);
     if (!in_array($mode, $valid)) {
         $err = "Invalid execute: result mode is value";
         return new DbError($errCode, $err);
     }
     try {
         $result = $driver->query($sql, $mode);
     } catch (\Exception $e) {
         return new DbError($e->getCode(), $e->getMessage());
     }
     if ($result instanceof mysqli_result) {
         $afResult = new Result($result);
         $result = $afResult->fetchAllData($type, $filter);
     }
     return $result;
 }
Exemplo n.º 3
0
 /**
  * @param	DbRequestInterface $request
  * @return	DbResponseInterface
  */
 public function multiQuery(DbRequestInterface $request)
 {
     $driver = $this->getDriver();
     $sql = $request->getSql();
     $options = $request->getResultOptions();
     /* 
      * -1 key indicated the loop never ran and this most likely a 
      * syntax error. 
      */
     if (!$driver->multi_query($sql)) {
         $error = new Error(-1, $drv->errno, $drv->error, $drv->sqlstate);
         return $this->createResponse($error);
     }
     /* index for each query, this is mapped to the result keys */
     $idx = 0;
     $data = array();
     do {
         /*
          * 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'];
         }
         /*
          * Returns a buffered result object or false if an error occured
          */
         $driverResult = $driver->store_result();
         if (!$driverResult) {
             $error = new Error($resultKey, $drv->errno, $drv->error, $drv->sqlstate);
             $data[$resultKey] = $this->createResponse($error);
             return $data;
         }
         $result = new Result($driverResult);
         $fetched = $result->fetchAllData(MYSQLI_ASSOC, $callback);
         $data[$resultKey] = $this->createResponse($fetched);
         $isMore = $drv->more_results();
         if ($isMore) {
             $drv->next_result();
             $idx++;
         }
     } while ($isMore);
     return $this->createResponse($data);
 }