]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
fixes to TypeDecorator, including A. Houghton's patch
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 11 Feb 2006 19:35:50 +0000 (19:35 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 11 Feb 2006 19:35:50 +0000 (19:35 +0000)
lib/sqlalchemy/types.py
test/engines.py
test/types.py

index 230107554258c7373844df02527d00879367ce11..ce4831ff8f1c5b1ae37ffa69a13a395a89e556cc 100644 (file)
@@ -35,8 +35,8 @@ class TypeEngine(object):
         """returns the class that should be sent to the adapt() method.  This class
         will be used to lookup an approprate database-specific subclass."""
         return self.__class__
-    def __repr__(self):
-        return util.generic_repr(self)
+#    def __repr__(self):
#       return util.generic_repr(self)
         
 def adapt_type(typeobj, colspecs):
     """given a generic type from this package, and a dictionary of 
@@ -46,7 +46,7 @@ def adapt_type(typeobj, colspecs):
         typeobj = typeobj()
     # if the type is not a base type, i.e. not from our module, or its Null, 
     # we return the type as is
-    if typeobj.__module__ != 'sqlalchemy.types' or typeobj.__class__==NullTypeEngine:
+    if (typeobj.__module__ != 'sqlalchemy.types' or typeobj.__class__ is NullTypeEngine) and not isinstance(typeobj, TypeDecorator):
         return typeobj
     typeobj = typeobj.adapt_args()
     t = typeobj.class_to_adapt()
@@ -71,7 +71,11 @@ class TypeDecorator(object):
     def get_col_spec(self):
         return self.extended.get_col_spec()
     def adapt(self, typeobj):
-        self.extended = self.extended.adapt(typeobj)
+        if self.extended is self:
+            t = self.__class__.__mro__[2]
+            self.extended = t.adapt(self, typeobj)
+        else:
+            self.extended = self.extended.adapt(typeobj)
         return self
     def adapt_args(self):
         t = self.__class__.__mro__[2]
index 3d9bd956e97bef08c08ecc35b46307b2e4c45a2b..75ac894a35a111907cf04d78b4d9c99135226fd8 100644 (file)
@@ -139,7 +139,7 @@ class EngineTest(PersistTest):
         
             print repr(table)
             self.assert_(isinstance(table.c.col1.type, Integer))
-            self.assert_(table.c.col2.type.is_unicode)
+            self.assert_(isinstance(table.c.col2.type, Unicode))
             self.assert_(isinstance(table.c.col4.type, String))
         finally:
             table.drop()
index 4de4daa4210cfe29f88f92839c2c7ce9d52c4be7..77452e51b1670cfe84f8867aba1337daa162117d 100644 (file)
@@ -6,6 +6,7 @@ import testbase
 db = testbase.db
 
 class OverrideTest(PersistTest):
+    """tests user-defined types, including a full type as well as a TypeDecorator"""
 
     def testprocessing(self):
         class MyType(types.TypeEngine):
@@ -20,25 +21,35 @@ class OverrideTest(PersistTest):
             def adapt_args(self):
                 return self
 
+        class MyDecoratedType(types.TypeDecorator, types.String):
+            def convert_bind_param(self, value, engine):
+                return "BIND_IN"+ value
+            def convert_result_value(self, value, engine):
+                return value + "BIND_OUT"
+
         global users
         users = Table('users', db, 
             Column('user_id', Integer, primary_key = True),
-            Column('goofy', MyType, nullable = False)
+            # totall custom type
+            Column('goofy', MyType, nullable = False),
+            
+            # decorated type with an argument, so its a String
+            Column('goofy2', MyDecoratedType(50), nullable = False),
+            
+            # decorated type without an argument, it will adapt_args to TEXT
+            Column('goofy3', MyDecoratedType, nullable = False),
         )
         
         users.create()
         
-        users.insert().execute(user_id = 2, goofy = 'jack')
-        users.insert().execute(user_id = 3, goofy = 'lala')
-        users.insert().execute(user_id = 4, goofy = 'fred')
+        users.insert().execute(user_id = 2, goofy = 'jack', goofy2='jack', goofy3='jack')
+        users.insert().execute(user_id = 3, goofy = 'lala', goofy2='lala', goofy3='lala')
+        users.insert().execute(user_id = 4, goofy = 'fred', goofy2='fred', goofy3='fred')
         
         l = users.select().execute().fetchall()
         print repr(l)
-        self.assert_(l == [(2, u'BIND_INjackBIND_OUT'), (3, u'BIND_INlalaBIND_OUT'), (4, u'BIND_INfredBIND_OUT')])
+        self.assert_(l == [(2, 'BIND_INjackBIND_OUT', 'BIND_INjackBIND_OUT', 'BIND_INjackBIND_OUT'), (3, 'BIND_INlalaBIND_OUT', 'BIND_INlalaBIND_OUT', 'BIND_INlalaBIND_OUT'), (4, 'BIND_INfredBIND_OUT', 'BIND_INfredBIND_OUT', 'BIND_INfredBIND_OUT')])
 
-        l = users.select(use_labels=True).execute().fetchall()
-        print repr(l)
-        self.assert_(l == [(2, u'BIND_INjackBIND_OUT'), (3, u'BIND_INlalaBIND_OUT'), (4, u'BIND_INfredBIND_OUT')])
 
     def tearDownAll(self):
         global users
@@ -71,11 +82,12 @@ class ColumnsTest(AssertMixin):
             self.assertEquals(expectedResults[aCol.name], db.schemagenerator(None).get_column_specification(aCol))
         
 class UnicodeTest(AssertMixin):
+    """tests the Unicode type.  also tests the TypeDecorator with instances in the types package."""
     def setUpAll(self):
         global unicode_table
         unicode_table = Table('unicode_table', db, 
             Column('id', Integer, primary_key=True),
-            Column('unicode_data', Unicode),
+            Column('unicode_data', Unicode(50)),
             Column('plain_data', String)
             )
         unicode_table.create()