1. March Liu
  2. Socrates

Commits

March Liu  committed 66dd121

use socped session

  • Participants
  • Parent commits ccd0cac
  • Branches default

Comments (0)

Files changed (1)

File src/socrates/core.py

View file
         engine = create_engine(uri, pool_size=pool_size) if uri else create_engine("sqlite://:memory:")
         self.engine = engine
         self.Session = scoped_session(sessionmaker(bind=engine))
-        session = self.Session(autocommit=True)
         metadata = MetaData(bind=engine) 
         self.sequence = Serial(engine, metadata, create=create, schema=schema)
         self.metadata = metadata
 
         subjects = self.registed_storage['subject']
         strings = self.registed_storage['string']
-        session = self.Session()
-        session.begin()
 
         for segmentType in self.registed_type.values():
             if storname == segmentType.table.name:
                 self.registed_storage[typename] = segmentType.table
                 self.registed_type[typename] = segmentType
-                session.execute(subjects.insert().values(predicate_id=meta.IS, obj=meta.TYPE),)
-                session.execute(strings.insert().values(predicate_id=meta.STORAGE, obj=table.name),)
+                self.Session.execute(subjects.insert().values(predicate_id=meta.IS, obj=meta.TYPE),)
+                self.Session.execute(strings.insert().values(predicate_id=meta.STORAGE, obj=table.name),)
                 return segmentType
 
         if create:
                           schema=schname)
             table.create()
             typeid = self.sequence()
-            session.execute(subjects.insert().values(subject_id=typeid, predicate_id=meta.IS, obj=meta.TYPE),)
-            session.execute(strings.insert().values(subject_id=typeid, predicate_id=meta.NAME, obj=typename),)
-            session.execute(strings.insert().values(subject_id=typeid, predicate_id=meta.STORAGE, obj=table.name),)
+            self.Session.execute(subjects.insert().values(subject_id=typeid, predicate_id=meta.IS, obj=meta.TYPE),)
+            self.Session.execute(strings.insert().values(subject_id=typeid, predicate_id=meta.NAME, obj=typename),)
+            self.Session.execute(strings.insert().values(subject_id=typeid, predicate_id=meta.STORAGE, obj=table.name),)
         else:
             table = Table(storname, self.metadata, 
                           Column('subject_id', Integer, default=self.sequence), 
         self.registed_storage[typename] = table
         segmentType = self.__make_type_helper(typename, table)
         self.registed_type[typename] = segmentType
-        session.commit()
+        self.Session.commit()
         return segmentType
 
     def newPredicate(self, name, objtype_name):
 >>> print when.objType.name
 datetime
         """
-        session = self.Session()
-        session.begin()
         Subject = self.registed_type["subject"]
         subjects = Subject.table
         String = self.registed_type["string"]
         strings = String.table
-        pid = session.query(Subject.subject_id).\
+        pid = self.Session.query(Subject.subject_id).\
             filter(Subject.predicate_id==meta.IS).\
             filter(Subject.obj==meta.PREDICATE).\
             filter(select([String.obj], and_(String.subject_id==Subject.subject_id,
         if objtype_name == 'subject':
             objType = meta.SUBJECT
         else:
-            objType = session.query(strings.c.subject_id).\
+            objType = self.Session.query(strings.c.subject_id).\
                 filter(strings.c.predicate_id==meta.NAME).\
                 filter(strings.c.obj==objtype_name).\
                 filter(select([subjects.c.obj], 
                               and_(subjects.c.subject_id==strings.c.subject_id,
                                    subjects.c.predicate_id==meta.IS,)).as_scalar()==meta.TYPE).scalar()
-        storid = session.query(subjects.c.obj).\
+        storid = self.Session.query(subjects.c.obj).\
             filter(subjects.c.subject_id==objType).\
             filter(subjects.c.predicate_id==meta.STORAGE).scalar()
-        session.execute(subjects.insert().values(subject_id=pid, predicate_id=meta.IS, obj=meta.PREDICATE))
-        session.execute(subjects.insert().values(subject_id=pid, predicate_id=meta.OBJTYPE, obj=objType))
-        session.execute(strings.insert().values(subject_id=pid, predicate_id=meta.NAME, obj=name))
-        session.commit()
+        self.Session.execute(subjects.insert().values(subject_id=pid, predicate_id=meta.IS, obj=meta.PREDICATE))
+        self.Session.execute(subjects.insert().values(subject_id=pid, predicate_id=meta.OBJTYPE, obj=objType))
+        self.Session.execute(strings.insert().values(subject_id=pid, predicate_id=meta.NAME, obj=name))
+        self.Session.commit()
         return pid
 
     def writeSubject(self, subject_id, segments, **pairs):
             sid = subject_id
         else:
             sid = self.sequence()
-        session = self.Session()
-        session.begin()
         for k,v in segments.iteritems():
-            self.writeSegment(sid, k, v, session)
+            self.writeSegment(sid, k, v, False)
         for k,v in pairs.iteritems():
-            self.writeSegment(sid, k, v, session)
-        session.commit()
+            self.writeSegment(sid, k, v, False)
+        self.Session.commit()
         return sid
 
-    def writeSegment(self, subject_id, predicate, obj, session=None):
+    def writeSegment(self, subject_id, predicate, obj, autocommit=True):
         """
         此版本未解决同名谓词问题。仅适用于单一谓词应用,即每个谓词名仅对应一条谓词定义。
 >>> from sqlalchemy import DateTime
 12
         """
         needclose = True
-        if session:
-            _session = session
-            needclose = False
-        else:
-            _session=self.Session()
-        if needclose:
-            _session.begin()
         subjects = self.registed_storage["subject"]
         strings = self.registed_storage["string"]
         if subject_id:
         else:
             sid = self.sequence()
         pid = self.get_preidcate_id_by_name(predicate)
-        objType = _session.query(subjects.c.obj).\
+        objType = self.Session.query(subjects.c.obj).\
             filter(subjects.c.subject_id==pid).\
             filter(subjects.c.predicate_id==meta.OBJTYPE).scalar() 
-        typename = _session.query(strings.c.obj).\
+        typename = self.Session.query(strings.c.obj).\
             filter(strings.c.subject_id==objType).\
             filter(strings.c.predicate_id==meta.NAME).scalar()
         SegmentType = self.registed_type[typename]
         segment = SegmentType(sid, pid, obj)
-        _session.add(segment)
-        if needclose:
-            _session.commit()
+        self.Session.add(segment)
+        if autocommit:
+            self.Session.commit()
         return segment 
 
     def initStorage(self, schema=None):
         meta.init_meta(self.engine, self.registed_storage["subject"], 
                        self.registed_storage["string"], self.sequence, clearfirst)
 
-    def getSegmentsBySubjectId(self, subject_id, session=None):
-        if session:
-            _session=session
-        else:
-            _session = self.Session()
+    def getSegmentsBySubjectId(self, subject_id, autocommit=False):
         for Type in self.registed_type.values():
-            for segment in _session.query(Type).filter(Type.subject_id==subject_id):
+            for segment in self.Session.query(Type).filter(Type.subject_id==subject_id):
                 yield segment
-        if session == None:
-            _session.commit()
+        if autocommit:
+            self.Session.commit()
         #session.close()
 
     def getSubjectById(self, subject_id):
 >>> print subject.pnames
 set([u'ssoid'])
         """
-        session = self.Session()
-        segments = [segment for segment in self.getSegmentsBySubjectId(subject_id, session)]
+        segments = [segment for segment in self.getSegmentsBySubjectId(subject_id)]
         subject_is = [segment 
                       for segment in segments 
                       if segment.predicate_id==meta.IS and segment.obj in (meta.PREDICATE, meta.TYPE)]
         """
         SegmentString = self.registed_type['string']
         SegmentSubject = self.registed_type['subject']
-        session=self.Session()
-        re = session.query(SegmentSubject.subject_id,).\
+        re = self.Session.query(SegmentSubject.subject_id,).\
             filter(SegmentSubject.predicate_id==meta.IS).\
             filter(SegmentSubject.obj==meta.PREDICATE).\
             filter(select([SegmentString.obj],