-
Notifications
You must be signed in to change notification settings - Fork 3
/
camus.py
291 lines (228 loc) · 8.59 KB
/
camus.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from inspect import isclass
from collections import OrderedDict
from contextlib import contextmanager
import boto3
def isexception(obj):
"""Given an object, return a boolean indicating whether it is an instance
or subclass of :py:class:`Exception`.
"""
if isinstance(obj, Exception):
return True
if isclass(obj) and issubclass(obj, Exception):
return True
return False
class RecordCollection(object):
def __init__(self, rows):
self._rows = rows
self._all_rows = []
self.pending = True
def __repr__(self):
return "<RecordCollection size={} pending={}>".format(len(self), self.pending)
def __iter__(self):
i = 0
while True:
if i < len(self):
yield self[i]
else:
try:
yield next(self)
except StopIteration:
return
i += 1
def next(self):
return self.__next__()
def __next__(self):
try:
nextrow = next(self._rows)
self._all_rows.append(nextrow)
return nextrow
except StopIteration:
self.pending = False
raise StopIteration("RecordCollection contains no more rows.")
def __getitem__(self, key):
is_int = isinstance(key, int)
# Convert RecordCollection[1] into slice.
if is_int:
key = slice(key, key + 1)
while len(self) < key.stop or key.stop is None:
try:
next(self)
except StopIteration:
break
rows = self._all_rows[key]
if is_int:
return rows[0]
else:
return RecordCollection(iter(rows))
def __len__(self):
return len(self._all_rows)
def all(self, as_dict=False, as_ordereddict=False):
"""Returns a list of all rows for the RecordCollection. If they haven't
been fetched yet, consume the iterator and cache the results."""
# By calling list it calls the __iter__ method
rows = list(self)
if as_dict:
return [r.as_dict() for r in rows]
elif as_ordereddict:
return [r.as_dict(ordered=True) for r in rows]
return rows
def as_dict(self, ordered=False):
return self.all(as_dict=not (ordered), as_ordereddict=ordered)
def first(self, default=None, as_dict=False, as_ordereddict=False):
"""Returns a single record for the RecordCollection, or `default`. If
`default` is an instance or subclass of Exception, then raise it
instead of returning it."""
# Try to get a record, or return/raise default.
try:
record = self[0]
except IndexError:
if isexception(default):
raise default
return default
# Cast and return.
if as_dict:
return record.as_dict()
elif as_ordereddict:
return record.as_dict(ordered=True)
else:
return record
def one(self, default=None, as_dict=False, as_ordereddict=False):
"""Returns a single record for the RecordCollection, ensuring that it
is the only record, or returns `default`. If `default` is an instance
or subclass of Exception, then raise it instead of returning it."""
# Ensure that we don't have more than one row.
try:
self[1]
except IndexError:
return self.first(
default=default, as_dict=as_dict, as_ordereddict=as_ordereddict
)
else:
raise ValueError(
"RecordCollection contained more than one row. "
"Expects only one row when using "
"RecordCollection.one"
)
def scalar(self, default=None):
"""Returns the first column of the first row, or `default`."""
row = self.one()
return row[0] if row else default
class Record(object):
"""A row, from a query, from a database."""
__slots__ = ("_keys", "_values")
def __init__(self, keys, values):
self._keys = keys
self._values = values
# Ensure that lengths match properly.
assert len(self._keys) == len(self._values)
def keys(self):
"""Returns the list of column names from the query."""
return self._keys
def values(self):
"""Returns the list of values from the query."""
return self._values
def __getitem__(self, key):
# Support for index-based lookup.
if isinstance(key, int):
return self.values()[key]
# Support for string-based lookup.
if key in self.keys():
i = self.keys().index(key)
if self.keys().count(key) > 1:
raise KeyError("Record contains multiple '{}' fields.".format(key))
return self.values()[i]
raise KeyError("Record contains no '{}' field.".format(key))
def __getattr__(self, key):
try:
return self[key]
except KeyError as e:
raise AttributeError(e)
def __dir__(self):
standard = dir(super(Record, self))
# Merge standard attrs with generated ones (from column names).
return sorted(standard + [str(k) for k in self.keys()])
def get(self, key, default=None):
"""Returns the value for a given key, or default."""
try:
return self[key]
except KeyError:
return default
def as_dict(self, ordered=False):
"""Returns the row as a dictionary, as ordered."""
items = zip(self.keys(), self.values())
return OrderedDict(items) if ordered else dict(items)
class Database:
def __init__(self, secret_arn, resource_arn, dbname, conn=None):
self._secret_arn = secret_arn
self._resource_arn = resource_arn
self._dbname = dbname
self._transactionId = None
if not conn:
aurora = boto3.client("rds-data")
self._conn = aurora
else:
self._conn = conn
def _auth(self):
return {
"secretArn": self._secret_arn,
"resourceArn": self._resource_arn,
}
@contextmanager
def transaction(self):
"""A context manager for executing a transaction on this Database."""
tx = self._conn.begin_transaction(**self._auth(), database=self._dbname)
self._transactionId = tx['transactionId']
try:
yield self._transactionId
self._conn.commit_transaction(**self._auth(), transactionId=self._transactionId)
except:
self._conn.rollback_transaction(**self._auth(), transactionId=self._transactionId)
raise
finally:
self._transactionId = None
def query(self, sql, fetchall=False, **params):
attrs = {
**self._auth(),
"database": self._dbname,
"sql": f"{sql}",
"includeResultMetadata": True,
}
if params:
attrs["parameters"] = self._build_parameters(**params)
if self._transactionId:
attrs["transactionId"] = self._transactionId
result = self._conn.execute_statement(**attrs)
if "records" in result:
columns = [meta["label"] for meta in result["columnMetadata"]]
values = [self._fetch_value(r) for r in result["records"]]
row_gen = (Record(columns, row) for row in values)
records = RecordCollection(row_gen)
if fetchall:
records.all()
else:
record = Record(["records_updated"], [result["numberOfRecordsUpdated"]])
records = RecordCollection(iter([record]))
return records
def _fetch_value(self, record):
values = [value[0] for value in [list(field.values()) for field in record]]
# transform isNull to None
keys = [key[0] for key in [list(field.keys()) for field in record]]
for idx, key in enumerate(keys):
if key == 'isNull':
values[idx] = None
return values
def _build_parameters(self, **params):
params = [self._build_field(field, value) for field, value in params.items()]
return params
def _build_field(self, field, value):
fieldtype = type(value).__name__
typemap = {
"str": "stringValue",
"int": "longValue",
"bool": "booleanValue",
"float": "doubleValue",
"NoneType": "isNull"
}
if fieldtype == "NoneType":
value = True
return {"name": field, "value": {typemap[fieldtype]: value}}