Example #1
0
 /**
  * Executes Mongo command.
  *
  * @param array $command command specification.
  * @param array $options options in format: "name" => "value"
  * @return array database response.
  * @throws MongoException on failure.
  */
 public function executeCommand($command, array $options = [])
 {
     $rawQuery = $this->getName() . '.$cmd(' . Json::encode($command) . ', ' . Json::encode($options) . ')';
     $token = ['query' => $rawQuery, 'valid' => true, 'cache' => false];
     //Log::info($token, __METHOD__);
     Trace::beginProfile('mongodb.query', $token);
     try {
         $result = $this->mongoDb->command($command, $options);
         $this->tryResultError($result);
         Trace::endProfile('mongodb.query', $token);
         Trace::trace('mongodb.query', $token);
         return $result;
     } catch (\Exception $e) {
         $message = $e->getMessage() . "\nThe query being executed was: {$rawQuery}";
         Trace::endProfile('mongodb.query', $token);
         $token['valid'] = false;
         $token['exception'] = defined('ROCK_DEBUG') && ROCK_DEBUG === true ? $e : $message;
         Trace::trace('mongodb.query', $token);
         throw new MongoException($message, [], 0, $e);
     }
 }
Example #2
0
File: Rock.php Project: romeoz/rock
 /**
  * End profile.
  *
  * @param $category
  * @param $token
  */
 public static function endProfile($category, $token)
 {
     Trace::endProfile($category, $token);
 }
Example #3
0
 /**
  * Sets the transaction isolation level for this transaction.
  *
  * This method can be used to set the isolation level while the transaction is already active.
  * However this is not supported by all DBMS so you might rather specify the isolation level directly
  * when calling {@see \rock\db\Transaction::begin()}.
  *
  * @param string $level The transaction isolation level to use for this transaction.
  * This can be one of {@see \rock\db\Transaction::READ_UNCOMMITTED}, {@see \rock\db\Transaction::READ_COMMITTED}, {@see \rock\db\Transaction::REPEATABLE_READ} and {@see \rock\db\Transaction::SERIALIZABLE} but
  * also a string containing DBMS specific syntax to be used after `SET TRANSACTION ISOLATION LEVEL`.
  * @throws DbException if the transaction is not active
  * @see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels
  */
 public function setIsolationLevel($level)
 {
     if (!$this->getIsActive()) {
         throw new DbException('Failed to set isolation level: transaction was inactive.');
     }
     Trace::trace('db', ['msg' => 'Setting transaction isolation level to ' . $level, 'method' => __METHOD__]);
     $this->connection->getSchema()->setTransactionIsolationLevel($level);
 }
Example #4
0
 protected function getCache($rawQuery, $token, $all, $indexBy, &$cache, &$cacheKey, &$lock = true)
 {
     /** @var $cache CacheInterface */
     if ($this->connection->enableQueryCache) {
         $cache = Instance::ensure($this->connection->queryCache, null, [], false);
     }
     if ($cache instanceof CacheInterface) {
         $cacheKey = json_encode([__METHOD__, $this->connection->dsn, $rawQuery]);
         if (($result = $cache->get($cacheKey)) !== false) {
             Trace::increment('cache.mongodb', 'Cache query MongoDB connection: ' . $this->connection->dsn);
             Trace::endProfile('mongodb.query', $token);
             $token['cache'] = true;
             Trace::trace('mongodb.query', $token);
             return is_array($result) ? $this->fetchRowsAsArrayInternal($result, $all, $indexBy) : $result;
         }
         $lock = $cache->lock($cacheKey);
     }
     return false;
 }
Example #5
0
 /**
  * Closes the currently active DB connection.
  * It does nothing if the connection is already closed.
  */
 public function close()
 {
     if ($this->pdo !== null) {
         Trace::trace('db', 'Closing DB connection: ' . $this->dsn);
         $this->pdo = null;
         $this->_schema = null;
         $this->_transaction = null;
     }
     if ($this->_slave) {
         $this->_slave->close();
         $this->_slave = null;
     }
 }
Example #6
0
 /**
  * Deletes the file with given _id.
  *
  * @param mixed $id _id of the file to find.
  * @return boolean whether the operation was successful.
  * @throws MongoException on failure.
  */
 public function delete($id)
 {
     $token = 'Inserting file uploads into ' . $this->getFullName();
     //Log::info($token);
     Trace::beginProfile('mongodb.query', $token);
     try {
         $result = $this->mongoCollection->delete($id);
         $this->tryResultError($result);
         Trace::endProfile('mongodb.query', $token);
         Trace::trace('mongodb.query', $token);
         return true;
     } catch (\Exception $e) {
         $message = $e->getMessage() . "\nThe query being executed was: {$token}";
         Trace::endProfile('mongodb.query', $token);
         $token['valid'] = false;
         $token['exception'] = defined('ROCK_DEBUG') && ROCK_DEBUG === true ? $e : $message;
         Trace::trace('mongodb.query', $token);
         throw new MongoException($message, [], $e);
     }
 }
Example #7
0
 /**
  * Closes the currently active DB connection.
  * It does nothing if the connection is already closed.
  */
 public function close()
 {
     if ($this->mongoClient !== null) {
         Trace::trace('mongodb', 'Closing MongoDB connection: ' . $this->dsn);
         $this->mongoClient = null;
         $this->_databases = [];
     }
 }
Example #8
0
 protected function clearCache($table, $rawSql)
 {
     $this->_clearCache = function ($connection) use($table, $rawSql) {
         /** @var Connection $connection */
         if (!$connection->autoClearCache || !($tables = $connection->queryCacheTags ?: [$table])) {
             return;
         }
         /** @var $cache CacheInterface */
         $cache = Instance::ensure($connection->queryCache, null, [], false);
         if (!$cache instanceof CacheInterface) {
             return;
         }
         $cache->removeMultiTags($tables);
         $token = ['dsn' => $connection->dsn, 'sql' => $rawSql, 'message' => "Clear cache by tags: {$table}"];
         Trace::trace('cache.db', $token);
     };
 }
Example #9
0
 protected function getCache($rawQuery, $token, &$cache, &$cacheKey)
 {
     $this->calculateCacheParams($this->connection);
     /** @var $cache CacheInterface */
     if ($this->connection->enableQueryCache) {
         $cache = Instance::ensure($this->connection->queryCache, null, [], false);
     }
     if ($cache instanceof CacheInterface) {
         $cacheKey = json_encode([__METHOD__, $this->connection->dsn, $rawQuery]);
         if (($result = $cache->get($cacheKey)) !== false) {
             Trace::increment('cache.mongodb', 'Cache query MongoDB connection: ' . $this->connection->dsn);
             Trace::endProfile('mongodb.query', $token);
             $token['cache'] = true;
             Trace::trace('mongodb.query', $token);
             return $result;
         }
     }
     return false;
 }