public function optimize(Erfurt_Sparql_Query $query) { $resultForm = $query->getResultForm(); $result = $query; // TODO Not working on all queries yet. #return $result; if ($resultForm === 'select distinct') { $result = $this->_optimizeDistinct($query); } return $result; }
public static function getSelect(Erfurt_Sparql_Query $query, $arSqls, $strAdditional = '') { if (count($arSqls) == 1) { return implode('', $arSqls[0]) . $strAdditional; } //union $strUnion = 'UNION' . ($query->getResultForm() == 'select distinct' ? '' : ' ALL'); $ar = array(); foreach ($arSqls as $arSql) { $ar[] = implode('', $arSql) . $strAdditional; } return '(' . implode(') ' . $strUnion . ' (', $ar) . ')'; }
/** * Simplify the query by flattening out subqueries. * Modifies the passed query object directly. */ public function simplify(Erfurt_Sparql_Query $query) { #return; $arPatterns = $query->getResultPart(); self::dropEmpty($arPatterns); $arPlan = $this->createPlan($arPatterns); if (count($arPlan) == 0) { $query->setResultPart($arPatterns); return 0; } $this->executePlan($arPatterns, $arPlan); $query->setResultPart($arPatterns); }
/** * 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; }
/** * Checks if $token is a variable. * * @param string $token The token * @return boolean true if the token is a variable false if not */ protected function _varCheck($token) { if (isset($token[0]) && ($token[0] == '$' || $token[0] == '?')) { $this->_query->addUsedVar($token); return true; } return false; }
public function __construct(Erfurt_Sparql_Query $query, array $arModelIdMapping) { $this->query = $query; $this->arModelIds = array(); foreach ($query->getFromPart() as $from) { if (isset($arModelIdMapping[$from])) { $this->arModelIds[] = $arModelIdMapping[$from]['modelId']; } else { $this->arModelIds[] = -1; } } foreach ($query->getFromNamedPart() as $from) { if (isset($arModelIdMapping[$from])) { $this->arModelIds[] = $arModelIdMapping[$from]['modelId']; } else { $this->arModelIds[] = -1; } } }
/** * 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); } }
public function __construct($variable) { $this->_variable = $variable; $this->_language = Erfurt_Sparql_Query::getLanguageTag($variable); }