/** * Performs the actual DB query of a SQL statement. * * @param string $method method of PDOStatement to be called * @param integer $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) * for valid fetch modes. If this parameter is null, the value set in {@see \rock\db\Command::$fetchMode} will be used. * @return mixed the method execution result * @throws DbException if the query causes any problem */ protected function queryInternal($method, $fetchMode = null) { $connection = $this->connection; $rawSql = $this->getRawSql(); $token = ['dsn' => $connection->dsn, 'sql' => $rawSql, 'valid' => true, 'cache' => false]; Trace::beginProfile('db.query', $token); $cache = null; /** @var $cache CacheInterface */ if ($connection->enableQueryCache && $method !== '') { $cache = Instance::ensure($connection->queryCache, null, [], false); } $lock = true; if ($cache instanceof CacheInterface) { $cacheKey = json_encode([__CLASS__, $method, $connection->dsn, $connection->username, $rawSql]); $result = $cache->get($cacheKey); if (is_array($result) && array_key_exists(0, $result)) { Trace::increment('cache.db', 'Cache query DB connection: ' . $connection->dsn); Trace::endProfile('db.query', $token); $token['cache'] = true; Trace::trace('db.query', $token); return $result[0]; } $lock = $cache->lock($cacheKey); } if ($lock === false) { return null; } $this->prepare(true); try { $this->pdoStatement->execute(); if ($method === '') { $result = new DataReader($this); } else { if ($fetchMode === null) { $fetchMode = $this->fetchMode; } $result = call_user_func_array([$this->pdoStatement, $method], (array) $fetchMode); if ($result === false) { $result = null; } $this->pdoStatement->closeCursor(); } if (isset($cache, $cacheKey) && $cache instanceof CacheInterface) { $cache->set($cacheKey, [$result], $connection->queryCacheExpire, $connection->queryCacheTags ?: $this->getRawEntityNames()); $cache->unlock($cacheKey); } Trace::endProfile('db.query', $token); Trace::trace('db.query', $token); return $result; } catch (\Exception $e) { $message = $e->getMessage() . "\nThe SQL being executed was: {$rawSql}"; Trace::endProfile('db.query', $token); $token['valid'] = false; $token['exception'] = defined('ROCK_DEBUG') && ROCK_DEBUG === true ? $e : $message; Trace::trace('db.query', $token); if (isset($cache, $cacheKey) && $cache instanceof CacheInterface) { $cache->unlock($cacheKey); } throw new DbException($message, [], 0, $e); } }
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; }
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; }