"""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
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()
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]
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):
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
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()