public function testStatements() { //simple query, should be sql $this->qb->from($this->qf->selector('nt:base', "nt:base")); $this->assertSame('sql', $this->qb->getQuery()->getLanguage()); $this->assertSame("SELECT s FROM nt:base", $this->qb->getQuery()->getStatement()); //localname is not supported by sql1 $this->qb->where($this->qf->comparison($this->qf->nodeLocalName('nt:base'), Constants::JCR_OPERATOR_EQUAL_TO, $this->qf->literal('foo'))); $this->assertSame('JCR-SQL2', $this->qb->getQuery()->getLanguage()); $this->assertSame("SELECT * FROM [nt:base] WHERE LOCALNAME(nt:base) = 'foo'", $this->qb->getQuery()->getStatement()); //descendantNode is supported by sql1 $this->qb->where($this->qf->descendantNode('nt:base', "/foo")); //$this->assertSame('sql', $this->qb->getQuery()->getLanguage()); $this->assertSame("SELECT s FROM nt:base WHERE jcr:path LIKE '/foo[%]/%'", $this->qb->getQuery()->getStatement()); //joins are not supported by sql1 $this->qb->join($this->qf->selector('nt:unstructured', "nt:unstructured"), $this->qf->equiJoinCondition("nt:base", "data", "nt:unstructured", "data")); $this->assertSame('JCR-SQL2', $this->qb->getQuery()->getLanguage()); $this->assertSame("SELECT * FROM [nt:base] INNER JOIN [nt:unstructured] ON [nt:base].data=[nt:unstructured].data WHERE ISDESCENDANTNODE([nt:base], [/foo])", $this->qb->getQuery()->getStatement()); }
public function testGetQueryWithOffsetAndLimit() { $source = $this->getMock('PHPCR\\Query\\QOM\\SourceInterface', array(), array()); $constraint = $this->getMock('PHPCR\\Query\\QOM\\ConstraintInterface', array(), array()); $query = $this->getMock('PHPCR\\Query\\QOM\\QueryObjectModelInterface', array(), array()); $qb = new QueryBuilder($this->qf); $qb->from($source); $qb->where($constraint); $qb->setFirstResult(13); $qb->setMaxResults(42); $query->expects($this->once())->method('setOffset'); $query->expects($this->once())->method('setLimit'); $this->qf->expects($this->once())->method('createQuery')->will($this->returnValue($query)); $qb->getQuery(); }
/** * Return snippets load query. * * If $type is given then only return the snippets of that type. * * @param string $languageCode * @param string $type Optional snippet type * @param int $offset Optional offset * @param int $max Optional max * @param string $search * @param string $sortBy * @param string $sortOrder * * @return Query */ private function getSnippetsQuery($languageCode, $type = null, $offset = null, $max = null, $search = null, $sortBy = null, $sortOrder = null) { $snippetNode = $this->sessionManager->getSnippetNode($type); $workspace = $this->sessionManager->getSession()->getWorkspace(); $queryManager = $workspace->getQueryManager(); $qf = $queryManager->getQOMFactory(); $qb = new QueryBuilder($qf); $qb->from($qb->qomf()->selector('a', 'nt:unstructured')); if (null === $type) { $qb->where($qb->qomf()->descendantNode('a', $snippetNode->getPath())); } else { $qb->where($qb->qomf()->childNode('a', $snippetNode->getPath())); } $qb->andWhere($qb->qomf()->comparison($qb->qomf()->propertyValue('a', 'jcr:mixinTypes'), QueryObjectModelConstantsInterface::JCR_OPERATOR_EQUAL_TO, $qb->qomf()->literal('sulu:snippet'))); if (null !== $offset) { $qb->setFirstResult($offset); if (null === $max) { // we get zero results if no max specified throw new \InvalidArgumentException('If you specify an offset then you must also specify $max'); } $qb->setMaxResults($max); } if (null !== $search) { $searchConstraint = $qf->orConstraint($qf->comparison($qf->propertyValue('a', 'i18n:' . $languageCode . '-title'), QueryObjectModelConstantsInterface::JCR_OPERATOR_LIKE, $qf->literal('%' . $search . '%')), $qf->comparison($qf->propertyValue('a', 'template'), QueryObjectModelConstantsInterface::JCR_OPERATOR_LIKE, $qf->literal('%' . $search . '%'))); $qb->andWhere($searchConstraint); } // Title is a mandatory property for snippets // NOTE: Prefixing the language code and namespace here is bad. But the solution is // refactoring (i.e. a node property name translator service). $sortOrder = $sortOrder !== null ? strtoupper($sortOrder) : 'ASC'; $sortBy = $sortBy !== null ? $sortBy : 'title'; $qb->orderBy($qb->qomf()->propertyValue('a', 'i18n:' . $languageCode . '-' . $sortBy), $sortOrder !== null ? strtoupper($sortOrder) : 'ASC'); return $qb->getQuery(); }
/** * Returns list of custom-url data-arrays. * * @param string $path * * @return \Iterator */ public function findUrls($path) { $session = $this->sessionManager->getSession(); $queryManager = $session->getWorkspace()->getQueryManager(); $qomFactory = $queryManager->getQOMFactory(); $queryBuilder = new QueryBuilder($qomFactory); $queryBuilder->addSelect('a', 'domainParts', 'domainParts')->addSelect('a', 'baseDomain', 'baseDomain'); $queryBuilder->from($queryBuilder->qomf()->selector('a', 'nt:unstructured')); $queryBuilder->where($queryBuilder->qomf()->comparison($queryBuilder->qomf()->propertyValue('a', 'jcr:mixinTypes'), QueryObjectModelConstantsInterface::JCR_OPERATOR_EQUAL_TO, $queryBuilder->qomf()->literal('sulu:custom_url'))); $queryBuilder->andWhere($queryBuilder->qomf()->descendantNode('a', $path)); $query = $queryBuilder->getQuery(); $result = $query->execute(); return array_map(function (Row $item) { return $this->generator->generate($item->getValue('a.baseDomain'), json_decode($item->getValue('a.domainParts'), true)); }, iterator_to_array($result->getRows())); }