/** * 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 SMWSQLStore2Query 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 SMWQuery $query */ public function getQueryResult(SMWQuery $query) { global $smwgIgnoreQueryErrors, $smwgQSortingSupport; if (!$smwgIgnoreQueryErrors && $query->querymode != SMWQuery::MODE_DEBUG && count($query->getErrors()) > 0) { return new SMWQueryResult($query->getDescription()->getPrintrequests(), $query, array(), $this->m_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 == SMWQuery::MODE_NONE) { // don't query, but return something to printer return new SMWQueryResult($query->getDescription()->getPrintrequests(), $query, array(), $this->m_store, true); } $this->m_qmode = $query->querymode; $this->m_queries = array(); $this->m_hierarchies = array(); $this->m_querylog = array(); $this->m_errors = array(); SMWSQLStore2Query::$qnum = 0; $this->m_sortkeys = $query->sortkeys; // *** First compute abstract representation of the query (compilation) ***// wfProfileIn('SMWSQLStore2Queries::compileMainQuery (SMW)'); $qid = $this->compileQueries($query->getDescription()); // compile query, build query "plan" wfProfileOut('SMWSQLStore2Queries::compileMainQuery (SMW)'); if ($qid < 0) { // no valid/supported condition; ensure that at least only proper pages are delivered $qid = SMWSQLStore2Query::$qnum; $q = new SMWSQLStore2Query(); $q->jointable = 'smw_ids'; $q->joinfield = "{$q->alias}.smw_id"; $q->where = "{$q->alias}.smw_iw!=" . $this->m_dbs->addQuotes(SMW_SQL2_SMWIW) . " AND {$q->alias}.smw_iw!=" . $this->m_dbs->addQuotes(SMW_SQL2_SMWREDIIW) . " AND {$q->alias}.smw_iw!=" . $this->m_dbs->addQuotes(SMW_SQL2_SMWBORDERIW) . " AND {$q->alias}.smw_iw!=" . $this->m_dbs->addQuotes(SMW_SQL2_SMWINTDEFIW); $this->m_queries[$qid] = $q; } if ($this->m_queries[$qid]->jointable != 'smw_ids') { // manually make final root query (to retrieve namespace,title): $rootid = SMWSQLStore2Query::$qnum; $qobj = new SMWSQLStore2Query(); $qobj->jointable = 'smw_ids'; $qobj->joinfield = "{$qobj->alias}.smw_id"; $qobj->components = array($qid => "{$qobj->alias}.smw_id"); $qobj->sortfields = $this->m_queries[$qid]->sortfields; $this->m_queries[$rootid] = $qobj; } else { // not such a common case, but worth avoiding the additional inner join: $rootid = $qid; } // commented by ning, no need to sort // // Include order conditions (may extend query if needed for sorting): // if ( $smwgQSortingSupport ) { // $this->applyOrderConditions( $rootid ); // } if (!$smwgIgnoreQueryErrors && $query->querymode != SMWQuery::MODE_DEBUG && count($this->m_errors) > 0) { // stop here if new errors happened $query->addErrors($this->m_errors); return new SMWQueryResult($query->getDescription()->getPrintrequests(), $query, array(), $this->m_store, false); } // *** Now execute the computed query ***// wfProfileIn('SMWSQLStore2Queries::executeMainQuery (SMW)'); $this->executeQueries($this->m_queries[$rootid]); // execute query tree, resolve all dependencies wfProfileOut('SMWSQLStore2Queries::executeMainQuery (SMW)'); $result = $this->getNMQueryResult($query, $rootid); $this->cleanUp(); $query->addErrors($this->m_errors); return $result; }
/** * The new SQL store's implementation of query answering. */ public function getQueryResult(SMWQuery $query) { if ($query->querymode == SMWQuery::MODE_NONE) { // don't query, but return something to printer $result = new SMWQueryResult($query->getDescription()->getPrintrequests(), $query, true); return $result; } $this->m_qmode = $query->querymode; $this->m_queries = array(); $this->m_hierarchies = array(); $this->m_querylog = array(); $this->m_errors = array(); SMWSQLStore2Query::$qnum = 0; $this->m_sortkeys = $query->sortkeys; // manually make final root query (to retrieve namespace,title): $rootid = SMWSQLStore2Query::$qnum; $qobj = new SMWSQLStore2Query(); $qobj->jointable = 'smw_ids'; $qobj->joinfield = "{$qobj->alias}.smw_id"; // build query dependency tree: wfProfileIn('SMWSQLStore2Queries::compileMainQuery (SMW)'); $qid = $this->compileQueries($query->getDescription()); // compile query, build query "plan" wfProfileOut('SMWSQLStore2Queries::compileMainQuery (SMW)'); if ($qid < 0) { // no valid/supported condition; ensure that at least only proper pages are delivered $qid = SMWSQLStore2Query::$qnum; $q = new SMWSQLStore2Query(); $q->jointable = 'smw_ids'; $q->joinfield = "{$q->alias}.smw_id"; $q->where = "{$q->alias}.smw_iw!=" . $this->m_dbs->addQuotes(SMW_SQL2_SMWIW) . " AND {$q->alias}.smw_iw!=" . $this->m_dbs->addQuotes(SMW_SQL2_SMWREDIIW) . " AND {$q->alias}.smw_iw!=" . $this->m_dbs->addQuotes(SMW_SQL2_SMWBORDERIW) . " AND {$q->alias}.smw_iw!=" . $this->m_dbs->addQuotes(SMW_SQL2_SMWINTDEFIW); $this->m_queries[$qid] = $q; } // append query to root: $qobj->components = array($qid => "{$qobj->alias}.smw_id"); $qobj->sortfields = $this->m_queries[$qid]->sortfields; $this->m_queries[$rootid] = $qobj; $this->applyOrderConditions($query, $rootid); // may extend query if needed for sorting wfProfileIn('SMWSQLStore2Queries::executeMainQuery (SMW)'); $this->executeQueries($this->m_queries[$rootid]); // execute query tree, resolve all dependencies wfProfileOut('SMWSQLStore2Queries::executeMainQuery (SMW)'); $result = $this->getNMQueryResult($query, $rootid); $this->cleanUp(); $query->addErrors($this->m_errors); return $result; }