/** * Executes multiple SQL queries and returns an array of results. * * @param array $arSqls Array of SQL queries. * @return array Array of query results. */ protected function _queryMultiple($arSqls) { $arSM = $this->query->getSolutionModifier(); if ($arSM['limit'] === null && $arSM['offset'] === null) { $nOffset = 0; $nLimit = null; $nSql = 0; } else { require_once 'Erfurt/Sparql/EngineDb/Offsetter.php'; $offsetter = new Erfurt_Sparql_EngineDb_Offsetter($this, $this->query); list($nSql, $nOffset) = $offsetter->determineOffset($arSqls); $nLimit = $arSM['limit']; } $nCount = 0; $arResults = array(); foreach ($arSqls as $nId => $arSql) { if ($nId < $nSql) { continue; } if ($nLimit != null) { $nCurrentLimit = $nLimit - $nCount; } else { $nCurrentLimit = null; } $dbResult = $this->_queryDb($arSql, $nOffset, $nCurrentLimit); $nCount += count($dbResult); $arResults[] = $dbResult; $nOffset = 0; if ($nLimit !== null && $nCount >= $nLimit) { break; } } return $arResults; }
/** * Converts the database results into the desired output format * and returns the result. * * @param array $arRecordSets Array of (possibly several) SQL query results. * @param Erfurt_Sparql_Query $query SPARQL query object * @param $engine Sparql Engine to query the database * @return array */ public function convertFromDbResults($arRecordSets, Erfurt_Sparql_Query $query, $engine, $vars) { $this->query = $query; $this->engine = $engine; $this->_vars = $vars; $strResultForm = $this->query->getResultForm(); switch ($strResultForm) { case 'construct': case 'select': case 'select distinct': switch ($strResultForm) { case 'construct': $arResult = $this->_getVariableArrayFromRecordSets($arRecordSets, $strResultForm, true); break; default: $arResult = $this->_getVariableArrayFromRecordSets($arRecordSets, $strResultForm, false); if (count($this->uriValues) > 0 || count($this->literalValues) > 0) { // If the query contains a ORDER BY wen need to reorder the result $sm = $query->getSolutionModifier(); if (null !== $sm['order by']) { foreach ($sm['order by'] as $order) { $n = count($arResult); $id = ltrim($order['val'], '?$'); while (true) { $hasChanged = false; for ($i = 0; $i < $n - 1; ++$i) { switch ($order['type']) { case 'desc': if ($arResult[$i][$id] < $arResult[$i + 1][$id]) { $dummy = $arResult[$i][$id]; $arResult[$i][$id] = $arResult[$i + 1][$id]; $arResult[$i + 1][$id] = $dummy; $hasChanged = true; } break; case 'asc': default: if ($arResult[$i][$id] > $arResult[$i + 1][$id]) { $dummy = $arResult[$i][$id]; $arResult[$i][$id] = $arResult[$i + 1][$id]; $arResult[$i + 1][$id] = $dummy; $hasChanged = true; } break; } } $n--; if (!$hasChanged && $n === 0) { break; } } } } } } //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 'count-distinct': case 'ask': if (count($arRecordSets) > 1) { require_once 'Erfurt/Exception.php'; throw new Erfurt_Exception('More than one result set for a ' . $strResultForm . ' query!'); } $nCount = 0; foreach ($arRecordSets[0] as $row) { $nCount += intval($row['count']); break; } if ($strResultForm == 'ask') { return $nCount > 0 ? true : false; } else { return $nCount; } break; case 'describe': default: throw new Exception('Yet not supported: ' . $strResultForm); } }