From 6033b074094d96f17640cc9ab8ea86e6de94927a Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 22 May 2010 19:08:48 -0400 Subject: [PATCH] - 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. --- CHANGES | 5 +++++ lib/sqlalchemy/types.py | 6 ++++++ test/sql/test_types.py | 13 ++++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 17ede848de..997e046c10 100644 --- a/CHANGES +++ b/CHANGES @@ -30,6 +30,11 @@ CHANGES 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 --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index 4d86dc3625..9a9f2d1cdf 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -1191,6 +1191,12 @@ class _Binary(TypeEngine): 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 --git a/test/sql/test_types.py b/test/sql/test_types.py index 5bdaca6c73..88875d1393 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -663,7 +663,18 @@ class BinaryTest(TestBase, AssertsExecutionResults): 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() -- 2.47.2