-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmetamodel.py
226 lines (179 loc) · 5.86 KB
/
metamodel.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.types import Integer
from sqlalchemy.sql.type_api import TypeEngine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.orm.attributes import QueryableAttribute
from flask import request
import sys
class SessionProxy(object):
def __getattr__(self, name):
return getattr(request._session, name)
def __enter__(self):
if not hasattr(request, '_inTransact'):
request._inTransact = 0
request._inTransact += 1
def __exit__(self, type, value, traceback):
request._inTransact -= 1
if request._inTransact > 0:
if type != None:
raise
return
if type == None:
try:
request._session.commit()
except:
request._session.rollback()
raise
else:
request._session.rollback()
raise
def createLocalSession():
request._session = scoped_session(sessionmaker())
request._session.configure(bind=engine)
def closeLocalSession(didExcept):
request._session.remove()
if didExcept and request._session.is_active:
request._session.rollback()
transact = SessionProxy()
metadata = sa.MetaData()
# Monkey patched into Model-decorated classes
@classmethod
def relation(cls, *args, **kwargs):
return orm.relation(cls, *args, **kwargs)
@classmethod
def create(cls, **kwargs):
with transact:
obj = cls()
for k, v in kwargs.items():
setattr(obj, k, v)
transact.add(obj)
return obj
def update(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
return self
@classmethod
def all(cls):
retry = 10
while retry:
try:
with transact:
return transact.query(cls).all()
except:
retry -= 1
def genFilter(cls, kwargs):
if len(kwargs) == 1:
k, v = kwargs.items()[0]
return getattr(cls, k) == v
filters = []
for k, v in kwargs.items():
filters.append(getattr(cls, k) == v)
return sa.and_(*filters)
@classmethod
def some(cls, **kwargs):
retry = 10
while retry:
try:
with transact:
filter = genFilter(cls, kwargs)
return transact.query(cls).filter(filter).all()
except:
retry -= 1
@classmethod
def one(cls, **kwargs):
retry = 10
while retry:
try:
with transact:
filter = genFilter(cls, kwargs)
return transact.query(cls).filter(filter).one()
except:
retry -= 1
def Model(func):
cframe = [None]
def trace(frame, event, arg):
if cframe[0] is None:
cframe[0] = frame
sys.settrace(trace)
func()
sys.settrace(None)
frame = cframe[0]
names = list(func.func_code.co_varnames)
elems = {name : frame.f_locals[name] for name in names}
cls = type(func.__name__, (object, ), elems)
cls._fields = names
cls.create = create
cls.update = update
cls.all = all
cls.some = some
cls.one = one
cls.relation = relation
Model.classes.append(cls)
return cls
Model.classes = []
engine = None
def setup(db):
global engine
engine = sa.create_engine(db, client_encoding='utf8')
metadata.bind = engine
initialized = False
for model in Model.classes:
name = model.__name__
params = []
for field in dir(model):
value = getattr(model, field)
if isinstance(value, PrimaryKey):
params = [field] + params
else:
params.append(field)
columns = []
columns.append(sa.Column('id', Integer, primary_key=True))
relations = {}
for field in params:
value = getattr(model, field)
if isinstance(value, Modifier):
columns.append(value.build(field))
delattr(model, field)
elif (isinstance(value, type) or isinstance(value, TypeEngine)) and field != '__class__':
columns.append(sa.Column(field, value))
delattr(model, field)
elif isinstance(value, orm.properties.RelationshipProperty):
relations[field] = value
delattr(model, field)
elif field in model._fields:
model._fields.remove(field)
table = sa.Table(name, metadata, *columns)
orm.mapper(model, table, properties=relations)
if table.exists():
initialized = True
metadata.create_all()
def sub(func):
if not initialized:
func()
return sub
class Modifier(object):
pass
class PrimaryKey(Modifier):
def __init__(self, type):
self.type = type
def build(self, name):
return sa.Column(name, self.type, primary_key=True)
class ForeignKey(Modifier):
def __init__(self, type, ref, *args, **kwargs):
self.type, self.ref = type, ref
self.args, self.kwargs = args, kwargs
def build(self, name):
return sa.Column(name, self.type, sa.ForeignKey(self.ref), *self.args, **self.kwargs)
class Nullable(Modifier):
def __init__(self, type, *args, **kwargs):
self.type = type
self.args, self.kwargs = args, kwargs
def build(self, name):
return sa.Column(name, self.type, nullable=True, *self.args, **self.kwargs)
class Unique(Modifier):
def __init__(self, type, *args, **kwargs):
self.type = type
self.args, self.kwargs = args, kwargs
def build(self, name):
return sa.Column(name, self.type, unique=True, *self.args, **self.kwargs)