class IntegerField(fields.IntegerField):
def get_db_prep_lookup(self, lookup_type, value):
- if lookup_type == 'exact':
+ if lookup_type in ('exact', 'gte', 'lte'):
return [value]
raise TypeError("IntegerField has invalid lookup: %s" % lookup_type)
from ldapdb.models.fields import CharField
+def get_lookup_operator(lookup_type):
+ if lookup_type == 'gte':
+ return '>='
+ elif lookup_type == 'lte':
+ return '<='
+ else:
+ return '='
+
class Constraint(BaseConstraint):
"""
An object that can be passed to WhereNode.add() and knows how to
constraint, x, y, values = item
if hasattr(constraint, "col"):
# django 1.2
- clause = "(%s=%s)" % (constraint.col, values)
+ comp = get_lookup_operator(constraint.lookup_type)
+ clause = "(%s%s%s)" % (constraint.col, comp, values)
else:
# django 1.1
- (table, column, type) = constraint
- equal_bits = [ "(%s=%s)" % (column, value) for value in values ]
+ (table, column, db_type) = constraint
+ comp = get_lookup_operator(x)
+ equal_bits = [ "(%s%s%s)" % (column, comp, value) for value in values ]
if len(equal_bits) == 1:
clause = equal_bits[0]
else:
from django.test import TestCase
from django.db.models.sql.where import Constraint, AND, OR
-from ldapdb.models.query import escape_ldap_filter
+from ldapdb import escape_ldap_filter
from ldapdb.models.fields import CharField, IntegerField, ListField
from ldapdb.models.query import WhereNode
def test_integer_field(self):
where = WhereNode()
- where.add((Constraint("uid", "uid", CharField()), 'exact', 1), AND)
+ where.add((Constraint("uid", "uid", IntegerField()), 'exact', 1), AND)
self.assertEquals(where.as_sql(), "(uid=1)")
+ where = WhereNode()
+ where.add((Constraint("uid", "uid", IntegerField()), 'gte', 1), AND)
+ self.assertEquals(where.as_sql(), "(uid>=1)")
+
+ where = WhereNode()
+ where.add((Constraint("uid", "uid", IntegerField()), 'lte', 1), AND)
+ self.assertEquals(where.as_sql(), "(uid<=1)")
+
def test_and(self):
where = WhereNode()
where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)