/** * This method sends a SPARQL query to the store. * * @param string $query The SPARQL query to send to the store. * @param array $options optional It contains key-value pairs and should provide additional * introductions for the store and/or its adapter(s). * @return Result Returns result of the query. Its type depends on the type of the query. * @throws \Exception If query is no string. * @throws \Exception If query is malformed. * @throws StoreException If server returned an error. * @todo add support for DESCRIBE queries */ public function query($query, array $options = array()) { $queryObject = $this->queryFactory->createInstanceByQueryString($query); $queryParts = $queryObject->getQueryParts(); /** * SPARQL query (usually to fetch data) */ if ('selectQuery' == AbstractQuery::getQueryType($query)) { $resultArray = json_decode($this->client->sendSparqlSelectQuery($query), true); $entries = array(); /** * go through all bindings and create according objects for SetResult instance. * * $bindingParts will look like: * * array( * 's' => array( * 'type' => 'uri', * 'value' => '...' * ), ... * ) */ foreach ($resultArray['results']['bindings'] as $bindingParts) { $newEntry = array(); /** * A part looks like: * array( * 'type' => 'uri', * 'value' => '...' * ) */ foreach ($bindingParts as $variable => $part) { switch ($part['type']) { /** * Literal (language'd) */ case 'literal': $lang = null; if (isset($part['xml:lang'])) { $lang = $part['xml:lang']; } $newEntry[$variable] = $this->nodeFactory->createLiteral($part['value'], 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString', $lang); break; /** * Typed-Literal */ /** * Typed-Literal */ case 'typed-literal': $newEntry[$variable] = $this->nodeFactory->createLiteral($part['value'], $part['datatype']); break; /** * NamedNode */ /** * NamedNode */ case 'uri': $newEntry[$variable] = $this->nodeFactory->createNamedNode($part['value']); break; /** * BlankNode */ /** * BlankNode */ case 'bnode': $newEntry[$variable] = $this->nodeFactory->createBlankNode($part['value']); break; default: throw new \Exception('Unknown type given.'); break; } } $entries[] = $newEntry; } $return = $this->resultFactory->createSetResult($entries); $return->setVariables($resultArray['head']['vars']); /** * SPARPQL Update query */ } else { $result = $this->client->sendSparqlUpdateQuery($query); $decodedResult = json_decode($result, true); if ('askQuery' === AbstractQuery::getQueryType($query)) { $askResult = json_decode($result, true); if (true === isset($askResult['boolean'])) { $return = $this->resultFactory->createValueResult($askResult['boolean']); // assumption here is, if a string was returned, something went wrong. } elseif (0 < strlen($result)) { throw new \Exception($result); } else { $return = $this->resultFactory->createEmptyResult(); } // usually a SPARQL result does not return a string. if it does anyway, assume there is an error. } elseif (null === $decodedResult && 0 < strlen($result)) { throw new \Exception($result); } else { $return = $this->resultFactory->createEmptyResult(); } } return $return; }