/** * Converts the database results into the desired output format * and returns the result. * * @param array $arRecordSets Array of (possibly several) SQL query results. * @param Query $query SPARQL query object * @param SparqlEngineDb $engine Sparql Engine to query the database * @return mixed The result as rendered by the result renderers. */ public function convertFromDbResults($arRecordSets, Query $query, SparqlEngineDb $engine) { $this->query = $query; $this->engine = $engine; $this->sg = $engine->getSqlGenerator(); $strResultForm = $this->query->getResultForm(); switch ($strResultForm) { case 'construct': case 'select': case 'select distinct': $arResult = $this->getVariableArrayFromRecordSets($arRecordSets, $strResultForm); //some result forms need more modification switch ($strResultForm) { case 'construct': $arResult = $this->constructGraph($arResult, $this->query->getConstructPattern()); break; case 'describe': $arResult = $this->describeGraph($arResult); break; } return $arResult; break; case 'count': case 'ask': if (count($arRecordSets) > 1) { throw new Exception('More than one result set for a ' . $strResultForm . ' query!'); } $nCount = 0; $dbRecordSet = reset($arRecordSets); foreach ($dbRecordSet as $row) { $nCount += intval($row['count']); break; } if ($strResultForm == 'ask') { return $nCount > 0; } else { return $nCount; } break; case 'describe': default: throw new Exception('Unsupported result form: ' . $strResultForm); } }
/** * Converts the database results into JSON Format * * @param array $arRecordSets Array of (possibly several) SQL query results. * @param Query $query SPARQL query object * @param SparqlEngineDb $engine Sparql Engine to query the database * @return mixed HTML code */ public function convertFromDbResults($arRecordSets, Query $query, SparqlEngineDb $engine) { $this->query = $query; $this->sg = $engine->getSqlGenerator(); $strResultForm = $query->getResultForm(); foreach ($this->getResultVars() as $var) { $ResultVarsTemp[] = substr($var, 1); } switch ($strResultForm) { case 'select': case 'select distinct': $results = $this->createFromRecords($arRecordSets, $strResultForm); $strCode = json_encode(array('head' => array('vars' => $ResultVarsTemp), 'results' => array('bindings' => $results))); //$strCode = str_replace(',{',','.PHP_EOL.'{',$strCode); break; case 'construct': case 'describe': throw new Exception('Construct and describe are not supported by the' . ' JSON renderer'); case 'count': case 'ask': if (count($arRecordSets) > 1) { throw new Exception('More than one result set for a ' . $strResultForm . ' query not supported by JSON Renderer'); } $nCount = 0; $dbRecordSet = reset($arRecordSets); foreach ($dbRecordSet as $row) { $nCount += intval($row['count']); break; } if ($strResultForm == 'ask') { $strcode = json_encode(array('boolean' => $nCount > 0)); } else { $strcode = json_encode(array('int' => $nCount)); } break; default: throw new Exception('Error'); } return $strCode; }
/** * Converts the database results into nice HTML. * * @param array $arRecordSets Array of (possibly several) SQL query results. * @param Query $query SPARQL query object * @param SparqlEngineDb $engine Sparql Engine to query the database * @return mixed HTML code */ public function convertFromDbResults($arRecordSets, Query $query, SparqlEngineDb $engine) { $this->query = $query; $this->sg = $engine->getSqlGenerator(); $strCode = ''; $strResultForm = $query->getResultForm(); switch ($strResultForm) { case 'select': case 'select distinct': $strCode = $this->createTableFromRecords($arRecordSets); break; case 'construct': case 'describe': throw new Exception('Construct and describe are currently not supported by the' . ' HTML renderer'); case 'count': case 'ask': if (count($arRecordSets) > 1) { throw new Exception('More than one result set for a ' . $strResultForm . ' query!'); } $nCount = 0; $dbRecordSet = reset($arRecordSets); foreach ($dbRecordSet as $row) { $nCount += intval($row['count']); break; } if ($strResultForm == 'ask') { $strCode = 'There were results.'; } else { $strCode = 'There are ' . $nCount . ' results.'; } break; default: throw new Exception('Unsupported result form: ' . $strResultForm); } return $this->wrapCode($strCode); }