]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
type tweak
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 6 Nov 2005 21:20:44 +0000 (21:20 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 6 Nov 2005 21:20:44 +0000 (21:20 +0000)
doc/build/content/types.myt
lib/sqlalchemy/types.py

index 07916270406419ce396ea3587a0b6f227fa70599..6f4de361954628ec4115c990c47076dfc71e9cc7 100644 (file)
@@ -35,19 +35,19 @@ class BOOLEAN(Boolean): pass
 </&>
 
 <&|doclib.myt:item, name="custom", description="Creating your Own Types" &>
-<p>Types also support pre-processing of query parameters as well as post-processing of result set data.  You can make your own type classes and specify them just like the standard types:</p>
+<p>Types also support pre-processing of query parameters as well as post-processing of result set data.  You can make your own classes to perform these operations.  They are specified by subclassing the desired type class as well as the special mixin TypeDecorator, which manages the adaptation of the underlying type to a database-specific type:</p>
 <&|formatting.myt:code&>
     import sqlalchemy.types as types
 
-    class MyType(types.String):
-        """basic type that subclasses String, prefixes values with "PREFIX:" on 
+    class MyType(types.TypeDecorator, types.String):
+        """basic type that decorates String, prefixes values with "PREFIX:" on 
         the way in and strips it off on the way out."""
         def convert_bind_param(self, value):
             return "PREFIX:" + value
         def convert_result_value(self, value):
             return value[7:]
 </&>
-<p>Another example, which illustrates a fully defined datatype:</p>
+<p>Another example, which illustrates a fully defined datatype.  This just overrides the base type class TypeEngine:</p>
 <&|formatting.myt:code&>
     import sqlalchemy.types as types
 
index 9a9e5423f25c496bf2138e3063262f7acf7a77ef..e9bc6d89fad4fde9b0f535b60f8fdee41f417c71 100644 (file)
@@ -15,7 +15,7 @@
 # along with this library; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-__ALL__ = [
+__ALL__ = [ 'TypeEngine', 'TypeDecorator', 'NullTypeEngine',
             'INT', 'CHAR', 'VARCHAR', 'TEXT', 'FLOAT', 'DECIMAL', 
             'TIMESTAMP', 'DATETIME', 'CLOB', 'BLOB', 'BOOLEAN', 'String', 'Integer', 'Numeric', 'DateTime', 'Binary', 'Boolean', 'NULLTYPE'
             ]
@@ -55,6 +55,16 @@ class NullTypeEngine(TypeEngine):
     def convert_result_value(self, value):
         return value
 
+class TypeDecorator(object):
+    def get_col_spec(self):
+        return self.extended.get_col_spec()
+    def adapt(self, typeobj):
+        t = self.__class__.__mro__[2]
+        print repr(t)
+        c = self.__class__()
+        c.extended = t.adapt(self, typeobj)
+        return c
+    
 class String(NullTypeEngine):
     def __init__(self, length = None):
         self.length = length