def triples(self, clause, 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? .. sourcecode:: text 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 = clause 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)) self.executeSQL(c, q, parameters) rt = c.fetchone() while rt: s, p, o, (graphKlass, idKlass, graphId) = \ extractTriple(rt, self, context) currentContext = graphKlass(self, idKlass(graphId)) contexts = [currentContext] rt = next = c.fetchone() sameTriple = next and \ extractTriple(next, self, context)[:3] == (s, p, o) while sameTriple: s2, p2, o2, (graphKlass, idKlass, graphId) = \ extractTriple(next, self, context) c2 = graphKlass(self, idKlass(graphId)) contexts.append(c2) rt = next = c.fetchone() sameTriple = next and \ extractTriple(next, self, context)[:3] == (s, p, o) yield (s, p, o), (c for c in contexts)
# 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: tmp.append(r) rt = tuple(tmp) 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(): yield (s,p,o),(c for c in contexts) CREATE_ASSERTED_STATEMENTS_TABLE = """ CREATE TABLE %s_asserted_statements ( subject text not NULL, predicate text not NULL, object text not NULL, context text not NULL,
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)
parameters.extend(params) selects.append( ( quoted_table, 'quoted', clauseString, QUOTED_PARTITION ) ) q=self._normalizeSQLCmd(unionSELECT(selects)) self.executeSQL(c,q,parameters) rt = c.fetchone() while rt: s,p,o,(graphKlass,idKlass,graphId) = \ extractTriple(rt,self,context) currentContext=graphKlass(self,idKlass(graphId)) contexts = [currentContext] rt = next = c.fetchone() sameTriple = next and \ extractTriple(next,self,context)[:3] == (s,p,o) while sameTriple: s2,p2,o2,(graphKlass,idKlass,graphId) = \ extractTriple(next,self,context) c2 = graphKlass(self,idKlass(graphId)) contexts.append(c2) rt = next = c.fetchone() sameTriple = next and \ extractTriple(next,self,context)[:3] == (s,p,o) yield (s,p,o),(c for c in contexts)
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)) self.executeSQL(c, q, parameters) rt = c.fetchone() while rt: s, p, o, (graphKlass, idKlass, graphId) = \ extractTriple(rt, self, context) currentContext = graphKlass(self, idKlass(graphId)) contexts = [currentContext] rt = next = c.fetchone() sameTriple = next and \ extractTriple(next, self, context)[:3] == (s, p, o) while sameTriple: s2, p2, o2, (graphKlass, idKlass, graphId) = \ extractTriple(next, self, context) c2 = graphKlass(self, idKlass(graphId)) contexts.append(c2) rt = next = c.fetchone() sameTriple = next and \ extractTriple(next, self, context)[:3] == (s, p, o) yield (s, p, o), (c for c in contexts)
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(): yield (s, p, o), (c for c in contexts) CREATE_ASSERTED_STATEMENTS_TABLE = """ CREATE TABLE %s_asserted_statements ( subject text not NULL, predicate text not NULL, object text not NULL, context text not NULL, termComb tinyint unsigned not NULL)"""