refactoring some code
[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 """
8619b00b
O
15 if os.path.isfile('rainbow.db'):
16 os.system('rm -rf rainbow.db')
17 init_db()
18cab06a
O
18 self.engine = create_engine('sqlite:///rainbow.db', echo=False)
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
305ce127 30 def rainbow_to_tweet_query(self, rid):
908d5012 31 """
32 Query base of rainbow id
33 """
18cab06a
O
34 Session = sessionmaker(bind=self.engine)
35 session = Session()
305ce127 36 res = session.query(Tweet).filter_by(rainbow_id=rid).all()
18cab06a
O
37 return res
38
305ce127 39 def tweet_to_rainbow_query(self, tid):
908d5012 40 """
41 Query base of tweet id
42 """
18cab06a
O
43 Session = sessionmaker(bind=self.engine)
44 session = Session()
305ce127 45 res = session.query(Tweet).filter_by(tweet_id=tid).all()
46 return res
47
48 def message_store(self, message_id):
49 """
50 Store message id
51 """
52 Session = sessionmaker(bind=self.engine)
53 session = Session()
54 m = Message(message_id)
55 session.add(m)
56 session.commit()
57
58 def rainbow_to_message_query(self, rid):
59 """
60 Query base of rainbow id
61 """
62 Session = sessionmaker(bind=self.engine)
63 session = Session()
64 res = session.query(Message).filter_by(rainbow_id=rid).all()
65 return res
66
67 def message_to_rainbow_query(self, mid):
68 """
69 Query base of message id
70 """
71 Session = sessionmaker(bind=self.engine)
72 session = Session()
73 res = session.query(Message).filter_by(message_id=mid).all()
74 return res
4cf86720
VNM
75
76 def theme_store(self, theme_name):
77 """
78 Store theme
79 """
80 Session = sessionmaker(bind=self.engine)
81 session = Session()
82 th = Theme(theme_name)
83 session.add(th)
84 session.commit()
85
86 def theme_update(self, theme_name):
87 """
88 Update theme
89 """
90 Session = sessionmaker(bind=self.engine)
91 session = Session()
92 res = session.query(Theme).all()
93 for r in res:
94 r.theme_name = theme_name
95 session.commit()
96
97 def theme_query(self):
98 """
99 Query theme
100 """
101 Session = sessionmaker(bind=self.engine)
102 session = Session()
103 res = session.query(Theme).all()
104 return res
9683e61d 105
c37c04a9 106 def semaphore_store(self, lock, pause):
9683e61d 107 """
c37c04a9 108 Store semaphore lock
9683e61d
O
109 """
110 Session = sessionmaker(bind=self.engine)
111 session = Session()
c37c04a9 112 th = Semaphore(lock, pause)
9683e61d
O
113 session.add(th)
114 session.commit()
115
c37c04a9 116 def semaphore_update_lock(self, lock):
9683e61d 117 """
c37c04a9 118 Update semaphore lock
9683e61d
O
119 """
120 Session = sessionmaker(bind=self.engine)
121 session = Session()
122 res = session.query(Semaphore).all()
123 for r in res:
c37c04a9 124 r.lock = lock
9683e61d
O
125 session.commit()
126
d6cc4c67 127 def semaphore_update_pause(self, pause):
9683e61d 128 """
d6cc4c67
O
129 Update semaphore pause
130 """
131 Session = sessionmaker(bind=self.engine)
132 session = Session()
133 res = session.query(Semaphore).all()
134 for r in res:
135 r.pause = pause
136 session.commit()
137
c37c04a9 138 def semaphore_query_lock(self):
d6cc4c67 139 """
c37c04a9 140 Query semaphore lock
9683e61d
O
141 """
142 Session = sessionmaker(bind=self.engine)
143 session = Session()
144 res = session.query(Semaphore).all()
c37c04a9 145 return res[0].lock
d6cc4c67 146
d6cc4c67
O
147 def semaphore_query_pause(self):
148 """
149 Query semaphore pause
150 """
151 Session = sessionmaker(bind=self.engine)
152 session = Session()
153 res = session.query(Semaphore).all()
154 return res[0].pause