Example #1
0
 /**
  * Return users indexed by id
  *
  * @param Query  $query
  * @param string $fieldName
  *
  * @return \Doctrine\ORM\AbstractQuery
  */
 public function setIndexBy(Query $query, $fieldName)
 {
     if (!strpos($query->getDQL(), 'WHERE')) {
         return $query->setDQL($query->getDQL() . ' INDEX BY ' . $fieldName);
     }
     return $query->setDQL(str_replace('WHERE', 'INDEX BY ' . $fieldName . ' WHERE', $query->getDQL()));
 }
Example #2
0
 /**
  * @param \Kdyby\Persistence\Queryable $repository
  *
  * @throws UnexpectedValueException
  * @return \Doctrine\ORM\Query
  */
 protected function getQuery(Queryable $repository)
 {
     $query = $this->toQuery($this->doCreateQuery($repository));
     if ($this->lastQuery && $this->lastQuery->getDQL() === $query->getDQL()) {
         $query = $this->lastQuery;
     }
     if ($this->lastQuery !== $query) {
         $this->lastResult = new ResultSet($query, $this, $repository);
     }
     return $this->lastQuery = $query;
 }
Example #3
0
 /**
  * @param string|array $columns
  * @throws InvalidStateException
  * @return ResultSet
  */
 public function applySorting($columns)
 {
     $this->updating();
     $sorting = [];
     foreach (is_array($columns) ? $columns : func_get_args() as $name => $column) {
         if (!is_numeric($name)) {
             $column = $name . ' ' . $column;
         }
         if (!preg_match('~\\s+(DESC|ASC)\\s*\\z~i', $column = trim($column))) {
             $column .= ' ASC';
         }
         $sorting[] = $column;
     }
     if ($sorting) {
         $dql = Strings::normalize($this->query->getDQL());
         if (!preg_match('~^(.+)\\s+(ORDER BY\\s+((?!FROM|WHERE|ORDER\\s+BY|GROUP\\sBY|JOIN).)*)\\z~si', $dql, $m)) {
             $dql .= ' ORDER BY ';
         } else {
             $dql .= ', ';
         }
         $this->query->setDQL($dql . implode(', ', $sorting));
     }
     $this->iterator = NULL;
     return $this;
 }
 /**
  * Updates the configured query object with the where-clause and query-hints.
  *
  * @param string $prependQuery Prepends this string to the where-clause
  *                             (" WHERE " or " AND " for example)
  *
  * @return self
  */
 public function updateQuery($prependQuery = ' WHERE ')
 {
     $whereCase = $this->getWhereClause($prependQuery);
     if ('' !== $whereCase) {
         $this->query->setDQL($this->query->getDQL() . $whereCase);
         $this->query->setHint($this->getQueryHintName(), $this->getQueryHintValue());
     }
     return $this;
 }
 /**
  * Generates a new semantical error.
  *
  * @param string $message Optional message.
  * @param array $token Optional token.
  *
  * @throws \Doctrine\ORM\Query\QueryException
  */
 public function semanticalError($message = '', $token = null)
 {
     if ($token === null) {
         $token = $this->_lexer->lookahead;
     }
     // Minimum exposed chars ahead of token
     $distance = 12;
     // Find a position of a final word to display in error string
     $dql = $this->_query->getDql();
     $length = strlen($dql);
     $pos = $token['position'] + $distance;
     $pos = strpos($dql, ' ', $length > $pos ? $pos : $length);
     $length = $pos !== false ? $pos - $token['position'] : $distance;
     $tokenPos = isset($token['position']) && $token['position'] > 0 ? $token['position'] : '-1';
     $tokenStr = substr($dql, $token['position'], $length);
     // Building informative message
     $message = 'line 0, col ' . $tokenPos . " near '" . $tokenStr . "': Error: " . $message;
     throw QueryException::semanticalError($message, QueryException::dqlError($this->_query->getDQL()));
 }
Example #6
0
 /**
  *
  * @param Query $query        	
  * @param string $cacheItemKey        	
  * @return array|bool|mixed|string
  */
 protected function getCachedResult(Query $query, $cacheItemKey = '', $ttl = 0)
 {
     if (!$cacheItemKey) {
         $cacheItemKey = ($this->cache_prefix ? $this->cache_prefix : get_called_class()) . md5($query->getDQL());
     }
     $cache = $this->getEntityManager()->getConfiguration()->getResultCacheImpl();
     // test if item exists in the cache
     if ($cache->contains($cacheItemKey)) {
         // retrieve item from cache
         $items = $cache->fetch($cacheItemKey);
     } else {
         // retrieve item from repository
         $items = $query->getResult();
         // save item to cache
         $cache->save($cacheItemKey, $items, $ttl);
     }
     return $items;
 }
 /**
  * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::info().
  */
 public function info()
 {
     $count = count($this->reader());
     return new StorageInfo(array('name' => $this->query->getDQL(), 'type' => 'DQL Query', 'count' => $count));
 }