Example #1
0
 /**
  *   Creates an SQL statement that checks for the value
  *   of some subject/predicate/object
  *
  *   @param mixed    $bject          subject|predicate|object
  *   @param string   $strTablePrefix Table prefix (e.g. "t0")
  *   @param string   $strType        Type of $bject ('subject'|'predicate'|'object')
  *   @return string  Part of the SQL query (prefixed with AND)
  */
 function getSqlCondition($bject, $strTablePrefix, $strType)
 {
     if (is_string($bject)) {
         if (SparqlVariable::isVariable($bject)) {
             //variable?
             if (self::isPreparedVariable($bject)) {
                 //no, not really
                 $value = $this->getPreparedVariablePlaceholder($bject);
             } else {
                 //yes
                 return null;
             }
         } else {
             $value = $this->dbConn->qstr($bject);
         }
         //literal
         return ' AND ' . $strTablePrefix . '.' . $strType . ' = ' . $value;
     }
     if ($bject instanceof BlankNode) {
         //Blank node
         throw new SparqlEngineDb_SqlGeneratorException('FIXME: Querying for blank nodes not supported');
     } else {
         if ($bject instanceof Resource) {
             //Resource
             $r = ' AND ' . $strTablePrefix . '.' . $strType . ' = ' . $this->dbConn->qstr($bject->getURI());
             if ($strType !== 'predicate') {
                 $r .= ' AND ' . $strTablePrefix . '.' . $strType . '_is =' . ' "r"';
             }
             return $r;
         } else {
             if ($bject instanceof Literal) {
                 //Literal
                 //I'm doubling Filter code here, but what the hell
                 $strColDatatype = $strTablePrefix . '.l_datatype';
                 if ($bject->dtype == 'http://www.w3.org/2001/XMLSchema#integer' || $bject->dtype == 'http://www.w3.org/2001/XMLSchema#double') {
                     $strVariable = 'CAST(' . $strTablePrefix . '.' . $strType . ' AS DECIMAL(15,10))';
                     $strValue = $bject->getLabel();
                 } else {
                     $strVariable = $strTablePrefix . '.' . $strType;
                     $strValue = $this->dbConn->qstr($bject->getLabel());
                 }
                 $r = ' AND ' . $strVariable . ' = ' . $strValue;
                 if ($strType !== 'predicate') {
                     $r .= ' AND ' . $strTablePrefix . '.' . $strType . '_is =' . ' "l"';
                 }
                 if ($strType == 'object') {
                     if ($bject->dtype == '' || $bject->dtype == 'http://www.w3.org/2001/XMLSchema#string') {
                         //string
                         $r .= ' AND (' . $strColDatatype . ' = ""' . ' OR ' . $strColDatatype . ' = "http://www.w3.org/2001/XMLSchema#string"' . ')';
                     } else {
                         $r .= ' AND ' . $strColDatatype . ' = "' . $bject->dtype . '"';
                     }
                 }
                 if ($bject->lang != '') {
                     $strColLanguage = $strTablePrefix . '.l_language';
                     $r .= ' AND ' . $strColLanguage . ' = ' . $this->dbConn->qstr($bject->lang);
                 }
                 return $r;
             } else {
                 throw new SparqlEngineDb_SqlGeneratorException('Unsupported sentence part: ' . get_class($bject));
             }
         }
     }
 }
Example #2
0
 /**
  *   Checks if the given tree element is a variable
  *
  *   @param array $tree  Tree element
  *   @return boolean True if the element is a variable
  */
 protected function isVariable($tree)
 {
     return $tree['type'] == 'value' && $tree['quoted'] === false && SparqlVariable::isVariable($tree['value']) && isset($this->sg->arUnionVarAssignments[$this->nUnionCount][$tree['value']]);
 }
Example #3
0
 /**
  *   Returns an array of all variables in this triple.
  *
  *   @return array   Array of variable names
  */
 public function getVariables()
 {
     $arVars = array();
     foreach (array('subject', 'predicate', 'object') as $strVar) {
         if (SparqlVariable::isVariable($this->{$strVar})) {
             $arVars[] = $this->{$strVar};
         }
     }
     return $arVars;
 }