/** * {@inheritDoc} */ public function query(Query $query) { $this->assertLoggedIn(); if (!$query instanceof QueryObjectModelInterface) { $parser = new Sql2ToQomQueryConverter($this->factory->get('Query\\QOM\\QueryObjectModelFactory')); try { $qom = $parser->parse($query->getStatement()); $qom->setLimit($query->getLimit()); $qom->setOffset($query->getOffset()); } catch (\Exception $e) { throw new InvalidQueryException('Invalid query: ' . $query->getStatement(), null, $e); } } else { $qom = $query; } $qomWalker = new QOMWalker($this->nodeTypeManager, $this->getConnection(), $this->getNamespaces()); list($selectors, $selectorAliases, $sql) = $qomWalker->walkQOMQuery($qom); $primarySource = reset($selectors); $primaryType = $primarySource->getSelectorName() ?: $primarySource->getNodeTypeName(); $data = $this->getConnection()->fetchAll($sql, array($this->workspaceName)); $results = $properties = $standardColumns = array(); foreach ($data as $row) { $result = array(); /** @var SelectorInterface $selector */ foreach ($selectors as $selector) { $selectorName = $selector->getSelectorName() ?: $selector->getNodeTypeName(); $columnPrefix = isset($selectorAliases[$selectorName]) ? $selectorAliases[$selectorName] . '_' : $selectorAliases[''] . '_'; if ($primaryType === $selector->getNodeTypeName()) { $result[] = array('dcr:name' => 'jcr:path', 'dcr:value' => $row[$columnPrefix . 'path'], 'dcr:selectorName' => $selectorName); } $result[] = array('dcr:name' => 'jcr:path', 'dcr:value' => $row[$columnPrefix . 'path'], 'dcr:selectorName' => $selectorName); $result[] = array('dcr:name' => 'jcr:score', 'dcr:value' => 0, 'dcr:selectorName' => $selectorName); if (0 === count($qom->getColumns())) { $selectorPrefix = null !== $selector->getSelectorName() ? $selectorName . '.' : ''; $result[] = array('dcr:name' => $selectorPrefix . 'jcr:primaryType', 'dcr:value' => $primaryType, 'dcr:selectorName' => $selectorName); } if (isset($row[$columnPrefix . 'props'])) { $propertyNames = array(); $columns = $qom->getColumns(); // Always populate jcr:created and jcr:createdBy if a wildcard selector is used. // This emulates the behavior of Jackrabbit if (0 === count($columns)) { $propertyNames = array('jcr:created', 'jcr:createdBy'); } foreach ($columns as $column) { if (!$column->getSelectorName() || $column->getSelectorName() == $selectorName) { $propertyNames[] = $column->getPropertyName(); } } $properties[$selectorName] = (array) $this->xmlToColumns($row[$columnPrefix . 'props'], $propertyNames); } else { $properties[$selectorName] = array(); } // TODO: add other default columns that Jackrabbit provides to provide a more consistent behavior if (isset($properties[$selectorName]['jcr:createdBy'])) { $standardColumns[$selectorName]['jcr:createdBy'] = $properties[$selectorName]['jcr:createdBy']; } if (isset($properties[$selectorName]['jcr:created'])) { $standardColumns[$selectorName]['jcr:created'] = $properties[$selectorName]['jcr:created']; } } $reservedNames = array('jcr:path', 'jcr:score'); foreach ($qom->getColumns() as $column) { $selectorName = $column->getSelectorName(); $columnName = $column->getPropertyName(); $columnPrefix = isset($selectorAliases[$selectorName]) ? $selectorAliases[$selectorName] . '_' : $selectorAliases[''] . '_'; if (in_array($column->getColumnName(), $reservedNames)) { throw new InvalidQueryException(sprintf('Cannot reserved name "%s". Reserved names are "%s"', $column->getColumnName(), implode('", "', $reservedNames))); } $dcrValue = 'jcr:uuid' === $columnName ? $row[$columnPrefix . 'identifier'] : (isset($properties[$selectorName][$columnName]) ? $properties[$selectorName][$columnName] : ''); if (isset($standardColumns[$selectorName][$columnName])) { unset($standardColumns[$selectorName][$columnName]); } $result[] = array('dcr:name' => $column->getColumnName() === $columnName && isset($properties[$selectorName][$columnName]) ? $selectorName . '.' . $columnName : $column->getColumnName(), 'dcr:value' => $dcrValue, 'dcr:selectorName' => $selectorName ?: $primaryType); } foreach ($standardColumns as $selectorName => $columns) { foreach ($columns as $columnName => $value) { $result[] = array('dcr:name' => $primaryType . '.' . $columnName, 'dcr:value' => $value, 'dcr:selectorName' => $selectorName); } } $results[] = $result; } return $results; }
/** * @expectedException \PHPCR\Query\InvalidQueryException */ public function testDescendantQueryTrailingSlash() { $this->nodeTypeManager->expects($this->once())->method('getSubtypes')->will($this->returnValue(array())); $query = $this->factory->createQuery($this->factory->selector('nt:unstructured', 'nt:unstructured'), $this->factory->descendantNode('nt:unstructured', '/some/node/')); $this->walker->walkQOMQuery($query); }