[Sqlalchemy-commits] sqlalchemy: - Columns of _Binary type (i.e. LargeBinary, BLOB, e...
Brought to you by:
zzzeek
From: <co...@sq...> - 2010-05-22 23:09:06
|
details: http://hg.sqlalchemy.org/sqlalchemy/sqlalchemy/rev/19922de7317c changeset: 6604:19922de7317c user: zzzeek date: Sat May 22 19:08:48 2010 -0400 description: - Columns of _Binary type (i.e. LargeBinary, BLOB, etc.) will coerce a "basestring" on the right side into a _Binary as well so that required DBAPI processing takes place. diffstat: CHANGES | 5 +++++ lib/sqlalchemy/types.py | 6 ++++++ test/sql/test_types.py | 13 ++++++++++++- 3 files changed, 23 insertions(+), 1 deletions(-) diffs (54 lines): diff -r b923acca9b5e -r 19922de7317c CHANGES --- a/CHANGES Fri May 21 17:44:56 2010 -0400 +++ b/CHANGES Sat May 22 19:08:48 2010 -0400 @@ -30,6 +30,11 @@ is like `col.in_(text("select id from table"))`. [ticket:1793] + - Columns of _Binary type (i.e. LargeBinary, BLOB, etc.) + will coerce a "basestring" on the right side into a + _Binary as well so that required DBAPI processing + takes place. + - Added table.add_is_dependent_on(othertable), allows manual placement of dependency rules between two Table objects for use within create_all(), drop_all(), sorted_tables. diff -r b923acca9b5e -r 19922de7317c lib/sqlalchemy/types.py --- a/lib/sqlalchemy/types.py Fri May 21 17:44:56 2010 -0400 +++ b/lib/sqlalchemy/types.py Sat May 22 19:08:48 2010 -0400 @@ -1191,6 +1191,12 @@ process = processors.to_str return process # end Py2K + + def _coerce_compared_value(self, op, value): + if isinstance(value, basestring): + return self + else: + return super(_Binary, self)._coerce_compared_value(op, value) def adapt(self, impltype): return impltype(length=self.length) diff -r b923acca9b5e -r 19922de7317c test/sql/test_types.py --- a/test/sql/test_types.py Fri May 21 17:44:56 2010 -0400 +++ b/test/sql/test_types.py Sat May 22 19:08:48 2010 -0400 @@ -663,7 +663,18 @@ eq_(testobj2, l[1]['pickled']) eq_(testobj3.moredata, l[0]['mypickle'].moredata) eq_(l[0]['mypickle'].stuff, 'this is the right stuff') - + + def test_comparison(self): + """test that type coercion occurs on comparison for binary""" + + expr = binary_table.c.data == 'foo' + assert isinstance(expr.right.type, LargeBinary) + + data = os.urandom(32) + binary_table.insert().execute(data=data) + eq_(binary_table.select().where(binary_table.c.data==data).count().scalar(), 1) + + def load_stream(self, name): f = os.path.join(os.path.dirname(__file__), "..", name) return open(f, mode='rb').read() |