/** * @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; } $comparator = $description->getComparator(); $value = $description->getDataItem()->getSortKey(); // A simple value match using the `~~Foo` will initiate a fulltext // search without being bound to a property allowing a broad match // search if (($comparator === SMW_CMP_LIKE || $comparator === SMW_CMP_NLKE) && strpos($value, '~') !== false) { $fulltextSearchSupport = $this->addFulltextSearchCondition($query, $comparator, $value); if ($fulltextSearchSupport) { return $query; } } if ($comparator === 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"; $comparator = $this->comparatorMapper->mapComparator($description, $value); $db = $this->querySegmentListBuilder->getStore()->getConnection('mw.db.queryengine'); $query->where = "{$query->alias}.smw_sortkey{$comparator}" . $db->addQuotes($value); } return $query; }
/** * 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->querySegmentListBuilder->getStore()->getConnection('mw.db.queryengine'); $valueMatchConditionBuilder = $this->fulltextSearchTableFactory->newValueMatchConditionBuilderByType($this->querySegmentListBuilder->getStore()); // 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->querySegmentListBuilder->getStore()->getConnection(DB_SLAVE)); } if ($where == '' && $valueMatchConditionBuilder->canApplyFulltextSearchMatchCondition($description)) { $query->joinTable = $valueMatchConditionBuilder->getTableName(); $query->sortIndexField = $valueMatchConditionBuilder->getSortIndexField($query->alias); $query->components = array(); $where = $valueMatchConditionBuilder->getWhereCondition($description, $query->alias); } elseif ($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})"; } }