/** * @since 2.2 * * @param Store $store * @param DescriptionInterpreterFactory $descriptionInterpreterFactory */ public function __construct(Store $store, DescriptionInterpreterFactory $descriptionInterpreterFactory) { $this->store = $store; $this->dispatchingDescriptionInterpreter = $descriptionInterpreterFactory->newDispatchingDescriptionInterpreter($this); $this->circularReferenceGuard = new CircularReferenceGuard('sql-query'); $this->circularReferenceGuard->setMaxRecursionDepth(2); QuerySegment::$qnum = 0; }
/** * @since 2.2 * * @param Store $store */ public function __construct(Store $store) { $this->store = $store; QuerySegment::$qnum = 0; $this->dispatchingInterpreter = new DispatchingInterpreter(); $this->dispatchingInterpreter->addDefaultInterpreter(new ThingDescriptionInterpreter($this)); $this->dispatchingInterpreter->addInterpreter(new SomePropertyInterpreter($this)); $this->dispatchingInterpreter->addInterpreter(new DisjunctionConjunctionInterpreter($this)); $this->dispatchingInterpreter->addInterpreter(new NamespaceDescriptionInterpreter($this)); $this->dispatchingInterpreter->addInterpreter(new ClassDescriptionInterpreter($this)); $this->dispatchingInterpreter->addInterpreter(new ValueDescriptionInterpreter($this)); $this->dispatchingInterpreter->addInterpreter(new ConceptDescriptionInterpreter($this)); $this->circularReferenceGuard = new CircularReferenceGuard('sql-query'); $this->circularReferenceGuard->setMaxRecursionDepth(2); }
/** * @param string $conceptDescriptionText * * @return QuerySegment|null */ public function prepareQuerySegmentFor($conceptDescriptionText) { QuerySegment::$qnum = 0; $querySegmentListBuilder = $this->queryEngine->getQuerySegmentListBuilder(); $querySegmentListBuilder->setSortKeys(array()); $qp = new QueryParser($this->conceptFeatures); $querySegmentListBuilder->getQuerySegmentFrom($qp->getQueryDescription($conceptDescriptionText)); $qid = $querySegmentListBuilder->getLastQuerySegmentId(); $querySegmentList = $querySegmentListBuilder->getQuerySegmentList(); if ($qid < 0) { return null; } // execute query tree, resolve all dependencies $querySegmentListProcessor = $this->queryEngine->getQuerySegmentListProcessor(); $querySegmentListProcessor->setQueryMode(Query::MODE_INSTANCES); $querySegmentListProcessor->setQuerySegmentList($querySegmentList); $querySegmentListProcessor->doResolveQueryDependenciesById($qid); return $querySegmentList[$qid]; }
/** * @param string $conceptDescriptionText * * @return QuerySegment|null */ public function prepareQuerySegmentFor($conceptDescriptionText) { $querySegements = array(); QuerySegment::$qnum = 0; $queryBuilder = $this->queryEngine->getQueryBuilder(); $queryBuilder->setSortKeys(array()); $qp = new QueryParser($this->conceptFeatures); $queryBuilder->buildQuerySegmentFor($qp->getQueryDescription($conceptDescriptionText)); $qid = $queryBuilder->getLastQuerySegmentId(); $querySegements = $queryBuilder->getQuerySegments(); if ($qid < 0) { return null; } // execute query tree, resolve all dependencies $querySegmentListResolver = $this->queryEngine->getQuerySegmentListResolver(); $querySegmentListResolver->setQueryMode(Query::MODE_INSTANCES); $querySegmentListResolver->setQuerySegmentList($querySegements); $querySegmentListResolver->resolveForSegmentId($qid); return $querySegements[$qid]; }
/** * 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->querySegments = array(); $this->errors = array(); QuerySegment::$qnum = 0; $this->sortKeys = $query->sortkeys; // *** First compute abstract representation of the query (compilation) ***// $this->queryBuilder->setSortKeys($this->sortKeys); $this->queryBuilder->buildQuerySegmentFor($query->getDescription()); // compile query, build query "plan" $qid = $this->queryBuilder->getLastQuerySegmentId(); $this->querySegments = $this->queryBuilder->getQuerySegments(); $this->errors = $this->queryBuilder->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::tableName; $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->querySegments[$qid] = $q; } if ($this->querySegments[$qid]->joinTable != SMWSql3SmwIds::tableName) { // manually make final root query (to retrieve namespace,title): $rootid = QuerySegment::$qnum; $qobj = new QuerySegment(); $qobj->joinTable = SMWSql3SmwIds::tableName; $qobj->joinfield = "{$qobj->alias}.smw_id"; $qobj->components = array($qid => "{$qobj->alias}.smw_id"); $qobj->sortfields = $this->querySegments[$qid]->sortfields; $this->querySegments[$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->querySegmentListResolver->setQueryMode($this->queryMode); $this->querySegmentListResolver->setQuerySegmentList($this->querySegments); // execute query tree, resolve all dependencies $this->querySegmentListResolver->resolveForSegmentId($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->querySegmentListResolver->cleanUp(); $query->addErrors($this->errors); return $result; }