Revision 1c11bace snf-cyclades-app/synnefo/db/managers.py
b/snf-cyclades-app/synnefo/db/managers.py | ||
---|---|---|
49 | 49 |
|
50 | 50 |
""" |
51 | 51 |
|
52 |
def __init__(self, *args, **kwargs): |
|
53 |
super(ForUpdateManager, self).__init__(*args, **kwargs) |
|
54 |
self._select_for_update = False |
|
52 |
def select_for_update(self, *args, **kwargs): |
|
53 |
return ForUpdateQuerySet(self.model, using=self.db) |
|
54 |
|
|
55 |
|
|
56 |
class ForUpdateQuerySet(QuerySet): |
|
57 |
""" QuerySet implmenting SELECT .. FOR UPDATE statement |
|
58 |
|
|
59 |
This QuerySet overrides filter and get methods in order to implement |
|
60 |
select_for_update() statement, by appending 'FOR UPDATE' to the end |
|
61 |
for the SQL query. |
|
62 |
|
|
63 |
""" |
|
55 | 64 |
|
56 | 65 |
def filter(self, *args, **kwargs): |
57 |
query = self.get_query_set().filter(*args, **kwargs) |
|
58 |
if self._select_for_update: |
|
59 |
self._select_for_update = False |
|
60 |
return for_update(query) |
|
61 |
else: |
|
62 |
return query |
|
66 |
query = super(ForUpdateQuerySet, self).filter(*args, **kwargs) |
|
67 |
return for_update(query) |
|
63 | 68 |
|
64 | 69 |
def get(self, *args, **kwargs): |
65 |
if not self._select_for_update: |
|
66 |
return self.get_query_set().get(*args, **kwargs) |
|
67 |
|
|
68 | 70 |
query = self.filter(*args, **kwargs) |
69 | 71 |
query = list(query) |
70 | 72 |
num = len(query) |
... | ... | |
80 | 82 |
"Lookup parameters were %s" % |
81 | 83 |
(self.model._meta.object_name, num, kwargs)) |
82 | 84 |
|
83 |
def select_for_update(self, *args, **kwargs): |
|
84 |
self._select_for_update = True |
|
85 |
return self |
|
86 |
|
|
87 | 85 |
|
88 | 86 |
def for_update(query): |
89 | 87 |
""" Rewrite query using SELECT .. FOR UPDATE. |
Also available in: Unified diff