コード例 #1
0
 public function testTryResolveSegmentForInvalidIdThrowsException()
 {
     $connection = $this->getMockBuilder('\\SMW\\MediaWiki\\Database')->disableOriginalConstructor()->getMock();
     $temporaryTableBuilder = $this->getMockBuilder('\\SMW\\SQLStore\\TableBuilder\\TemporaryTableBuilder')->disableOriginalConstructor()->getMock();
     $hierarchyTempTableBuilder = $this->getMockBuilder('\\SMW\\SQLStore\\QueryEngine\\HierarchyTempTableBuilder')->disableOriginalConstructor()->getMock();
     $instance = new QuerySegmentListProcessor($connection, $temporaryTableBuilder, $hierarchyTempTableBuilder);
     $this->setExpectedException('RuntimeException');
     $instance->doResolveQueryDependenciesById(42);
 }
コード例 #2
0
 /**
  * The new SQL store's implementation of query answering. This function
  * works in two stages: First, the nested conditions of the given query
  * object are preprocessed to compute an abstract representation of the
  * SQL query that is to be executed. Since query conditions correspond to
  * joins with property tables in most cases, this abstract representation
  * is essentially graph-like description of how property tables are joined.
  * Moreover, this graph is tree-shaped, since all query conditions are
  * tree-shaped. Each part of this abstract query structure is represented
  * by an QuerySegment object in the array m_queries.
  *
  * As a second stage of processing, the thus prepared SQL query is actually
  * executed. Typically, this means that the joins are collapsed into one
  * SQL query to retrieve results. In some cases, such as in dbug mode, the
  * execution might be restricted and not actually perform the whole query.
  *
  * The two-stage process helps to separate tasks, and it also allows for
  * better optimisations: it is left to the execution engine how exactly the
  * query result is to be obtained. For example, one could pre-compute
  * partial suib-results in temporary tables (or even cache them somewhere),
  * instead of passing one large join query to the DB (of course, it might
  * be large only if the configuration of SMW allows it). For some DBMS, a
  * step-wise execution of the query might lead to better performance, since
  * it exploits the tree-structure of the joins, which is important for fast
  * processing -- not all DBMS might be able in seeing this by themselves.
  *
  * @param Query $query
  *
  * @return mixed depends on $query->querymode
  */
 public function getQueryResult(Query $query)
 {
     if ((!$this->engineOptions->get('smwgIgnoreQueryErrors') || $query->getDescription() instanceof ThingDescription) && $query->querymode != Query::MODE_DEBUG && count($query->getErrors()) > 0) {
         return new QueryResult($query->getDescription()->getPrintrequests(), $query, array(), $this->store, false);
         // NOTE: we check this here to prevent unnecessary work, but we check
         // it after query processing below again in case more errors occurred.
     } elseif ($query->querymode == Query::MODE_NONE || $query->getLimit() < 1) {
         // don't query, but return something to printer
         return new QueryResult($query->getDescription()->getPrintrequests(), $query, array(), $this->store, true);
     }
     $db = $this->store->getConnection('mw.db.queryengine');
     $this->queryMode = $query->querymode;
     $this->querySegmentList = array();
     $this->errors = array();
     QuerySegment::$qnum = 0;
     $this->sortKeys = $query->sortkeys;
     // Anchor IT_TABLE as root element
     $rootSegmentNumber = QuerySegment::$qnum;
     $rootSegment = new QuerySegment();
     $rootSegment->joinTable = SMWSql3SmwIds::TABLE_NAME;
     $rootSegment->joinfield = "{$rootSegment->alias}.smw_id";
     $this->querySegmentListBuilder->addQuerySegment($rootSegment);
     // *** First compute abstract representation of the query (compilation) ***//
     $this->querySegmentListBuilder->setSortKeys($this->sortKeys);
     $this->querySegmentListBuilder->getQuerySegmentFrom($query->getDescription());
     // compile query, build query "plan"
     $qid = $this->querySegmentListBuilder->getLastQuerySegmentId();
     $this->querySegmentList = $this->querySegmentListBuilder->getQuerySegmentList();
     $this->errors = $this->querySegmentListBuilder->getErrors();
     if ($qid < 0) {
         // no valid/supported condition; ensure that at least only proper pages are delivered
         $qid = $rootSegmentNumber;
         $q = $this->querySegmentList[$rootSegmentNumber];
         $q->where = "{$q->alias}.smw_iw!=" . $db->addQuotes(SMW_SQL3_SMWIW_OUTDATED) . " AND {$q->alias}.smw_iw!=" . $db->addQuotes(SMW_SQL3_SMWREDIIW) . " AND {$q->alias}.smw_iw!=" . $db->addQuotes(SMW_SQL3_SMWBORDERIW) . " AND {$q->alias}.smw_iw!=" . $db->addQuotes(SMW_SQL3_SMWINTDEFIW);
         $this->querySegmentList[$rootSegmentNumber] = $q;
     }
     if (isset($this->querySegmentList[$qid]->joinTable) && $this->querySegmentList[$qid]->joinTable != SMWSql3SmwIds::TABLE_NAME) {
         // manually make final root query (to retrieve namespace,title):
         $rootid = $rootSegmentNumber;
         $qobj = $this->querySegmentList[$rootSegmentNumber];
         $qobj->components = array($qid => "{$qobj->alias}.smw_id");
         $qobj->sortfields = $this->querySegmentList[$qid]->sortfields;
         $this->querySegmentList[$rootSegmentNumber] = $qobj;
     } else {
         // not such a common case, but worth avoiding the additional inner join:
         $rootid = $qid;
     }
     // var_dump( json_encode( $this->querySegmentList, JSON_PRETTY_PRINT ) );
     // Include order conditions (may extend query if needed for sorting):
     if ($this->engineOptions->get('smwgQSortingSupport')) {
         $this->applyOrderConditions($rootid);
     }
     // Possibly stop if new errors happened:
     if (!$this->engineOptions->get('smwgIgnoreQueryErrors') && $query->querymode != Query::MODE_DEBUG && count($this->errors) > 0) {
         $query->addErrors($this->errors);
         return new QueryResult($query->getDescription()->getPrintrequests(), $query, array(), $this->store, false);
     }
     // *** Now execute the computed query ***//
     $this->querySegmentListProcessor->setQueryMode($this->queryMode);
     $this->querySegmentListProcessor->setQuerySegmentList($this->querySegmentList);
     // execute query tree, resolve all dependencies
     $this->querySegmentListProcessor->doResolveQueryDependenciesById($rootid);
     $this->applyExtraWhereCondition($rootid);
     switch ($query->querymode) {
         case Query::MODE_DEBUG:
             $result = $this->getDebugQueryResult($query, $rootid);
             break;
         case Query::MODE_COUNT:
             $result = $this->getCountQueryResult($query, $rootid);
             break;
         default:
             $result = $this->getInstanceQueryResult($query, $rootid);
             break;
     }
     $this->querySegmentListProcessor->cleanUp();
     $query->addErrors($this->errors);
     return $result;
 }