/**
  * Given an SMWDescription that is just a conjunction or disjunction of
  * SMWValueDescription objects, create and return a plain WHERE condition
  * string for it.
  *
  * @param $query
  * @param SMWDescription $description
  * @param SMWSQLStore3Table $proptable
  * @param SMWDataItemHandler $diHandler for that table
  * @param string $operator SQL operator "AND" or "OR"
  */
 protected function compileValueDescription($query, SMWValueDescription $description, SMWSQLStore3Table $proptable, SMWDataItemHandler $diHandler, $operator)
 {
     $where = '';
     $dataItem = $description->getDataItem();
     // 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
     $diType = $dataItem->getDIType();
     // Try comparison based on value field and comparator,
     // but only if no join with SMW IDs table is needed.
     if ($diType != SMWDataItem::TYPE_WIKIPAGE) {
         // Do not support smw_id joined data for now.
         // See if the getSQLCondition method exists and call it if this is the case.
         if (method_exists($description, 'getSQLCondition')) {
             $fields = $diHandler->getTableFields();
             $where = $description->getSQLCondition($query->alias, array_keys($fields), $this->m_dbs);
         }
         if ($where == '') {
             $indexField = $diHandler->getIndexField();
             //Hack to get to the field used as index
             $keys = $diHandler->getWhereConds($dataItem);
             $value = $keys[$indexField];
             switch ($description->getComparator()) {
                 case SMW_CMP_EQ:
                     $comparator = '=';
                     break;
                 case SMW_CMP_LESS:
                     $comparator = '<';
                     break;
                 case SMW_CMP_GRTR:
                     $comparator = '>';
                     break;
                 case SMW_CMP_LEQ:
                     $comparator = '<=';
                     break;
                 case SMW_CMP_GEQ:
                     $comparator = '>=';
                     break;
                 case SMW_CMP_NEQ:
                     $comparator = '!=';
                     break;
                 case SMW_CMP_LIKE:
                 case SMW_CMP_NLKE:
                     if ($description->getComparator() == SMW_CMP_LIKE) {
                         $comparator = ' LIKE ';
                     } else {
                         $comparator = ' NOT LIKE ';
                     }
                     // Escape to prepare string matching:
                     $value = str_replace(array('%', '_', '*', '?'), array('\\%', '\\_', '%', '_'), $value);
                     break;
                 default:
                     throw new MWException("Unsupported comparator '" . $description->getComparator() . "' in query condition.");
             }
             $where = "{$query->alias}.{$indexField}{$comparator}" . $this->m_dbs->addQuotes($value);
         }
     } else {
         // exact match (like comparator = above, but not using $valueField
         throw new MWException("Debug -- this code might be dead.");
         foreach ($diHandler->getWhereConds($dataItem) as $fieldname => $value) {
             $where .= ($where ? ' AND ' : '') . "{$query->alias}.{$fieldname}=" . $this->m_dbs->addQuotes($value);
         }
     }
     if ($where !== '') {
         $query->where .= ($query->where ? " {$operator} " : '') . "({$where})";
     }
 }
 /**
  * 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})";
     }
 }