Ejemplo n.º 1
0
 public function interpretQuery(TokenSequencerInterface $query)
 {
     $this->reset();
     $this->query = $query;
     $sql = "DELETE FROM " . $this->renderArbitraryReference($query->getEntityMetadata()->getCollection());
     while ($token = $query->getNextToken()) {
         $sql .= " " . $this->renderToken($token);
     }
     return $sql;
 }
 protected function renderInCondition()
 {
     $sql = "IN ";
     $sql .= $this->renderToken($this->query->getNextToken());
     // open
     $list = [];
     while (($token = $this->query->getNextToken()) && $token->getType() != "close") {
         $list[] = $this->renderToken($token);
     }
     $sql .= implode(", ", $list);
     if (!empty($token)) {
         $sql .= $this->renderToken($token);
         // close
     }
     return $sql;
 }
Ejemplo n.º 3
0
 public function interpretQuery(TokenSequencerInterface $query)
 {
     $this->reset();
     $this->query = $query;
     $includes = $query->getIncludes();
     $mainMetadata = $query->getEntityMetadata();
     $mainCollection = $mainMetadata->getCollection();
     $token = $query->getNextToken();
     $this->fields = [];
     while (!empty($token) && in_array($token->getType(), ["function", "field"])) {
         if ($token->getType() == "function") {
             /** @var Value $token */
             // render the aggregate function SQL e.g. COUNT(*), SUM(field), etc...
             $fieldSql = $this->renderFunction($token);
             // create an alias for this aggregate and append the SQL
             $collectionAlias = $this->findFreeAlias($token->getValue(), $this->fields);
             $fieldSql .= $this->renderAlias($collectionAlias);
         } else {
             /** @var Reference $token */
             $fieldName = $mainCollection . "." . $token->getValue();
             $collectionAlias = $this->getSelectFieldAlias($fieldName);
             $fieldSql = $this->renderArbitraryReference($fieldName, $collectionAlias);
         }
         // add the sql to the fields array and load the next token
         $this->fields[$collectionAlias] = $fieldSql;
         $token = $query->getNextToken();
     }
     // if we had no aggregate functions in the sequence, then this is a standard select query
     // so, we need to get the fields to return from the entity metadata for each
     if (empty($this->fields)) {
         $metadata = $mainMetadata;
         $collectionAlias = $mainCollection;
         do {
             if (empty($metadata)) {
                 continue;
             }
             // for each entity field, create an aliased SQL reference
             $entityFields = $metadata->getFieldNames();
             sort($entityFields);
             foreach ($entityFields as $field) {
                 $field = $collectionAlias . "." . $field;
                 $thisFieldAlias = $this->getSelectFieldAlias($field);
                 $this->fields[$thisFieldAlias] = $this->renderArbitraryReference($field, $thisFieldAlias);
             }
         } while (list($collectionAlias, $metadata) = each($includes));
     }
     if (empty($this->fields)) {
         throw new InterpretationException("Cannot interpret find query, there are no fields to return");
     }
     // if this query has an include, it's more complex and requires separate processing
     if (count($includes) > 0) {
         return $this->renderIncludeQuery($token);
     }
     // simple, no include query. Render all remaining tokens
     $sql = "SELECT " . implode(", ", $this->fields) . " FROM " . $this->renderArbitraryReference($mainCollection);
     // render all other tokens
     do {
         if (empty($token)) {
             break;
         }
         $sql .= " " . $this->renderToken($token);
     } while ($token = $this->query->getNextToken());
     return $sql;
 }