/**
  * 6.7.13 And
  * 6.7.14 Or
  *
  * @param ConstraintInterface $lhs     Left hand side
  * @param int                 $minprec Precedence
  *
  * @return ConstraintInterface
  *
  * @throws \Exception
  */
 protected function parseConstraint($lhs = null, $minprec = 0)
 {
     if ($lhs === null) {
         $lhs = $this->parsePrimaryConstraint();
     }
     $opprec = array('OR' => 1, 'AND' => 2);
     $op = strtoupper($this->scanner->lookupNextToken());
     while (isset($opprec[$op]) && $opprec[$op] >= $minprec) {
         $this->scanner->fetchNextToken();
         $rhs = $this->parsePrimaryConstraint();
         $nextop = strtoupper($this->scanner->lookupNextToken());
         while (isset($opprec[$nextop]) && $opprec[$nextop] > $opprec[$op]) {
             $rhs = $this->parseConstraint($rhs, $opprec[$nextop]);
             $nextop = strtoupper($this->scanner->lookupNextToken());
         }
         switch ($op) {
             case 'AND':
                 $lhs = $this->factory->andConstraint($lhs, $rhs);
                 break;
             case 'OR':
                 $lhs = $this->factory->orConstraint($lhs, $rhs);
                 break;
             default:
                 // this only happens if the operator is
                 // in the $opprec-array but there is no
                 // "elseif"-branch here for this operator.
                 throw new \Exception("Internal error: No action is defined for operator '{$op}'");
         }
         $op = strtoupper($this->scanner->lookupNextToken());
     }
     return $lhs;
 }
 /**
  * 6.7.13. AndConstraint.
  */
 public function testAndConstraint()
 {
     $selector = $this->factory->selector('file', 'nt:file');
     $constraint1 = $this->factory->propertyExistence('file', 'prop1');
     $constraint2 = $this->factory->propertyExistence('file', 'prop2');
     $this->assertQuery($this->queries['6.7.13.And'], $selector, array(), $this->factory->andConstraint($constraint1, $constraint2), array());
 }
 public function walkWhereAnd(WhereAnd $whereAnd)
 {
     if (!$this->constraint) {
         return $this->walkWhere($whereAnd);
     }
     $constraint = $whereAnd->getChild();
     $res = $this->dispatch($constraint);
     $newConstraint = $this->qomf->andConstraint($this->constraint, $res);
     $this->constraint = $newConstraint;
     return $this->constraint;
 }
Example #4
0
 /**
  * {@inheritDoc}
  */
 public function getQuery(QueryBuilder $builder)
 {
     $this->aliasWithTranslatedFields = array();
     $this->locale = $builder->getLocale();
     if (null === $this->locale && $this->dm->hasLocaleChooserStrategy()) {
         $this->locale = $this->dm->getLocaleChooserStrategy()->getLocale();
     }
     $from = $builder->getChildrenOfType(QBConstants::NT_FROM);
     if (!$from) {
         throw new RuntimeException('No From (source) node in query');
     }
     $dispatches = array(QBConstants::NT_FROM, QBConstants::NT_SELECT, QBConstants::NT_WHERE, QBConstants::NT_ORDER_BY);
     foreach ($dispatches as $dispatchType) {
         $this->dispatchMany($builder->getChildrenOfType($dispatchType));
     }
     if (count($this->sourceDocumentNodes) > 1 && null === $builder->getPrimaryAlias()) {
         throw new InvalidArgumentException('You must specify a primary alias when selecting from multiple document sources' . 'e.g. $qb->from(\'a\') ...');
     }
     // for each document source add phpcr:{class,classparents} restrictions
     foreach ($this->sourceDocumentNodes as $sourceNode) {
         $documentFqn = $this->aliasMetadata[$sourceNode->getAlias()]->getName();
         $odmClassConstraints = $this->qomf->orConstraint($this->qomf->comparison($this->qomf->propertyValue($sourceNode->getAlias(), 'phpcr:class'), QOMConstants::JCR_OPERATOR_EQUAL_TO, $this->qomf->literal($documentFqn)), $this->qomf->comparison($this->qomf->propertyValue($sourceNode->getAlias(), 'phpcr:classparents'), QOMConstants::JCR_OPERATOR_EQUAL_TO, $this->qomf->literal($documentFqn)));
         if ($this->constraint) {
             $this->constraint = $this->qomf->andConstraint($this->constraint, $odmClassConstraints);
         } else {
             $this->constraint = $odmClassConstraints;
         }
     }
     foreach (array_keys($this->aliasWithTranslatedFields) as $alias) {
         $this->translator[$alias]->alterQueryForTranslation($this->qomf, $this->from, $this->constraint, $alias, $this->locale);
     }
     $phpcrQuery = $this->qomf->createQuery($this->from, $this->constraint, $this->orderings, $this->columns);
     $query = new Query($phpcrQuery, $this->dm, $builder->getPrimaryAlias());
     if ($firstResult = $builder->getFirstResult()) {
         $query->setFirstResult($firstResult);
     }
     if ($maxResults = $builder->getMaxResults()) {
         $query->setMaxResults($maxResults);
     }
     return $query;
 }
 public static function getQueries(QueryObjectModelFactoryInterface $factory)
 {
     $queries = array();
     /*
      * 6.7.3. Selector
      */
     // SELECT * FROM nt:unstructured as test
     $queries['6.7.3.Selector.Named'] = $factory->createQuery($factory->selector('test', 'nt:unstructured'), null, array(), array());
     /*
      * 6.7.8. EquiJoinCondition
      */
     // SELECT * FROM nt:file INNER JOIN nt:folder ON sel1.prop1=sel2.prop2
     $queries['6.7.8.EquiJoin.Inner'] = $factory->createQuery($factory->join($factory->selector('file', 'nt:file'), $factory->selector('folder', 'nt:folder'), Constants::JCR_JOIN_TYPE_INNER, $factory->equiJoinCondition('file', 'prop1', 'folder', 'prop2')), null, array(), array());
     // SELECT * FROM nt:file LEFT OUTER JOIN nt:folder ON sel1.prop1=sel2.prop2
     $queries['6.7.8.EquiJoin.Left'] = $factory->createQuery($factory->join($factory->selector('file', 'nt:file'), $factory->selector('folder', 'nt:folder'), Constants::JCR_JOIN_TYPE_LEFT_OUTER, $factory->equiJoinCondition('file', 'prop1', 'folder', 'prop2')), null, array(), array());
     // SELECT * FROM nt:file RIGHT OUTER JOIN nt:folder ON sel1.prop1=sel2.prop2
     $queries['6.7.8.EquiJoin.Right'] = $factory->createQuery($factory->join($factory->selector('file', 'nt:file'), $factory->selector('folder', 'nt:folder'), Constants::JCR_JOIN_TYPE_RIGHT_OUTER, $factory->equiJoinCondition('file', 'prop1', 'folder', 'prop2')), null, array(), array());
     /*
      * 6.7.9. SameNodeJoinCondition
      */
     // SELECT * FROM nt:file INNER JOIN nt:folder ON ISSAMENODE(sel1, sel2)
     $queries['6.7.9.SameNodeJoinCondition.Simple'] = $factory->createQuery($factory->join($factory->selector('file', 'nt:file'), $factory->selector('folder', 'nt:folder'), Constants::JCR_JOIN_TYPE_INNER, $factory->sameNodeJoinCondition('file', 'folder')), null, array(), array());
     // SELECT * FROM nt:file INNER JOIN nt:folder ON ISSAMENODE(sel1, sel2, /home)
     $queries['6.7.9.SameNodeJoinCondition.Path'] = $factory->createQuery($factory->join($factory->selector('file', 'nt:file'), $factory->selector('folder', 'nt:folder'), Constants::JCR_JOIN_TYPE_INNER, $factory->sameNodeJoinCondition('file', 'folder', '/home')), null, array(), array());
     /*
      * 6.7.10 ChildNodeJoinCondition
      */
     // SELECT * FROM nt:file INNER JOIN nt:folder ON ISCHILDNODE(child, parent)
     $queries['6.7.10.ChildNodeCondition'] = $factory->createQuery($factory->join($factory->selector('child', 'nt:file'), $factory->selector('parent', 'nt:folder'), Constants::JCR_JOIN_TYPE_INNER, $factory->childNodeJoinCondition('child', 'parent')), null, array(), array());
     /*
      * 6.7.11 DescendantNodeJoinCondition
      */
     // SELECT * FROM nt:file INNER JOIN nt:folder ON ISDESCENDANTNODE(descendant, ancestor)
     $queries['6.7.11.DescendantNodeJoinCondition'] = $factory->createQuery($factory->join($factory->selector('descendant', 'nt:file'), $factory->selector('ancestor', 'nt:folder'), Constants::JCR_JOIN_TYPE_INNER, $factory->descendantNodeJoinCondition('descendant', 'ancestor')), null, array(), array());
     /*
      * 6.7.12. Constraint (operator precedence)
      */
     $queries['6.7.12.Constraint.Precedence.1'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->orConstraint($factory->comparison($factory->propertyValue('file', 'prop1'), Constants::JCR_OPERATOR_EQUAL_TO, $factory->literal('1')), $factory->andConstraint($factory->comparison($factory->propertyValue('file', 'prop2'), Constants::JCR_OPERATOR_EQUAL_TO, $factory->literal('2')), $factory->comparison($factory->propertyValue('file', 'prop3'), Constants::JCR_OPERATOR_EQUAL_TO, $factory->literal('3')))), array(), array());
     $queries['6.7.12.Constraint.Precedence.2'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->orConstraint($factory->andConstraint($factory->comparison($factory->propertyValue('file', 'prop1'), Constants::JCR_OPERATOR_EQUAL_TO, $factory->literal('1')), $factory->comparison($factory->propertyValue('file', 'prop2'), Constants::JCR_OPERATOR_EQUAL_TO, $factory->literal('2'))), $factory->comparison($factory->propertyValue('file', 'prop3'), Constants::JCR_OPERATOR_EQUAL_TO, $factory->literal('3'))), array(), array());
     $queries['6.7.12.Constraint.Precedence.3'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->orConstraint($factory->notConstraint($factory->comparison($factory->propertyValue('file', 'prop1'), Constants::JCR_OPERATOR_EQUAL_TO, $factory->literal('1'))), $factory->andConstraint($factory->comparison($factory->propertyValue('file', 'prop2'), Constants::JCR_OPERATOR_EQUAL_TO, $factory->literal('2')), $factory->notConstraint($factory->comparison($factory->propertyValue('file', 'prop3'), Constants::JCR_OPERATOR_EQUAL_TO, $factory->literal('3'))))), array(), array());
     $queries['6.7.12.Constraint.Precedence.4'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->orConstraint($factory->andConstraint($factory->andConstraint($factory->propertyExistence('file', 'prop1'), $factory->propertyExistence('file', 'prop2')), $factory->propertyExistence('file', 'prop3')), $factory->andConstraint($factory->andConstraint($factory->andConstraint($factory->propertyExistence('file', 'prop4'), $factory->propertyExistence('file', 'prop5')), $factory->propertyExistence('file', 'prop6')), $factory->propertyExistence('file', 'prop7'))), array(), array());
     $queries['6.7.12.Constraint.Precedence.5'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->orConstraint($factory->andConstraint($factory->notConstraint($factory->propertyExistence('file', 'prop1')), $factory->notConstraint($factory->notConstraint($factory->propertyExistence('file', 'prop2')))), $factory->andConstraint($factory->notConstraint($factory->comparison($factory->propertyValue('file', 'prop3'), Constants::JCR_OPERATOR_EQUAL_TO, $factory->literal('hello'))), $factory->comparison($factory->propertyValue('file', 'prop4'), Constants::JCR_OPERATOR_NOT_EQUAL_TO, $factory->literal('hello')))), array(), array());
     /*
      * 6.7.13. AndConstraint
      */
     // SELECT * FROM nt:file WHERE sel1.prop1 IS NOT NULL AND sel2.prop2 IS NOT NULL
     $queries['6.7.13.And'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->andConstraint($factory->propertyExistence('file', 'prop1'), $factory->propertyExistence('file', 'prop2')), array(), array());
     /*
      * 6.7.14. OrConstraint
      */
     // SELECT * FROM nt:file WHERE sel1.prop1 IS NOT NULL OR sel2.prop2 IS NOT NULL
     $queries['6.7.14.Or'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->orConstraint($factory->propertyExistence('file', 'prop1'), $factory->propertyExistence('file', 'prop2')), array(), array());
     /*
      * 6.7.15. NotConstraint
      */
     // SELECT * FROM nt:file WHERE NOT sel1.prop1 IS NOT NULL
     $queries['6.7.15.Not'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->notConstraint($factory->propertyExistence('file', 'prop1')), array(), array());
     /*
      * 6.7.16. Comparison
      */
     // SELECT * FROM nt:file WHERE NAME(test) LIKE 'literal2'
     $queries['6.7.16.Comparison'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->comparison($factory->nodeName('file'), Constants::JCR_OPERATOR_LIKE, $factory->literal('literal2')), array(), array());
     /*
      * 6.7.18. PropertyExistence
      */
     // SELECT * FROM nt:file WHERE sel1.prop1 IS NOT NULL
     $queries['6.7.18.PropertyExistence'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->propertyExistence('file', 'prop1'), array(), array());
     /*
      * 6.7.19. FullTextSearch
      */
     // SELECT * FROM nt:file WHERE CONTAINS(sel.prop, expr)
     $queries['6.7.19.FullTextSearch'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->fullTextSearch('file', 'prop', 'expr'), array(), array());
     $queries['6.7.19.FullTextSearch_With_Single_Quote'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->fullTextSearch('file', 'prop', "expr'"), array(), array());
     /*
      * 6.7.20. SameNode
      */
     // SELECT * FROM [nt:file] AS file WHERE ISSAMENODE(file, /home)
     $queries['6.7.20.SameNode.Selector'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->sameNode('file', '/home'), array(), array());
     /* TODO
        $queries['6.7.20.SameNode.Selector_Space'] =
            $factory->createQuery(
                $factory->selector('file', 'nt:file'),
                $factory->sameNode('file', '/home node'),
                array(),
                array());
        */
     /*
      * 6.7.21. ChildNode
      */
     // SELECT * FROM [nt:file] AS file WHERE ISCHILDNODE(file, /home)
     $queries['6.7.21.ChildNode.Selector'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->childNode('file', '/home'), array(), array());
     /*
      * 6.7.22. DescendantNode
      */
     // SELECT * FROM [nt:file] AS file WHERE ISDESCENDANTNODE(file, /home)
     $queries['6.7.22.DescendantNode.Selector'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->descendantNode('file', '/home'), array(), array());
     /*
      * 6.7.27. ProperyValue
      */
     // SELECT * FROM [nt:file] AS file WHERE file.prop LIKE 'literal'
     $queries['6.7.27.PropertyValue'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->comparison($factory->propertyValue('file', 'prop'), Constants::JCR_OPERATOR_LIKE, $factory->literal('literal')), array(), array());
     // SELECT * FROM nt:unstructured WHERE sel.prop > '2013-04-15'
     $queries['6.7.27.1.PropertyValue'] = $factory->createQuery($factory->selector('sel', 'nt:unstructured'), $factory->comparison($factory->propertyValue('sel', 'prop'), Constants::JCR_OPERATOR_GREATER_THAN, $factory->literal(new \DateTime('2013-04-15 +02:00'))), array(), array());
     /*
      * 6.7.28. Length
      */
     // SELECT * FROM [nt:file] AS file WHERE LENGTH(file.prop) LIKE 'literal'
     $queries['6.7.28.Length'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->comparison($factory->length($factory->propertyValue('file', 'prop')), Constants::JCR_OPERATOR_LIKE, $factory->literal('literal')), array(), array());
     /*
      * 6.7.29. NodeName
      */
     // SELECT * FROM [nt:file] AS file WHERE NAME(file) LIKE 'literal'
     $queries['6.7.29.NodeName'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->comparison($factory->nodeName('file'), Constants::JCR_OPERATOR_LIKE, $factory->literal('literal')), array(), array());
     /*
      * 6.7.30. NodeLocalName
      */
     // SELECT * FROM [nt:file] AS file WHERE LOCALNAME(file) LIKE 'literal'
     $queries['6.7.30.NodeLocalName'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->comparison($factory->nodeLocalName('file'), Constants::JCR_OPERATOR_LIKE, $factory->literal('literal')), array(), array());
     /*
      * 6.7.31. FullTextSearchScore
      */
     // SELECT * FROM [nt:file] AS file WHERE SCORE(file) LIKE 'literal'
     $queries['6.7.31.FullTextSearchScore'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->comparison($factory->fullTextSearchScore('file'), Constants::JCR_OPERATOR_LIKE, $factory->literal('literal')), array(), array());
     /*
      * 6.7.32. LowerCase
      */
     // SELECT * FROM [nt:file] AS file WHERE LOWER(NAME(file)) LIKE 'literal'
     $queries['6.7.32.LowerCase'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->comparison($factory->lowerCase($factory->nodeName('file')), Constants::JCR_OPERATOR_LIKE, $factory->literal('literal')), array(), array());
     /*
      * 6.7.33. UpperCase
      */
     // SELECT * FROM [nt:file] AS file WHERE UPPER(NAME()) LIKE 'literal'
     $queries['6.7.33.UpperCase'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->comparison($factory->upperCase($factory->nodeName('file')), Constants::JCR_OPERATOR_LIKE, $factory->literal('literal')), array(), array());
     /*
      * 6.7.35. BindVariable
      */
     // SELECT * FROM [nt:file] AS file WHERE UPPER(NAME(file)) LIKE $var
     $queries['6.7.35.BindValue'] = $factory->createQuery($factory->selector('file', 'nt:file'), $factory->comparison($factory->upperCase($factory->nodeName('file')), Constants::JCR_OPERATOR_LIKE, $factory->bindVariable('var')), array(), array());
     /*
      * 6.7.38 Order
      */
     // SELECT * FROM nt:unstructured
     $queries['6.7.38.Order.None'] = $factory->createQuery($factory->selector('u', 'nt:unstructured'), null, array(), array());
     // SELECT * FROM nt:unstructured ORDER BY prop1 ASC
     $queries['6.7.38.Order.Asc'] = $factory->createQuery($factory->selector('u', 'nt:unstructured'), null, array($factory->ascending($factory->propertyValue('u', 'prop1'))), array());
     // SELECT * FROM nt:unstructured ORDER BY prop1 DESC
     $queries['6.7.38.Order.Desc'] = $factory->createQuery($factory->selector('u', 'nt:unstructured'), null, array($factory->descending($factory->propertyValue('u', 'prop1'))), array());
     // SELECT * FROM nt:unstructured ORDER BY prop1 ASC, prop2 DESC
     $queries['6.7.38.Order.Mixed'] = $factory->createQuery($factory->selector('u', 'nt:unstructured'), null, array($factory->ascending($factory->propertyValue('u', 'prop1')), $factory->descending($factory->propertyValue('u', 'prop2'))), array());
     /*
      * 6.7.39 Column
      */
     // SELECT * FROM nt:unstructured
     $queries['6.7.39.Colum.Wildcard'] = $factory->createQuery($factory->selector('u', 'nt:unstructured'), null, array(), array());
     // SELECT u.prop1 AS prop1 FROM [nt:unstructured] AS u
     $queries['6.7.39.Colum.Selector'] = $factory->createQuery($factory->selector('u', 'nt:unstructured'), null, array(), array($factory->column('u', 'prop1', 'col1')));
     // SELECT u.prop1, u.prop2 AS col2 FROM nt:unstructured
     $queries['6.7.39.Colum.Mixed'] = $factory->createQuery($factory->selector('u', 'nt:unstructured'), null, array(), array($factory->column('u', 'prop1', 'col1'), $factory->column('u', 'prop2', 'prop2')));
     return $queries;
 }
 /**
  * {@inheritDoc}
  *
  * Join document with translation children, and filter on the right child
  * node.
  */
 public function alterQueryForTranslation(QueryObjectModelFactoryInterface $qomf, SelectorInterface &$selector, ConstraintInterface &$constraint = null, $alias, $locale)
 {
     $childAlias = "_{$locale}_{$alias}";
     $selector = $qomf->join($selector, $qomf->selector($childAlias, 'nt:base'), QueryObjectModelConstantsInterface::JCR_JOIN_TYPE_RIGHT_OUTER, $qomf->childNodeJoinCondition($childAlias, $alias));
     $languageConstraint = $qomf->comparison($qomf->nodeName($childAlias), QueryObjectModelConstantsInterface::JCR_OPERATOR_EQUAL_TO, $qomf->literal(Translation::LOCALE_NAMESPACE . ":{$locale}"));
     if ($constraint) {
         $constraint = $qomf->andConstraint($constraint, $languageConstraint);
     } else {
         $constraint = $languageConstraint;
     }
 }