| 1 | import os |
| 2 | from sqlalchemy import create_engine |
| 3 | from sqlalchemy.orm import sessionmaker |
| 4 | from table_def import Map |
| 5 | from .table_def import * |
| 6 | |
| 7 | class RainbowDB(): |
| 8 | |
| 9 | engine=None |
| 10 | |
| 11 | def __init__(self): |
| 12 | if not os.path.isfile('rainbow.db'): |
| 13 | init_db() |
| 14 | self.engine = create_engine('sqlite:///rainbow.db', echo=False) |
| 15 | |
| 16 | def store(self, tweet_id): |
| 17 | Session = sessionmaker(bind=self.engine) |
| 18 | session = Session() |
| 19 | m = Map(tweet_id) |
| 20 | session.add(m) |
| 21 | session.commit() |
| 22 | |
| 23 | def rainbow_query(self, rid): |
| 24 | Session = sessionmaker(bind=self.engine) |
| 25 | session = Session() |
| 26 | res = session.query(Map).filter("rainbow_id =:rid").params(rid=rid).all() |
| 27 | return res |
| 28 | |
| 29 | def tweet_query(self, tid): |
| 30 | Session = sessionmaker(bind=self.engine) |
| 31 | session = Session() |
| 32 | res = session.query(Map).filter("tweet_id =:tid").params(tid=tid).all() |
| 33 | return res |