/** * Adds a graph pattern to the result part. * * @param Erfurt_Sparql_GraphPattern $pattern */ public function addGraphPattern($pattern) { $pattern->setId($this->graphPatternCounter++); $this->resultPart[] = $pattern; }
/** * Creates some SQL statements from the given triple pattern * array. * * @param QueryTriple $triple Array containing subject, predicate and object * @param GraphPattern $graphPattern Graph pattern object * * @return array Array consisting of on array and two string values: * SELECT, FROM and WHERE part */ function getTripleSql(Erfurt_Sparql_QueryTriple $triple, Erfurt_Sparql_GraphPattern $graphPattern, $arResultVars) { $arSelect = array(); $strFrom = null; $strWhere = null; $strWhereEquality = ''; $bWhereEqualitySubject = false; $bWhereEqualityPredicate = false; $bWhereEqualityObject = false; $subject = $triple->getSubject(); $predicate = $triple->getPredicate(); $object = $triple->getObject(); $arRefVars = array(); $strTablePrefix = 't' . $this->nTableId; /** * SELECT part * We do select only the columns we need for variables */ require_once 'Erfurt/Sparql/Variable.php'; if (Erfurt_Sparql_Variable::isVariable($subject)) { if (isset($this->arUnionVarAssignments[$this->nUnionCount][$subject])) { //already selected -> add equality check $bWhereEqualitySubject = true; $this->arUsedVarTypes[$subject]['s'] = true; } else { #if (isset($this->arVarAssignments[$subject][0])) { # $strTablePrefix = $this->arVarAssignments[$subject][0]; #} if (!isset($this->arVarAssignments[$subject])) { $this->arVarAssignments[$subject] = array($strTablePrefix, 's'); } #$this->arVarAssignments[$subject] = array($strTablePrefix, 's'); $this->arVarAssignments[$subject][1] = 's'; $this->arUnionVarAssignments[$this->nUnionCount][$subject] = array($strTablePrefix, 's'); $this->arUsedVarTypes[$subject]['s'] = true; if ($this->isResultVar($subject, $arResultVars)) { //new variable that needs to be selected $arSelect[$subject] = $this->createVariableSelectArray('s', $subject, $strTablePrefix); if (isset($this->arUsedVarAssignments[$subject])) { $arRefVars[$subject] = $strTablePrefix . '.s'; } else { $this->arUsedVarAssignments[$subject] = $strTablePrefix . '.s'; } } } } else { if (strlen($subject) > 160) { $subject = substr((string) $subject, 0, 128) . md5((string) $subject); } } if (Erfurt_Sparql_Variable::isVariable($predicate)) { if (isset($this->arUnionVarAssignments[$this->nUnionCount][$predicate])) { //already selected -> add equality check $bWhereEqualityPredicate = true; $this->arUsedVarTypes[$predicate]['p'] = true; } else { #if (isset($this->arVarAssignments[$predicate][0])) { # $strTablePrefix = $this->arVarAssignments[$predicate][0]; #} if (!isset($this->arVarAssignments[$predicate])) { $this->arVarAssignments[$predicate] = array($strTablePrefix, 'p'); } #$this->arVarAssignments[$predicate] = array($strTablePrefix, 'p'); $this->arVarAssignments[$predicate][1] = 'p'; $this->arUnionVarAssignments[$this->nUnionCount][$predicate] = array($strTablePrefix, 'p'); $this->arUsedVarTypes[$predicate]['p'] = true; if ($this->isResultVar($predicate, $arResultVars)) { $arSelect[$predicate] = $this->createVariableSelectArray('p', $predicate, $strTablePrefix); if (isset($this->arUsedVarAssignments[$predicate])) { $arRefVars[$predicate] = $strTablePrefix . '.p'; } else { $this->arUsedVarAssignments[$predicate] = $strTablePrefix . '.p'; } } } } else { if (strlen($predicate) > 160) { $predicate = substr((string) $predicate, 0, 128) . md5((string) $predicate); } } if (Erfurt_Sparql_Variable::isVariable($object)) { if (isset($this->arUnionVarAssignments[$this->nUnionCount][$object])) { //already selected -> add equality check $bWhereEqualityObject = true; $this->arUsedVarTypes[$object]['o'] = true; } else { #if (isset($this->arVarAssignments[$object][0])) { # $strTablePrefix = $this->arVarAssignments[$object][0]; #} if (!isset($this->arVarAssignments[$object])) { $this->arVarAssignments[$object] = array($strTablePrefix, 'o'); } $this->arVarAssignments[$object][1] = 'o'; $this->arUnionVarAssignments[$this->nUnionCount][$object] = array($strTablePrefix, 'o'); $this->arUsedVarTypes[$object]['o'] = true; if ($this->isResultVar($object, $arResultVars)) { $arSelect[$object] = $this->createVariableSelectArray('o', $object, $strTablePrefix); if (isset($this->arUsedVarAssignments[$object])) { $arRefVars[$object] = $strTablePrefix . '.o'; } else { $this->arUsedVarAssignments[$object] = $strTablePrefix . '.o'; } } if (isset($this->query->varLanguages[$object]) && $this->query->varLanguages[$object] !== null) { $strWhereEquality .= ' AND ' . $strTablePrefix . '.ol="' . addslashes($this->query->varLanguages[$object]) . '"'; } if (isset($this->query->varDatatypes[$object]) && $this->query->varDatatypes[$object] !== null) { $strWhereEquality .= ' AND ' . $strTablePrefix . '.ol="' . addslashes($this->query->varDatatypes[$object]) . '"'; } } } else { if (strlen($object) > 160) { $object = substr((string) $object, 0, 128) . md5((string) $object); } } /** * WhereEquality - needs to be done now because strTablePrefix may change */ if ($bWhereEqualitySubject) { $strWhereEquality .= ' AND ' . $this->getSqlEqualityCondition(array($strTablePrefix, 's'), $this->arVarAssignments[$subject]); } if ($bWhereEqualityPredicate) { $strWhereEquality .= ' AND ' . $this->getSqlEqualityCondition(array($strTablePrefix, 'p'), $this->arVarAssignments[$predicate]); } if ($bWhereEqualityObject) { $strWhereEquality .= ' AND ' . $this->getSqlEqualityCondition(array($strTablePrefix, 'o'), $this->arVarAssignments[$object]); } /** * FROM part */ //receive tableName from QueryCache ; if QueryCache dont have some special views //the original tableName will be chosen $tableName = $this->tblStatements; if ($viewName = Erfurt_App::getInstance()->getQueryCache()->getMaterializedViewName($subject, $predicate, $object)) { $tableName = $viewName; } if ($this->nUnionTriplePatternCount == 0) { //first FROM $strFrom = $tableName . ' as ' . $strTablePrefix; } else { //normal join if (count($this->arModelIds) == 1) { $strFrom = 'LEFT JOIN ' . $tableName . ' as ' . $strTablePrefix . ' ON t0.g=' . $strTablePrefix . '.g'; } else { if (count($this->arModelIds) > 1) { $arIDs = array(); foreach ($this->arModelIds as $nId) { $arIDs[] = $strTablePrefix . '.g=' . intval($nId); } $strFrom = 'LEFT JOIN ' . $tableName . ' as ' . $strTablePrefix . ' ON (' . implode(' OR ', $arIDs) . ')'; } else { $strFrom = 'LEFT JOIN ' . $tableName . ' as ' . $strTablePrefix . ' ON t0.g=' . $strTablePrefix . '.g'; } } if ($graphPattern->getOptional() !== null) { $strFrom .= $this->getSqlCondition($subject, $strTablePrefix, 's') . $this->getSqlCondition($predicate, $strTablePrefix, 'p') . $this->getSqlCondition($object, $strTablePrefix, 'o') . $strWhereEquality; } } /** * WHERE part */ if ($this->nUnionTriplePatternCount == 0) { if (count($this->arModelIds) == 1) { $strWhere = $strTablePrefix . '.g=' . intval(reset($this->arModelIds)); } else { if (count($this->arModelIds) > 1) { $arIDs = array(); foreach ($this->arModelIds as $nId) { $arIDs[] = $strTablePrefix . '.g=' . intval($nId); } $strWhere = '(' . implode(' OR ', $arIDs) . ')'; } else { //so that we can append an AND $strWhere = '1'; } } } if ($graphPattern->getOptional() === null || $this->nGraphPatternCount == 0) { $strWhere .= $this->getSqlCondition($subject, $strTablePrefix, 's') . $this->getSqlCondition($predicate, $strTablePrefix, 'p') . $this->getSqlCondition($object, $strTablePrefix, 'o') . $strWhereEquality; } return array($arSelect, $strFrom, $strWhere); }
/** * Constructs a result graph. * * @param array $arVartable A table containing the result vars and their bindings. * @param Erfurt_Sparql_GraphPattern $constructPattern The CONSTRUCT pattern. * @return array */ protected function _constructGraph($arVartable, $constructPattern) { $resultGraph = array(); if (!$arVartable) { return $resultGraph; } $tp = $constructPattern->getTriplePatterns(); $bnode = 0; foreach ($arVartable as $value) { foreach ($tp as $triple) { $subVar = substr($triple->getSubject(), 1); $predVar = substr($triple->getPredicate(), 1); $objVar = substr($triple->getObject(), 1); $sub = $value["{$subVar}"]['value']; $pred = $value["{$predVar}"]['value']; $obj = $value["{$objVar}"]; if (!isset($resultGraph["{$sub}"])) { $resultGraph["{$sub}"] = array(); } if (!isset($resultGraph["{$sub}"]["{$pred}"])) { $resultGraph["{$sub}"]["{$pred}"] = array(); } $resultGraph["{$sub}"]["{$pred}"][] = $obj; } } return $resultGraph; }