From: Mike Bayer Date: Tue, 20 Dec 2005 05:24:51 +0000 (+0000) Subject: in_ clause uses bind params, for typing etc. X-Git-Tag: rel_0_1_0~226 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3ccf3408073b0452435bb5704b342c14a15518f4;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git in_ clause uses bind params, for typing etc. --- diff --git a/lib/sqlalchemy/sql.py b/lib/sqlalchemy/sql.py index 99755ae6c9..ab762cb39e 100644 --- a/lib/sqlalchemy/sql.py +++ b/lib/sqlalchemy/sql.py @@ -412,7 +412,7 @@ class CompareMixin(object): elif len(other) == 1 and not isinstance(other[0], Selectable): return self.__eq__(other[0]) elif _is_literal(other[0]): - return self._compare('IN', ClauseList(parens=True, *[TextClause(o, isliteral=True) for o in other])) + return self._compare('IN', ClauseList(parens=True, *[self._bind_param(o) for o in other])) else: # assume *other is a list of selects. # so put them in a UNION. if theres only one, you just get one SELECT @@ -433,6 +433,8 @@ class CompareMixin(object): return self._compare('/', other) def __truediv__(self, other): return self._compare('/', other) + def _bind_param(self, obj): + return BindParamClause('literal', obj, shortname=None, type=self.type) def _compare(self, operator, obj): if _is_literal(obj): if obj is None: @@ -440,7 +442,7 @@ class CompareMixin(object): raise "Only '=' operator can be used with NULL" return BinaryClause(self, null(), 'IS') else: - obj = BindParamClause('literal', obj, shortname=None, type=self.type) + obj = self._bind_param(obj) return BinaryClause(self, obj, operator) @@ -825,16 +827,20 @@ class ColumnImpl(Selectable, CompareMixin): def _get_from_objects(self): return [self.column.table] + def _bind_param(self, obj): + if self.column.table.name is None: + return BindParamClause(self.name, obj, shortname = self.name, type = self.column.type) + else: + return BindParamClause(self.column.table.name + "_" + self.name, obj, shortname = self.name, type = self.column.type) + def _compare(self, operator, obj): if _is_literal(obj): if obj is None: if operator != '=': raise "Only '=' operator can be used with NULL" return BinaryClause(self.column, null(), 'IS') - elif self.column.table.name is None: - obj = BindParamClause(self.name, obj, shortname = self.name, type = self.column.type) else: - obj = BindParamClause(self.column.table.name + "_" + self.name, obj, shortname = self.name, type = self.column.type) + obj = self._bind_param(obj) return BinaryClause(self.column, obj, operator) diff --git a/test/select.py b/test/select.py index 8a0027beaa..4e2a7cae32 100644 --- a/test/select.py +++ b/test/select.py @@ -384,7 +384,7 @@ FROM mytable, myothertable WHERE mytable.myid = myothertable.otherid AND mytable def testin(self): self.runtest(select([table], table.c.id.in_(1, 2, 3)), - "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.myid IN (1, 2, 3)") + "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.myid IN (:mytable_myid, :mytable_myid_1, :mytable_myid_2)") self.runtest(select([table], table.c.id.in_(select([table2.c.id]))), "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.myid IN (SELECT myothertable.otherid FROM myothertable)")