/**
  * Given an Description that is just a conjunction or disjunction of
  * ValueDescription objects, create and return a plain WHERE condition
  * string for it.
  *
  * @param $query
  * @param ValueDescription $description
  * @param DataItemHandler $diHandler for that table
  * @param string $operator SQL operator "AND" or "OR"
  */
 private function mapValueDescription($query, ValueDescription $description, DataItemHandler $diHandler, $operator)
 {
     $where = '';
     $dataItem = $description->getDataItem();
     $db = $this->queryBuilder->getStore()->getConnection('mw.db');
     // TODO Better get the handle from the property type
     // Some comparators (e.g. LIKE) could use DI values of
     // a different type; we care about the property table, not
     // about the value
     // Do not support smw_id joined data for now.
     $indexField = $diHandler->getIndexField();
     //Hack to get to the field used as index
     $keys = $diHandler->getWhereConds($dataItem);
     $value = $keys[$indexField];
     // See if the getSQLCondition method exists and call it if this is the case.
     // Invoked by SMAreaValueDescription, SMGeoCoordsValueDescription
     if (method_exists($description, 'getSQLCondition')) {
         $fields = $diHandler->getTableFields();
         $where = $description->getSQLCondition($query->alias, array_keys($fields), $this->queryBuilder->getStore()->getConnection(DB_SLAVE));
     }
     if ($where == '') {
         $comparator = $this->comparatorMapper->mapComparator($description, $value);
         $where = "{$query->alias}.{$indexField}{$comparator}" . $db->addQuotes($value);
     }
     if ($where !== '') {
         if ($query->where && substr($query->where, -1) != '(') {
             $query->where .= " {$operator} ";
         }
         $query->where .= "({$where})";
     }
 }
 /**
  * @dataProvider comparatorProvider
  */
 public function testSQLComparatorElement($comparator, $value, $expected)
 {
     $valueDescription = $this->getMockBuilder('\\SMW\\Query\\Language\\ValueDescription')->disableOriginalConstructor()->getMock();
     $valueDescription->expects($this->once())->method('getComparator')->will($this->returnValue($comparator));
     $instance = new ComparatorMapper();
     $this->assertEquals($expected['comparator'], $instance->mapComparator($valueDescription, $value));
     $this->assertEquals($expected['value'], $value);
 }
 /**
  * Only type '_wpg' objects can appear on query level (essentially as nominal classes)
  *
  * @since 2.2
  *
  * @param Description $description
  *
  * @return QuerySegment
  */
 public function interpretDescription(Description $description)
 {
     $query = new QuerySegment();
     if (!$description->getDataItem() instanceof DIWikiPage) {
         return $query;
     }
     if ($description->getComparator() === SMW_CMP_EQ) {
         $query->type = QuerySegment::Q_VALUE;
         $oid = $this->querySegmentListBuilder->getStore()->getObjectIds()->getSMWPageID($description->getDataItem()->getDBkey(), $description->getDataItem()->getNamespace(), $description->getDataItem()->getInterwiki(), $description->getDataItem()->getSubobjectName());
         $query->joinfield = array($oid);
     } else {
         // Join with SMW IDs table needed for other comparators (apply to title string).
         $query->joinTable = SMWSql3SmwIds::TABLE_NAME;
         $query->joinfield = "{$query->alias}.smw_id";
         $value = $description->getDataItem()->getSortKey();
         $comparator = $this->comparatorMapper->mapComparator($description, $value);
         $query->where = "{$query->alias}.smw_sortkey{$comparator}" . $this->querySegmentListBuilder->getStore()->getConnection('mw.db')->addQuotes($value);
     }
     return $query;
 }