)) if context is not None: clauseString,params = self.buildClause( 'quoted',subject,predicate, obj,context) parameters.extend(params) selects.append( ( quoted_table, 'quoted', clauseString, QUOTED_PARTITION ) ) q = self._normalizeSQLCmd(unionSELECT( selects,selectType=TRIPLE_SELECT_NO_ORDER)) self.executeSQL(c,q,parameters) # NOTE: SQLite does not support ORDER BY terms that aren't # integers, so the entire result set must be iterated # in order to be able to return a generator of contexts tripleCoverage = {} result = c.fetchall() c.close() for rt in result: # Fix by Alcides Fonseca # https://github.com/slok/rdflib/commit/e05827b080772e785290b270da63dce64addfc7c#diff-0 tmp = [] for i,r in enumerate(rt): if r == u"NULL": tmp.append(None) else:
def triples(self, pattern, context=None): """ A generator over all the triples matching pattern. Pattern can be any objects for comparing against nodes in the store, for example, RegExLiteral, Date? DateRange? quoted table: <id>_quoted_statements asserted rdf:type table: <id>_type_statements asserted non rdf:type table: <id>_asserted_statements triple columns: subject, predicate, object, context, termComb, objLanguage, objDatatype class membership columns: member, klass, context, termComb FIXME: These union all selects *may* be further optimized by joins """ (subject, predicate, obj) = pattern quoted_table = "%s_quoted_statements" % self._internedId asserted_table = "%s_asserted_statements" % self._internedId asserted_type_table = "%s_type_statements" % self._internedId literal_table = "%s_literal_statements" % self._internedId c = self._db.cursor() parameters = [] if predicate == RDF.type: # Select from asserted rdf:type partition and quoted table # (if a context is specified) clauseString, params = self.buildClause( 'typeTable', subject, RDF.type, obj, context, True) parameters.extend(params) selects = [ ( asserted_type_table, 'typeTable', clauseString, ASSERTED_TYPE_PARTITION ), ] elif isinstance(predicate, REGEXTerm) \ and predicate.compiledExpr.match(RDF.type) \ or not predicate: # Select from quoted partition (if context is specified), # literal partition if (obj is Literal or None) and asserted # non rdf:type partition (if obj is URIRef or None) selects = [] if not self.STRONGLY_TYPED_TERMS \ or isinstance(obj, Literal) \ or not obj \ or (self.STRONGLY_TYPED_TERMS and isinstance(obj, REGEXTerm)): clauseString, params = self.buildClause( 'literal', subject, predicate, obj, context) parameters.extend(params) selects.append(( literal_table, 'literal', clauseString, ASSERTED_LITERAL_PARTITION )) if not isinstance(obj, Literal) \ and not (isinstance(obj, REGEXTerm) \ and self.STRONGLY_TYPED_TERMS) \ or not obj: clauseString, params = self.buildClause( 'asserted', subject, predicate, obj, context) parameters.extend(params) selects.append(( asserted_table, 'asserted', clauseString, ASSERTED_NON_TYPE_PARTITION )) clauseString, params = self.buildClause( 'typeTable', subject, RDF.type, obj, context, True) parameters.extend(params) selects.append( ( asserted_type_table, 'typeTable', clauseString, ASSERTED_TYPE_PARTITION ) ) elif predicate: # Select from asserted non rdf:type partition (optionally), # quoted partition (if context is speciied), and literal # partition (optionally) selects = [] if not self.STRONGLY_TYPED_TERMS \ or isinstance(obj, Literal) \ or not obj \ or (self.STRONGLY_TYPED_TERMS and isinstance(obj, REGEXTerm)): clauseString, params = self.buildClause( 'literal', subject, predicate, obj, context) parameters.extend(params) selects.append(( literal_table, 'literal', clauseString, ASSERTED_LITERAL_PARTITION )) if not isinstance(obj, Literal) \ and not (isinstance(obj, REGEXTerm) \ and self.STRONGLY_TYPED_TERMS) \ or not obj: clauseString, params = self.buildClause( 'asserted', subject, predicate, obj, context) parameters.extend(params) selects.append(( asserted_table, 'asserted', clauseString, ASSERTED_NON_TYPE_PARTITION )) if context is not None: clauseString, params = self.buildClause( 'quoted', subject, predicate, obj, context) parameters.extend(params) selects.append( ( quoted_table, 'quoted', clauseString, QUOTED_PARTITION ) ) q = self._normalizeSQLCmd( unionSELECT(selects, selectType=TRIPLE_SELECT_NO_ORDER)) self.executeSQL(c, q, parameters) # NOTE: SQLite does not support ORDER BY terms that aren't integers, # so the entire result set must be iterated in order to be able to # return a generator of contexts tripleCoverage = {} result = c.fetchall() c.close() for rt in result: s, p, o, (graphKlass, idKlass, graphId) = \ extractTriple(rt, self, context) contexts = tripleCoverage.get((s, p, o), []) contexts.append(graphKlass(self, idKlass(graphId))) tripleCoverage[(s, p, o)] = contexts for (s, p, o), contexts in list(tripleCoverage.items()): yield (s, p, o), (c for c in contexts)
def triples(self, pattern, context=None): """ A generator over all the triples matching pattern. Pattern can be any objects for comparing against nodes in the store, for example, RegExLiteral, Date? DateRange? quoted table: <id>_quoted_statements asserted rdf:type table: <id>_type_statements asserted non rdf:type table: <id>_asserted_statements triple columns: subject, predicate, object, context, termComb, objLanguage, objDatatype class membership columns: member, klass, context, termComb FIXME: These union all selects *may* be further optimized by joins """ (subject, predicate, obj) = pattern quoted_table = "%s_quoted_statements" % self._internedId asserted_table = "%s_asserted_statements" % self._internedId asserted_type_table = "%s_type_statements" % self._internedId literal_table = "%s_literal_statements" % self._internedId c = self._db.cursor() parameters = [] if predicate == RDF.type: # Select from asserted rdf:type partition and quoted table # (if a context is specified) clauseString, params = self.buildClause('typeTable', subject, RDF.type, obj, context, True) parameters.extend(params) selects = [ (asserted_type_table, 'typeTable', clauseString, ASSERTED_TYPE_PARTITION), ] elif isinstance(predicate, REGEXTerm) \ and predicate.compiledExpr.match(RDF.type) \ or not predicate: # Select from quoted partition (if context is specified), # literal partition if (obj is Literal or None) and asserted # non rdf:type partition (if obj is URIRef or None) selects = [] if not self.STRONGLY_TYPED_TERMS \ or isinstance(obj, Literal) \ or not obj \ or (self.STRONGLY_TYPED_TERMS and isinstance(obj, REGEXTerm)): clauseString, params = self.buildClause( 'literal', subject, predicate, obj, context) parameters.extend(params) selects.append((literal_table, 'literal', clauseString, ASSERTED_LITERAL_PARTITION)) if not isinstance(obj, Literal) \ and not (isinstance(obj, REGEXTerm) \ and self.STRONGLY_TYPED_TERMS) \ or not obj: clauseString, params = self.buildClause( 'asserted', subject, predicate, obj, context) parameters.extend(params) selects.append((asserted_table, 'asserted', clauseString, ASSERTED_NON_TYPE_PARTITION)) clauseString, params = self.buildClause('typeTable', subject, RDF.type, obj, context, True) parameters.extend(params) selects.append((asserted_type_table, 'typeTable', clauseString, ASSERTED_TYPE_PARTITION)) elif predicate: # Select from asserted non rdf:type partition (optionally), # quoted partition (if context is speciied), and literal # partition (optionally) selects = [] if not self.STRONGLY_TYPED_TERMS \ or isinstance(obj, Literal) \ or not obj \ or (self.STRONGLY_TYPED_TERMS and isinstance(obj, REGEXTerm)): clauseString, params = self.buildClause( 'literal', subject, predicate, obj, context) parameters.extend(params) selects.append((literal_table, 'literal', clauseString, ASSERTED_LITERAL_PARTITION)) if not isinstance(obj, Literal) \ and not (isinstance(obj, REGEXTerm) \ and self.STRONGLY_TYPED_TERMS) \ or not obj: clauseString, params = self.buildClause( 'asserted', subject, predicate, obj, context) parameters.extend(params) selects.append((asserted_table, 'asserted', clauseString, ASSERTED_NON_TYPE_PARTITION)) if context is not None: clauseString, params = self.buildClause('quoted', subject, predicate, obj, context) parameters.extend(params) selects.append( (quoted_table, 'quoted', clauseString, QUOTED_PARTITION)) q = self._normalizeSQLCmd( unionSELECT(selects, selectType=TRIPLE_SELECT_NO_ORDER)) self.executeSQL(c, q, parameters) # NOTE: SQLite does not support ORDER BY terms that aren't integers, # so the entire result set must be iterated in order to be able to # return a generator of contexts tripleCoverage = {} result = c.fetchall() c.close() for rt in result: s, p, o, (graphKlass, idKlass, graphId) = \ extractTriple(rt, self, context) contexts = tripleCoverage.get((s, p, o), []) contexts.append(graphKlass(self, idKlass(graphId))) tripleCoverage[(s, p, o)] = contexts for (s, p, o), contexts in list(tripleCoverage.items()): yield (s, p, o), (c for c in contexts)
if context is not None: clauseString, params = self.buildClause( 'quoted', subject, predicate, obj, context) parameters.extend(params) selects.append( ( quoted_table, 'quoted', clauseString, QUOTED_PARTITION ) ) q = self._normalizeSQLCmd( unionSELECT(selects, selectType=TRIPLE_SELECT_NO_ORDER)) self.executeSQL(c, q, parameters) # NOTE: SQLite does not support ORDER BY terms that aren't integers, # so the entire result set must be iterated in order to be able to # return a generator of contexts tripleCoverage = {} result = c.fetchall() c.close() for rt in result: s, p, o, (graphKlass, idKlass, graphId) = \ extractTriple(rt, self, context) contexts = tripleCoverage.get((s, p, o), []) contexts.append(graphKlass(self, idKlass(graphId))) tripleCoverage[(s, p, o)] = contexts for (s, p, o), contexts in tripleCoverage.items():