1714005d77a4154d9052144b595e5b978a855640
[rainbowstream.git] / rainbowstream / db.py
1 import os
2 from sqlalchemy import create_engine
3 from sqlalchemy.orm import sessionmaker
4 from .table_def import *
5
6
7 class RainbowDB():
8
9 engine = None
10
11 def __init__(self):
12 """
13 Init DB
14 """
15 if not os.path.isfile('rainbow.db'):
16 init_db()
17 self.engine = create_engine('sqlite:///rainbow.db', echo=False)
18
19
20 def tweet_store(self, tweet_id):
21 """
22 Store tweet id
23 """
24 Session = sessionmaker(bind=self.engine)
25 session = Session()
26 t = Tweet(tweet_id)
27 session.add(t)
28 session.commit()
29
30
31 def rainbow_to_tweet_query(self, rid):
32 """
33 Query base of rainbow id
34 """
35 Session = sessionmaker(bind=self.engine)
36 session = Session()
37 res = session.query(Tweet).filter_by(rainbow_id=rid).all()
38 return res
39
40
41 def tweet_to_rainbow_query(self, tid):
42 """
43 Query base of tweet id
44 """
45 Session = sessionmaker(bind=self.engine)
46 session = Session()
47 res = session.query(Tweet).filter_by(tweet_id=tid).all()
48 return res
49
50
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
61
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
71
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
80
81
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
92
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
104
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
113
114
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
125
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
137
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