From c133b136e14c9ad7023f396abbcdd109513407db Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 8 Jun 2006 17:29:18 +0000 Subject: [PATCH] fixed typing for between() operator, [ticket:202] --- lib/sqlalchemy/sql.py | 11 ++++++++--- test/sql/select.py | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/sqlalchemy/sql.py b/lib/sqlalchemy/sql.py index 2af45c6da6..48c9f87546 100644 --- a/lib/sqlalchemy/sql.py +++ b/lib/sqlalchemy/sql.py @@ -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): diff --git a/test/sql/select.py b/test/sql/select.py index 19d39e41f1..e246ece89d 100644 --- a/test/sql/select.py +++ b/test/sql/select.py @@ -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())) -- 2.47.2