Revision 2653
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql_gen.py | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import operator |
4 | 4 |
import re |
5 |
import UserDict |
|
5 | 6 |
|
6 | 7 |
import objects |
7 | 8 |
import strings |
... | ... | |
175 | 176 |
if isinstance(col, NamedCol): col = col.code |
176 | 177 |
return col |
177 | 178 |
|
178 |
class ColDict(dict):
|
|
179 |
class ColDict(UserDict.DictMixin):
|
|
179 | 180 |
'''A dict that automatically makes inserted entries Col objects''' |
180 | 181 |
|
181 |
'''For params, see dict()''' |
|
182 | 182 |
def __init__(self, db, keys_table, dict_={}): |
183 |
dict.__init__(self, dict_) |
|
184 |
|
|
185 | 183 |
keys_table = as_Table(keys_table) |
186 | 184 |
|
187 | 185 |
self.db = db |
188 | 186 |
self.table = keys_table |
187 |
self.dict = {} |
|
188 |
self.update(dict_) # after setting vars because __setitem__() needs them |
|
189 | 189 |
|
190 |
def keys(self): return self.dict.keys() |
|
191 |
|
|
190 | 192 |
def __getitem__(self, key): |
191 |
return dict.__getitem__(self, self._key(key))
|
|
193 |
return self.dict[self._key(key)]
|
|
192 | 194 |
|
193 | 195 |
def __setitem__(self, key, value): |
194 | 196 |
key = self._key(key) |
195 |
return dict.__setitem__(self, key, as_Col(value, name=key.name))
|
|
197 |
self.dict[key] = as_Col(value, name=key.name)
|
|
196 | 198 |
|
197 |
def update(self, dict_): |
|
198 |
for key, value in dict_.iteritems(): self[key] = value |
|
199 |
|
|
200 | 199 |
def _key(self, key): return as_Col(key, self.table) |
201 | 200 |
|
202 | 201 |
##### Functions |
lib/sql.py | ||
---|---|---|
881 | 881 |
row_ct_ref[0] += cur.rowcount |
882 | 882 |
return value(cur) |
883 | 883 |
except DuplicateKeyException, e: |
884 |
return value(select(db, table, [pkey_], |
|
885 |
util.dict_subset_right_join(row, e.cols), recover=True)) |
|
884 |
row = sql_gen.ColDict(db, table, |
|
885 |
util.dict_subset_right_join(row, e.cols)) |
|
886 |
return value(select(db, table, [pkey_], row, recover=True)) |
|
886 | 887 |
|
887 | 888 |
def get(db, table, row, pkey, row_ct_ref=None, create=False): |
888 | 889 |
'''Recovers from errors''' |
Also available in: Unified diff
sql_gen.py: ColDict: Extend UserDict.DictMixin instead of dict because its non-core function implementations route all inner dict accesses to the core functions getitem() and setitem(). sql.py: put(): DuplicateKeyException: Wrap util.dict_subset_right_join() in a sql_gen.ColDict because the dict returned by util.dict_subset_right_join() is just a plain dict. (This change must happen at the same time because the previous functionality relied on a bug in ColDict.)