Created
November 9, 2010 13:30
-
-
Save dchaplinsky/669070 to your computer and use it in GitHub Desktop.
Deep injection into the django ORM to find select requests suitable for Handler Socket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
old_iterator = QuerySet.iterator | |
def new_iterator(self): | |
def drilldown(node, negated): | |
NEGATE_TABLE = { | |
'exact': 'nexact', # bogus, I know | |
'lt': 'gte', | |
'lte': 'gt', | |
'gte': 'lt', | |
'gt': 'lte', | |
} | |
if len(node.children) == 1: | |
if getattr(node, 'negated', False): | |
negated = not negated | |
if hasattr(node.children[0], 'as_sql'): | |
return drilldown(node.children[0], negated) | |
else: | |
lvalue, lookup_type, value_annot, params_or_value = node.children[0] | |
if (lvalue.field.unique or lvalue.field.db_index) and \ | |
lookup_type in NEGATE_TABLE: | |
if negated: | |
lookup_type = NEGATE_TABLE[lookup_type] | |
return (lvalue, lookup_type, value_annot, params_or_value,) | |
else: | |
return False | |
else: | |
return False; | |
# HS don't support grouping | |
if self.query.group_by == None: | |
atom = drilldown(self.query.where, False) | |
if atom: | |
ordering = self.query.extra_order_by or \ | |
self.query.order_by or \ | |
self.query.model._meta.ordering | |
lvalue, lookup_type, value_annot, params_or_value = atom | |
# No ordering support (but we don't need it for pk/unique lookups) | |
if not ordering or \ | |
(lookup_type == 'exact' and lvalue.field.unique): | |
print atom | |
# TODO: Support for limits | |
# print self.query.low_mark | |
# print self.query.high_mark | |
print "%s%s%s" % \ | |
('\033[31m', self.query, '\033[0m',) | |
return old_iterator(self) | |
QuerySet.iterator = new_iterator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment