diff options
-rw-r--r-- | CHANGES | 5 | ||||
-rw-r--r-- | doc/build/content/types.txt | 20 | ||||
-rw-r--r-- | lib/sqlalchemy/types.py | 75 | ||||
-rw-r--r-- | test/orm/relationships.py | 4 | ||||
-rw-r--r-- | test/sql/testtypes.py | 46 |
5 files changed, 32 insertions, 118 deletions
@@ -136,7 +136,10 @@ CHANGES - The 'length' argument to all Numeric types has been renamed to 'scale'. 'length' is deprecated and is still accepted with a warning. - + + - Dropped 0.3-compatibility for user defined types + (convert_result_value, convert_bind_param). + - sql - Temporarily rolled back the "ORDER BY" enhancement from [ticket:1068]. This feature is on hold pending further diff --git a/doc/build/content/types.txt b/doc/build/content/types.txt index 2fdf424ea..764f4e537 100644 --- a/doc/build/content/types.txt +++ b/doc/build/content/types.txt @@ -128,9 +128,7 @@ User-defined types can be created which can augment the bind parameter and resul def copy(self): return MyType(self.impl.length) -Note that the "old" way to process bind parameters and result values, the `convert_bind_param()` and `convert_result_value()` methods, are still available. The downside of these is that when using a type which already processes data such as the `Unicode` type, you need to call the superclass version of these methods directly. Using `process_bind_param()` and `process_result_value()`, user-defined code can return and receive the desired Python data directly. - -As of version 0.4.2, `TypeDecorator` should generally be used for any user-defined type which redefines the behavior of another type, including other `TypeDecorator` subclasses such as `PickleType`, and the new `process_...()` methods described above should be used. +`TypeDecorator` should generally be used for any user-defined type which redefines the behavior of another type, including other `TypeDecorator` subclasses such as `PickleType`, and the new `process_...()` methods described above should be used. To build a type object from scratch, which will not have a corresponding database-specific implementation, subclass `TypeEngine`: @@ -140,13 +138,19 @@ To build a type object from scratch, which will not have a corresponding databas class MyType(types.TypeEngine): def __init__(self, precision = 8): self.precision = precision - + def get_col_spec(self): return "MYTYPE(%s)" % self.precision - - def convert_bind_param(self, value, dialect): - return value - + + def bind_processor(self, dialect): + def process(value): + return value + return process + def result_processor(self, dialect): + def process(value): + return value + return process + def convert_result_value(self, value, dialect): return value diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index 21762c663..3690ed3ca 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -29,39 +29,8 @@ from sqlalchemy import exc from sqlalchemy.util import pickle import sqlalchemy.util as util NoneType = type(None) - -class _UserTypeAdapter(type): - """adapts 0.3 style user-defined types with convert_bind_param/convert_result_value - to use newer bind_processor()/result_processor() methods.""" - - def __init__(cls, clsname, bases, dict): - if not hasattr(cls.convert_result_value, '_sa_override'): - cls.__instrument_result_proc(cls) - - if not hasattr(cls.convert_bind_param, '_sa_override'): - cls.__instrument_bind_proc(cls) - - return super(_UserTypeAdapter, cls).__init__(clsname, bases, dict) - - def __instrument_bind_proc(cls, class_): - def bind_processor(self, dialect): - def process(value): - return self.convert_bind_param(value, dialect) - return process - class_.super_bind_processor = class_.bind_processor - class_.bind_processor = bind_processor - - def __instrument_result_proc(cls, class_): - def result_processor(self, dialect): - def process(value): - return self.convert_result_value(value, dialect) - return process - class_.super_result_processor = class_.result_processor - class_.result_processor = result_processor - - + class AbstractType(object): - __metaclass__ = _UserTypeAdapter def __init__(self, *args, **kwargs): pass @@ -69,48 +38,6 @@ class AbstractType(object): def copy_value(self, value): return value - def convert_result_value(self, value, dialect): - """Legacy convert_result_value() compatibility method. - - This adapter method is provided for user-defined types that implement - the older convert_* interface and need to call their super method. - These calls are adapted behind the scenes to use the newer - callable-based interface via result_processor(). - - Compatibility is configured on a case-by-case basis at class - definition time by a legacy adapter metaclass. This method is only - available and functional if the concrete subclass implements the - legacy interface. - """ - - processor = self.super_result_processor(dialect) - if processor: - return processor(value) - else: - return value - convert_result_value._sa_override = True - - def convert_bind_param(self, value, dialect): - """Legacy convert_bind_param() compatability method. - - This adapter method is provided for user-defined types that implement - the older convert_* interface and need to call their super method. - These calls are adapted behind the scenes to use the newer - callable-based interface via bind_processor(). - - Compatibility is configured on a case-by-case basis at class - definition time by a legacy adapter metaclass. This method is only - available and functional if the concrete subclass implements the - legacy interface. - """ - - processor = self.super_bind_processor(dialect) - if processor: - return processor(value) - else: - return value - convert_bind_param._sa_override = True - def bind_processor(self, dialect): """Defines a bind parameter processing function.""" diff --git a/test/orm/relationships.py b/test/orm/relationships.py index 87f674b9f..fcb1807d6 100644 --- a/test/orm/relationships.py +++ b/test/orm/relationships.py @@ -721,9 +721,9 @@ class TypedAssociationTable(_base.MappedTest): def define_tables(self, metadata): class MySpecialType(sa.types.TypeDecorator): impl = String - def convert_bind_param(self, value, dialect): + def process_bind_param(self, value, dialect): return "lala" + value - def convert_result_value(self, value, dialect): + def process_result_value(self, value, dialect): return value[4:] Table('t1', metadata, diff --git a/test/sql/testtypes.py b/test/sql/testtypes.py index 345fbceac..8642c4fa3 100644 --- a/test/sql/testtypes.py +++ b/test/sql/testtypes.py @@ -108,9 +108,15 @@ class UserDefinedTest(TestBase): def testprocessing(self): global users - users.insert().execute(user_id = 2, goofy = 'jack', goofy2='jack', goofy4=u'jack', goofy5=u'jack', goofy6='jack', goofy7=u'jack', goofy8=12, goofy9=12) - users.insert().execute(user_id = 3, goofy = 'lala', goofy2='lala', goofy4=u'lala', goofy5=u'lala', goofy6='lala', goofy7=u'lala', goofy8=15, goofy9=15) - users.insert().execute(user_id = 4, goofy = 'fred', goofy2='fred', goofy4=u'fred', goofy5=u'fred', goofy6='fred', goofy7=u'fred', goofy8=9, goofy9=9) + users.insert().execute( + user_id=2, goofy='jack', goofy2='jack', goofy4=u'jack', + goofy7=u'jack', goofy8=12, goofy9=12) + users.insert().execute( + user_id=3, goofy='lala', goofy2='lala', goofy4=u'lala', + goofy7=u'lala', goofy8=15, goofy9=15) + users.insert().execute( + user_id=4, goofy='fred', goofy2='fred', goofy4=u'fred', + goofy7=u'fred', goofy8=9, goofy9=9) l = users.select().execute().fetchall() for assertstr, assertint, assertint2, row in zip( @@ -118,13 +124,12 @@ class UserDefinedTest(TestBase): [1200, 1500, 900], [1800, 2250, 1350], l - ): - for col in row[1:7]: + for col in row[1:5]: self.assertEquals(col, assertstr) - self.assertEquals(row[7], assertint) - self.assertEquals(row[8], assertint2) - for col in (row[3], row[4], row[6]): + self.assertEquals(row[5], assertint) + self.assertEquals(row[6], assertint2) + for col in row[3], row[4]: assert isinstance(col, unicode) def setUpAll(self): @@ -209,28 +214,6 @@ class UserDefinedTest(TestBase): def copy(self): return MyUnicodeType(self.impl.length) - class LegacyType(types.TypeEngine): - def get_col_spec(self): - return "VARCHAR(100)" - def convert_bind_param(self, value, dialect): - return "BIND_IN"+ value - def convert_result_value(self, value, dialect): - return value + "BIND_OUT" - def adapt(self, typeobj): - return typeobj() - - class LegacyUnicodeType(types.TypeDecorator): - impl = Unicode - - def convert_bind_param(self, value, dialect): - return "BIND_IN" + super(LegacyUnicodeType, self).convert_bind_param(value, dialect) - - def convert_result_value(self, value, dialect): - return super(LegacyUnicodeType, self).convert_result_value(value, dialect) + "BIND_OUT" - - def copy(self): - return LegacyUnicodeType(self.impl.length) - metadata = MetaData(testing.db) users = Table('type_users', metadata, Column('user_id', Integer, primary_key = True), @@ -241,12 +224,9 @@ class UserDefinedTest(TestBase): Column('goofy2', MyDecoratedType(50), nullable = False), Column('goofy4', MyUnicodeType(50), nullable = False), - Column('goofy5', LegacyUnicodeType(50), nullable = False), - Column('goofy6', LegacyType, nullable = False), Column('goofy7', MyNewUnicodeType(50), nullable = False), Column('goofy8', MyNewIntType, nullable = False), Column('goofy9', MyNewIntSubClass, nullable = False), - ) metadata.create_all() |