fix sqlalchemy bug
[rainbowstream.git] / rainbowstream / db.py
index 0756b3a5edc9469983ab872e29bd3a2e815b9b1a..5bfb04081c715ad1b8f2690b1527a42987e9222a 100644 (file)
 import os
 from sqlalchemy import create_engine
 from sqlalchemy.orm import sessionmaker
 import os
 from sqlalchemy import create_engine
 from sqlalchemy.orm import sessionmaker
-from table_def import Map
 from .table_def import *
 
 from .table_def import *
 
+
 class RainbowDB():
 
 class RainbowDB():
 
-    engine=None
+    engine = None
 
     def __init__(self):
         if not os.path.isfile('rainbow.db'):
             init_db()
         self.engine = create_engine('sqlite:///rainbow.db', echo=False)
 
 
     def __init__(self):
         if not os.path.isfile('rainbow.db'):
             init_db()
         self.engine = create_engine('sqlite:///rainbow.db', echo=False)
 
-    def store(self, tweet_id):
+    def tweet_store(self, tweet_id):
+        """
+        Store tweet id
+        """
+        Session = sessionmaker(bind=self.engine)
+        session = Session()
+        t = Tweet(tweet_id)
+        session.add(t)
+        session.commit()
+
+    def rainbow_to_tweet_query(self, rid):
+        """
+        Query base of rainbow id
+        """
+        Session = sessionmaker(bind=self.engine)
+        session = Session()
+        res = session.query(Tweet).filter_by(rainbow_id=rid).all()
+        return res
+
+    def tweet_to_rainbow_query(self, tid):
+        """
+        Query base of tweet id
+        """
+        Session = sessionmaker(bind=self.engine)
+        session = Session()
+        res = session.query(Tweet).filter_by(tweet_id=tid).all()
+        return res
+
+    def message_store(self, message_id):
+        """
+        Store message id
+        """
         Session = sessionmaker(bind=self.engine)
         session = Session()
         Session = sessionmaker(bind=self.engine)
         session = Session()
-        m = Map(tweet_id)
+        m = Message(message_id)
         session.add(m)
         session.commit()
 
         session.add(m)
         session.commit()
 
-    def rainbow_query(self, rid):
+    def rainbow_to_message_query(self, rid):
+        """
+        Query base of rainbow id
+        """
         Session = sessionmaker(bind=self.engine)
         session = Session()
         Session = sessionmaker(bind=self.engine)
         session = Session()
-        res = session.query(Map).filter("rainbow_id =:rid").params(rid=rid).all()
+        res = session.query(Message).filter_by(rainbow_id=rid).all()
         return res
 
         return res
 
-    def tweet_query(self, tid):
+    def message_to_rainbow_query(self, mid):
+        """
+        Query base of message id
+        """
+        Session = sessionmaker(bind=self.engine)
+        session = Session()
+        res = session.query(Message).filter_by(message_id=mid).all()
+        return res
+
+    def theme_store(self, theme_name):
+        """
+        Store theme
+        """
+        Session = sessionmaker(bind=self.engine)
+        session = Session()
+        th = Theme(theme_name)
+        session.add(th)
+        session.commit()
+
+    def theme_update(self, theme_name):
+        """
+        Update theme
+        """
+        Session = sessionmaker(bind=self.engine)
+        session = Session()
+        res = session.query(Theme).all()
+        for r in res:
+            r.theme_name = theme_name
+        session.commit()
+
+    def theme_query(self):
+        """
+        Query theme
+        """
         Session = sessionmaker(bind=self.engine)
         session = Session()
         Session = sessionmaker(bind=self.engine)
         session = Session()
-        res = session.query(Map).filter("tweet_id =:tid").params(tid=tid).all()
+        res = session.query(Theme).all()
         return res
         return res