Exemple #1
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) {
         // 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');
     $this->queryMode = $query->querymode;
     $this->querySegmentList = array();
     $this->errors = array();
     QuerySegment::$qnum = 0;
     $this->sortKeys = $query->sortkeys;
     // *** First compute abstract representation of the query (compilation) ***//
     $this->querySegmentListBuilder->setSortKeys($this->sortKeys);
     $this->querySegmentListBuilder->buildQuerySegmentFor($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 = QuerySegment::$qnum;
         $q = new QuerySegment();
         $q->joinTable = SMWSql3SmwIds::TABLE_NAME;
         $q->joinfield = "{$q->alias}.smw_id";
         $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[$qid] = $q;
     }
     if (isset($this->querySegmentList[$qid]->joinTable) && $this->querySegmentList[$qid]->joinTable != SMWSql3SmwIds::TABLE_NAME) {
         // manually make final root query (to retrieve namespace,title):
         $rootid = QuerySegment::$qnum;
         $qobj = new QuerySegment();
         $qobj->joinTable = SMWSql3SmwIds::TABLE_NAME;
         $qobj->joinfield = "{$qobj->alias}.smw_id";
         $qobj->components = array($qid => "{$qobj->alias}.smw_id");
         $qobj->sortfields = $this->querySegmentList[$qid]->sortfields;
         $this->querySegmentList[$rootid] = $qobj;
     } else {
         // not such a common case, but worth avoiding the additional inner join:
         $rootid = $qid;
     }
     // 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->doExecuteSubqueryJoinDependenciesFor($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;
 }