1. March Liu
  2. Socrates

Commits

March Liu  committed eacc05b

add commit and rollback methods to Subject Type
add delete subject by id method to Storage

  • Participants
  • Parent commits 6d27926
  • Branches default

Comments (0)

Files changed (2)

File src/socrates/core.py

View file
         pid = self.get_predicate_id_by_name(pname)
         SegmentType = self.registed_type[tnames[0]]
         segment = SegmentType(sid, pid, obj)
+        
         self.Session.add(segment)
         if autocommit:
             self.Session.commit()
                 return Type(self, segments)
         return Subject(self, segments)
 
+    def deleteSubjectById(self, subject_id):
+        """
+>>> storage = Storage("sqlite://", create=True)
+>>> pid = storage.newPredicate('ssoid', 'string')
+>>> print pid
+10
+>>> ssoid = storage.getSubjectById(pid)
+>>> print ssoid.name
+ssoid
+>>> segment = storage.writeSegment(500, 'ssoid', 'March Liu')
+>>> print segment.predicate.name
+ssoid
+>>> newseg = storage.writeSegment(None, 'ssoid', 'Milly Lee')
+>>> subject = storage.getSubjectById(segment.subject_id)
+>>> print subject.pnames
+set([u'ssoid'])
+>>> storage.deleteSubjectById(segment.subject_id)
+>>> segments = [segment for segment in storage.filterSegmentsByPair('ssoid', 'March Liu')]
+>>> segments
+[]
+>>> segments = [segment for segment in storage.filterSegmentsByPair('ssoid', 'Milly Lee')]
+>>> segments[0].obj
+u'Milly Lee'
+        """
+        for table in self.registed_storage.values():
+            self.Session.execute(table.delete().where(table.c.subject_id==subject_id))
+        self.Session.commit()
+
     def filterSegmentsByPair(self, predicate_segment, obj):
         """
 >>> storage = Storage("sqlite://", create=True)

File src/socrates/types.py

View file
         self.__storage = storage
         self.segments = segments
         self.subject_id=segments[0].subject_id
-    def write(self, predicate, obj):
-        segment = self.__storage.writeSegment(self.subject_id, predicate, obj)
+        self.prdexprpaser = Prdexprpaser()
+    def write(self, prdexpr, obj):
+        segment = self.__storage.writeSegment(self.subject_id, prdexpr, obj)
         self.segments.append(segment)
+    def Session(self, ):
+        return self.__storage.Session
+    def commit(self):
+        return self.Session.commit()
+    def rollback(self):
+        return self.Session.rollback()
+
     def object(self, pname):
         re = self.objects(pname)
         if len(re)==0: