X-Git-Url: https://vcs.fsf.org/?p=rainbowstream.git;a=blobdiff_plain;f=rainbowstream%2Fdb.py;h=775c8563d51dd6d81c79d9ae42b2d1417a22008b;hp=e0c7e84b2d2ffe7f160ef8f45c54795e1cace8d8;hb=a5301bc04171f5a7cb1a928ced38e912628cfa85;hpb=908d5012ca72d4a47d88e10cea211e94af03e1d6 diff --git a/rainbowstream/db.py b/rainbowstream/db.py index e0c7e84..775c856 100644 --- a/rainbowstream/db.py +++ b/rainbowstream/db.py @@ -1,51 +1,101 @@ import os from sqlalchemy import create_engine +from sqlalchemy import update from sqlalchemy.orm import sessionmaker -from table_def import Map from .table_def import * + 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 store(self, tweet_id): + def tweet_store(self, tweet_id): """ Store tweet id """ Session = sessionmaker(bind=self.engine) session = Session() - m = Map(tweet_id) - session.add(m) + t = Tweet(tweet_id) + session.add(t) session.commit() - def rainbow_query(self, rid): + def rainbow_to_tweet_query(self, rid): """ Query base of rainbow id """ Session = sessionmaker(bind=self.engine) session = Session() - res = session.query(Map).filter("rainbow_id =:rid").params(rid=rid).all() + res = session.query(Tweet).filter_by(rainbow_id=rid).all() return res - def tweet_query(self, tid): + def tweet_to_rainbow_query(self, tid): """ Query base of tweet id """ Session = sessionmaker(bind=self.engine) session = Session() - res = session.query(Map).filter("tweet_id =:tid").params(tid=tid).all() + 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() + m = Message(message_id) + session.add(m) + session.commit() + + def rainbow_to_message_query(self, rid): + """ + Query base of rainbow id + """ + Session = sessionmaker(bind=self.engine) + session = Session() + res = session.query(Message).filter_by(rainbow_id=rid).all() + return res + + 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 truncate(self): + def theme_store(self, theme_name): """ - Truncate table + Store theme """ Session = sessionmaker(bind=self.engine) session = Session() - session.query(Map).delete() + 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() + res = session.query(Theme).all() + return res