[] and id=
[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
531f5682 20
305ce127 21 def tweet_store(self, tweet_id):
908d5012 22 """
23 Store tweet id
24 """
18cab06a
O
25 Session = sessionmaker(bind=self.engine)
26 session = Session()
4cf86720
VNM
27 t = Tweet(tweet_id)
28 session.add(t)
18cab06a
O
29 session.commit()
30
531f5682 31
305ce127 32 def rainbow_to_tweet_query(self, rid):
908d5012 33 """
34 Query base of rainbow id
35 """
18cab06a
O
36 Session = sessionmaker(bind=self.engine)
37 session = Session()
305ce127 38 res = session.query(Tweet).filter_by(rainbow_id=rid).all()
18cab06a
O
39 return res
40
531f5682 41
305ce127 42 def tweet_to_rainbow_query(self, tid):
908d5012 43 """
44 Query base of tweet id
45 """
18cab06a
O
46 Session = sessionmaker(bind=self.engine)
47 session = Session()
305ce127 48 res = session.query(Tweet).filter_by(tweet_id=tid).all()
49 return res
50
531f5682 51
305ce127 52 def message_store(self, message_id):
53 """
54 Store message id
55 """
56 Session = sessionmaker(bind=self.engine)
57 session = Session()
58 m = Message(message_id)
59 session.add(m)
60 session.commit()
61
531f5682 62
305ce127 63 def rainbow_to_message_query(self, rid):
64 """
65 Query base of rainbow id
66 """
67 Session = sessionmaker(bind=self.engine)
68 session = Session()
69 res = session.query(Message).filter_by(rainbow_id=rid).all()
70 return res
71
531f5682 72
305ce127 73 def message_to_rainbow_query(self, mid):
74 """
75 Query base of message id
76 """
77 Session = sessionmaker(bind=self.engine)
78 session = Session()
79 res = session.query(Message).filter_by(message_id=mid).all()
80 return res
4cf86720 81
531f5682 82
4cf86720
VNM
83 def theme_store(self, theme_name):
84 """
85 Store theme
86 """
87 Session = sessionmaker(bind=self.engine)
88 session = Session()
89 th = Theme(theme_name)
90 session.add(th)
91 session.commit()
92
531f5682 93
4cf86720
VNM
94 def theme_update(self, theme_name):
95 """
96 Update theme
97 """
98 Session = sessionmaker(bind=self.engine)
99 session = Session()
100 res = session.query(Theme).all()
101 for r in res:
102 r.theme_name = theme_name
103 session.commit()
104
531f5682 105
4cf86720
VNM
106 def theme_query(self):
107 """
108 Query theme
109 """
110 Session = sessionmaker(bind=self.engine)
111 session = Session()
112 res = session.query(Theme).all()
113 return res
9683e61d 114
531f5682 115
9683e61d
O
116 def semaphore_store(self, flag):
117 """
118 Store semaphore flag
119 """
120 Session = sessionmaker(bind=self.engine)
121 session = Session()
122 th = Semaphore(flag)
123 session.add(th)
124 session.commit()
125
531f5682 126
9683e61d
O
127 def semaphore_update(self, flag):
128 """
129 Update semaphore flag
130 """
131 Session = sessionmaker(bind=self.engine)
132 session = Session()
133 res = session.query(Semaphore).all()
134 for r in res:
135 r.flag = flag
136 session.commit()
137
531f5682 138
9683e61d
O
139 def semaphore_query(self):
140 """
141 Query semaphore
142 """
143 Session = sessionmaker(bind=self.engine)
144 session = Session()
145 res = session.query(Semaphore).all()
146 return res[0].flag