add support for "greater or equal" and "less or equal" lookups on IntegerFields
authorjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 24 May 2010 08:23:14 +0000 (08:23 +0000)
committerjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 24 May 2010 08:23:14 +0000 (08:23 +0000)
git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@862 e071eeec-0327-468d-9b6a-08194a12b294

ldapdb/models/fields.py
ldapdb/models/query.py
ldapdb/tests.py

index ff0ad722b8e75a8d0067933be4637170e1cf426b..d21bbd0dc258c31bc05a35b8a1ee2d7a0b69eb07 100644 (file)
@@ -50,7 +50,7 @@ class ImageField(fields.Field):
 
 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)
index 67d193607b7bc77c09ac74c4a802388a185df6f1..a8c614f7ccfe6a2ee487ac35b38bb474951efa0c 100644 (file)
@@ -30,6 +30,14 @@ import ldapdb
 
 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
@@ -76,11 +84,13 @@ class WhereNode(BaseWhereNode):
             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:
index 4b6f7f15216ea28b36d1fee8165bc9421552c245..de13b9d97303357a4583617475cafd04d9e8cd21 100644 (file)
@@ -21,7 +21,7 @@
 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
 
@@ -60,9 +60,17 @@ class WhereTestCase(TestCase):
 
     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)