Example #1
0
 /**
  * @param CqlStatement $statement
  * @param array        $parameters
  * @param int          $consistency
  * @param int          $retries
  *
  * @return array|mixed
  * @throws CqlException
  * @throws \Exception
  */
 public function execute(CqlStatement $statement, array $parameters = [], $consistency = ConsistencyLevel::ONE, $retries = null)
 {
     if ($retries === null) {
         $retries = (int) $this->_config()->getItem('retries', min(max($this->_availableHostCount, 2), 10));
     }
     $return = [];
     try {
         $this->connect()->_switchDatabase();
         $packedParameters = [];
         foreach ($parameters as $k => $value) {
             $packedParameters[] = CqlDataType::pack($statement->getStatement()->variable_types[$k], $value);
         }
         $result = $this->_client->execute_prepared_cql3_query($statement->getStatement()->itemId, $packedParameters, $consistency);
         /**
          * @var $result CqlResult
          */
         if ($result->type == CqlResultType::VOID) {
             return true;
         }
         foreach ($result->rows as $row) {
             /**
              * @var $row CqlRow
              */
             $resultRow = [];
             foreach ($row->columns as $column) {
                 /**
                  * @var $column Column
                  */
                 $resultRow[$column->name] = CqlDataType::unpack($result->schema->value_types[$column->name], $column->value);
             }
             $return[] = $resultRow;
         }
     } catch (\Exception $exception) {
         if ($exception instanceof TimedOutException && !Strings::startsWith($statement->getQuery(), 'SELECT', false)) {
             // TimedOutException on writes is NOT a failure
             // http://www.datastax.com/dev/blog/how-cassandra-deals-with-replica-failure
             return true;
         }
         $this->_deleteCachedStmt($this->_cacheKey($statement->getQuery(), $statement->getCompression()));
         $e = CqlException::from($exception);
         if ($retries > 0 && $this->_isRecoverableException($e)) {
             $this->disconnect();
             if (Strings::startsWith($e->getMessage(), 'Prepared query with ID')) {
                 // re-prepare statement
                 $statement = $this->prepare($statement->getQuery(), $statement->getCompression());
             }
             return $this->execute($statement, $parameters, $consistency, $retries - 1);
         }
         error_log('CqlConnection Error: (' . $e->getCode() . ') ' . $e->getMessage());
         $this->disconnect();
         throw $e;
     }
     return $return;
 }
 public function __construct($transport)
 {
     parent::__construct(new TBinaryProtocolAccelerated($transport));
 }