fix bug
[rainbowstream.git] / rainbowstream / db.py
CommitLineData
eb9781ed 1import os
18cab06a 2from sqlalchemy import create_engine
4cf86720 3from sqlalchemy import update
18cab06a 4from sqlalchemy.orm import sessionmaker
eb9781ed 5from .table_def import *
18cab06a 6
b2b933a9 7
18cab06a
O
8class RainbowDB():
9
b2b933a9 10 engine = None
18cab06a
O
11
12 def __init__(self):
eb9781ed
O
13 if not os.path.isfile('rainbow.db'):
14 init_db()
18cab06a
O
15 self.engine = create_engine('sqlite:///rainbow.db', echo=False)
16
305ce127 17 def tweet_store(self, tweet_id):
908d5012 18 """
19 Store tweet id
20 """
18cab06a
O
21 Session = sessionmaker(bind=self.engine)
22 session = Session()
4cf86720
VNM
23 t = Tweet(tweet_id)
24 session.add(t)
18cab06a
O
25 session.commit()
26
305ce127 27 def rainbow_to_tweet_query(self, rid):
908d5012 28 """
29 Query base of rainbow id
30 """
18cab06a
O
31 Session = sessionmaker(bind=self.engine)
32 session = Session()
305ce127 33 res = session.query(Tweet).filter_by(rainbow_id=rid).all()
18cab06a
O
34 return res
35
305ce127 36 def tweet_to_rainbow_query(self, tid):
908d5012 37 """
38 Query base of tweet id
39 """
18cab06a
O
40 Session = sessionmaker(bind=self.engine)
41 session = Session()
305ce127 42 res = session.query(Tweet).filter_by(tweet_id=tid).all()
43 return res
44
45 def message_store(self, message_id):
46 """
47 Store message id
48 """
49 Session = sessionmaker(bind=self.engine)
50 session = Session()
51 m = Message(message_id)
52 session.add(m)
53 session.commit()
54
55 def rainbow_to_message_query(self, rid):
56 """
57 Query base of rainbow id
58 """
59 Session = sessionmaker(bind=self.engine)
60 session = Session()
61 res = session.query(Message).filter_by(rainbow_id=rid).all()
62 return res
63
64 def message_to_rainbow_query(self, mid):
65 """
66 Query base of message id
67 """
68 Session = sessionmaker(bind=self.engine)
69 session = Session()
70 res = session.query(Message).filter_by(message_id=mid).all()
71 return res
4cf86720
VNM
72
73 def theme_store(self, theme_name):
74 """
75 Store theme
76 """
77 Session = sessionmaker(bind=self.engine)
78 session = Session()
79 th = Theme(theme_name)
80 session.add(th)
81 session.commit()
82
83 def theme_update(self, theme_name):
84 """
85 Update theme
86 """
87 Session = sessionmaker(bind=self.engine)
88 session = Session()
89 res = session.query(Theme).all()
90 for r in res:
91 r.theme_name = theme_name
92 session.commit()
93
94 def theme_query(self):
95 """
96 Query theme
97 """
98 Session = sessionmaker(bind=self.engine)
99 session = Session()
100 res = session.query(Theme).all()
101 return res