]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
fixed typing for between() operator, [ticket:202]
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 8 Jun 2006 17:29:18 +0000 (17:29 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 8 Jun 2006 17:29:18 +0000 (17:29 +0000)
lib/sqlalchemy/sql.py
test/sql/select.py

index 2af45c6da6cc5b74eb518bcfe72fa2dc3d2bd50c..48c9f87546ac4b7dc9b3a8cc25dc53fac4cc6be6 100644 (file)
@@ -551,7 +551,7 @@ class CompareMixin(object):
     def distinct(self):
         return CompoundClause(None,"DISTINCT", self)
     def between(self, cleft, cright):
-        return between_(self, cleft, cright)
+        return between_(self, self._check_literal(cleft), self._check_literal(cright))
     def op(self, operator):
         return lambda other: self._compare(operator, other)
     # and here come the math operators:
@@ -569,6 +569,11 @@ class CompareMixin(object):
         return self._operate('/', other)
     def _bind_param(self, obj):
         return BindParamClause('literal', obj, shortname=None, type=self.type)
+    def _check_literal(self, other):
+        if _is_literal(other):
+            return self._bind_param(other)
+        else:
+            return other
     def _compare(self, operator, obj):
         if obj is None or isinstance(obj, Null):
             if operator == '=':
@@ -577,8 +582,8 @@ class CompareMixin(object):
                 return BooleanExpression(self._compare_self(), null(), 'IS NOT')
             else:
                 raise exceptions.ArgumentError("Only '='/'!=' operators can be used with NULL")
-        elif _is_literal(obj):
-            obj = self._bind_param(obj)
+        else:
+            obj = self._check_literal(obj)
 
         return BooleanExpression(self._compare_self(), obj, operator, type=self._compare_type(obj))
     def _operate(self, operator, obj):
index 19d39e41f11ed6dd2bf8b320dbd285918a42bb69..e246ece89dc8938917a86a5353cf70acca168aa3 100644 (file)
@@ -559,6 +559,13 @@ FROM mytable, myothertable WHERE mytable.myid = myothertable.otherid AND mytable
         # and the MySQL engine
         check_results(mysql.dialect(), ['NUMERIC(10, 2)', 'NUMERIC(12, 9)', 'DATE', 'TEXT', 'VARCHAR(20)'], '%s')
 
+
+    def testdatebetween(self):
+        import datetime
+        table = Table('dt', metadata, 
+            Column('date', Date))
+        self.runtest(table.select(table.c.date.between(datetime.date(2006,6,1), datetime.date(2006,6,5))), "SELECT dt.date FROM dt WHERE dt.date BETWEEN :dt_date AND :dt_da_1", checkparams={'dt_date':datetime.date(2006,6,1), 'dt_da_1':datetime.date(2006,6,5)})
+
 class CRUDTest(SQLTest):
     def testinsert(self):
         # generic insert, will create bind params for all columns
@@ -584,6 +591,7 @@ class CRUDTest(SQLTest):
             checkparams = {'myid':3, 'name':'jack', 'description':'mydescription'}
         )
     
+        
     def testinsertexpression(self):
         self.runtest(insert(table1), "INSERT INTO mytable (myid) VALUES (lala())", params=dict(myid=func.lala()))