-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
db_sqlalchemy.py
99 lines (85 loc) · 2.49 KB
/
db_sqlalchemy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
SQLAlchemy Database Adapter
---------------------------
"""
from datetime import datetime
import random
import string
from sqlalchemy import create_engine, Column, Integer, String, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.exc import IntegrityError
import db
Base = declarative_base()
class ExecMessage(Base):
"""
Table of input messages in JSON form.
"""
__tablename__ = "permalinks"
ident = Column(String, primary_key=True, index=True)
code = Column(String)
language = Column(String)
interacts = Column(String)
created = Column(DateTime, default=datetime.utcnow)
last_accessed = Column(
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
requested = Column(Integer, default=0)
def __repr__(self):
return """\
ident: {}
Code:
{}
Interacts:
{}
Language: {}
Created: {}
Last accessed: {}
Requested: {}""".format(
self.ident,
self.code,
self.interacts,
self.language,
self.created,
self.last_accessed,
self.requested)
class DB(db.DB):
"""
SQLAlchemy database adapter
:arg db_file str: the SQLAlchemy URI for a database file
"""
def __init__(self, db_file):
self.engine = create_engine(db_file)
self.SQLSession = sessionmaker(bind=self.engine)
Base.metadata.create_all(self.engine)
self.dbsession = self.SQLSession()
async def add(self, code, language, interacts):
"""
See :meth:`db.DB.add`
"""
while True:
ident = "".join(
random.choice(string.ascii_lowercase) for _ in range(6))
message = ExecMessage(
ident=ident,
code=code,
language=language,
interacts=interacts)
try:
self.dbsession.add(message)
self.dbsession.commit()
except IntegrityError:
# ident was used before
self.dbsession.rollback()
else:
break
return ident
async def get(self, key):
"""
See :meth:`db.DB.get`
"""
msg = self.dbsession.query(ExecMessage).filter_by(ident=key).first()
if msg is None:
raise LookupError
msg.requested = ExecMessage.requested + 1
self.dbsession.commit()
return (msg.code, msg.language, msg.interacts)