Commits

Marc-Antoine Parent committed 04c86d8

Construct returns a graph. Select needs a 'vars', which requires to wrapping the generator.

  • Participants
  • Parent commits e3ac47b

Comments (0)

Files changed (2)

File virtuoso/tests/test_rdflib3.py

         q = "CONSTRUCT { ?s %(t)s ?o } FROM %(g)s WHERE { ?s %(t)s ?o }" % {
             "t": RDF["type"].n3(), "g": self.graph.identifier.n3()}
         result = self.graph.query(q)
-        assert result.construct is True
-        assert isinstance(result.result, Graph)
-        assert len(result.result) == 3
+        assert isinstance(result, Graph)
+        assert len(result) == 3
         self.graph.remove((None, None, None))
 
     def test_07_float(self):

File virtuoso/vstore.py

     from rdflib import URIRef, BNode, Literal, Variable
 from rdflib.store import Store, VALID_STORE
 
-if __dist__.version.startswith('3'):
-    import vsparql
-
 import pyodbc
 
 __all__ = ['Virtuoso', 'OperationalError', 'resolve']
     def isOpen(self):
         return self.__cursor__ is not None
 
+class EagerIterator(object):
+    """A wrapper for an iterator that calculates one element ahead.
+    Allows to start context handlers within the inner generator."""
+    def __init__(self, g):
+        self.g = g
+        self.done = False
+        try:
+            self.next = g.next()
+        except StopIteration:
+            self.done = True
+    def __iter__(self):
+        return self
+    def next(self):
+        if self.done:
+            raise StopIteration()
+        a = self.next
+        try:
+            next = self.g.next()
+        except StopIteration:
+            self.done = True
+        finally:
+            return a
+
 
 class Virtuoso(Store):
     """
         g = Graph()
         with cursor:
             results = cursor.execute(q.encode("utf-8"))
-            with self.cursor() as resolver:
+            with cursor as resolver:
                 for result in results:
                     g.add(resolve(resolver, x) for x in result)
-        return vsparql.Result(g)
+        return g
 
     def _sparql_ask(self, q, cursor):
         with cursor:
     def _sparql_select(self, q, cursor):
         with cursor:
             results = cursor.execute(q.encode("utf-8"))
-            with self.cursor() as resolver:
-                for result in results:
-                    yield [resolve(resolver, x) for x in result]
+            def f():
+                with cursor:
+                    for r in results:
+                        yield [resolve(cursor, x) for x in r]
+            e = EagerIterator(f())
+            e.vars = [Variable(col[0]) for col in results.description]
+            e.selectionF = e.vars
+            return e
 
     def _sparql_ul(self, q, cursor, commit):
         with cursor: