refactoring and add various comments
[rainbowstream.git] / rainbowstream / db.py
CommitLineData
eb9781ed 1import os
18cab06a
O
2from sqlalchemy import create_engine
3from sqlalchemy.orm import sessionmaker
eb9781ed 4from .table_def import *
18cab06a 5
b2b933a9 6
18cab06a
O
7class RainbowDB():
8
b2b933a9 9 engine = None
18cab06a
O
10
11 def __init__(self):
531f5682
O
12 """
13 Init DB
14 """
eb9781ed
O
15 if not os.path.isfile('rainbow.db'):
16 init_db()
18cab06a
O
17 self.engine = create_engine('sqlite:///rainbow.db', echo=False)
18
531f5682 19
305ce127 20 def tweet_store(self, tweet_id):
908d5012 21 """
22 Store tweet id
23 """
18cab06a
O
24 Session = sessionmaker(bind=self.engine)
25 session = Session()
4cf86720
VNM
26 t = Tweet(tweet_id)
27 session.add(t)
18cab06a
O
28 session.commit()
29
531f5682 30
305ce127 31 def rainbow_to_tweet_query(self, rid):
908d5012 32 """
33 Query base of rainbow id
34 """
18cab06a
O
35 Session = sessionmaker(bind=self.engine)
36 session = Session()
305ce127 37 res = session.query(Tweet).filter_by(rainbow_id=rid).all()
18cab06a
O
38 return res
39
531f5682 40
305ce127 41 def tweet_to_rainbow_query(self, tid):
908d5012 42 """
43 Query base of tweet id
44 """
18cab06a
O
45 Session = sessionmaker(bind=self.engine)
46 session = Session()
305ce127 47 res = session.query(Tweet).filter_by(tweet_id=tid).all()
48 return res
49
531f5682 50
305ce127 51 def message_store(self, message_id):
52 """
53 Store message id
54 """
55 Session = sessionmaker(bind=self.engine)
56 session = Session()
57 m = Message(message_id)
58 session.add(m)
59 session.commit()
60
531f5682 61
305ce127 62 def rainbow_to_message_query(self, rid):
63 """
64 Query base of rainbow id
65 """
66 Session = sessionmaker(bind=self.engine)
67 session = Session()
68 res = session.query(Message).filter_by(rainbow_id=rid).all()
69 return res
70
531f5682 71
305ce127 72 def message_to_rainbow_query(self, mid):
73 """
74 Query base of message id
75 """
76 Session = sessionmaker(bind=self.engine)
77 session = Session()
78 res = session.query(Message).filter_by(message_id=mid).all()
79 return res
4cf86720 80
531f5682 81
4cf86720
VNM
82 def theme_store(self, theme_name):
83 """
84 Store theme
85 """
86 Session = sessionmaker(bind=self.engine)
87 session = Session()
88 th = Theme(theme_name)
89 session.add(th)
90 session.commit()
91
531f5682 92
4cf86720
VNM
93 def theme_update(self, theme_name):
94 """
95 Update theme
96 """
97 Session = sessionmaker(bind=self.engine)
98 session = Session()
99 res = session.query(Theme).all()
100 for r in res:
101 r.theme_name = theme_name
102 session.commit()
103
531f5682 104
4cf86720
VNM
105 def theme_query(self):
106 """
107 Query theme
108 """
109 Session = sessionmaker(bind=self.engine)
110 session = Session()
111 res = session.query(Theme).all()
112 return res
9683e61d 113
531f5682 114
9683e61d
O
115 def semaphore_store(self, flag):
116 """
117 Store semaphore flag
118 """
119 Session = sessionmaker(bind=self.engine)
120 session = Session()
121 th = Semaphore(flag)
122 session.add(th)
123 session.commit()
124
531f5682 125
9683e61d
O
126 def semaphore_update(self, flag):
127 """
128 Update semaphore flag
129 """
130 Session = sessionmaker(bind=self.engine)
131 session = Session()
132 res = session.query(Semaphore).all()
133 for r in res:
134 r.flag = flag
135 session.commit()
136
531f5682 137
9683e61d
O
138 def semaphore_query(self):
139 """
140 Query semaphore
141 """
142 Session = sessionmaker(bind=self.engine)
143 session = Session()
144 res = session.query(Semaphore).all()
145 return res[0].flag