From: Mike Bayer Date: Sun, 6 Nov 2005 21:20:44 +0000 (+0000) Subject: type tweak X-Git-Tag: rel_0_1_0~364 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=baca2befc8b3e5f88d796c42cec84134850494de;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git type tweak --- diff --git a/doc/build/content/types.myt b/doc/build/content/types.myt index 0791627040..6f4de36195 100644 --- a/doc/build/content/types.myt +++ b/doc/build/content/types.myt @@ -35,19 +35,19 @@ class BOOLEAN(Boolean): pass <&|doclib.myt:item, name="custom", description="Creating your Own Types" &> -

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:

+

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:

<&|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:] -

Another example, which illustrates a fully defined datatype:

+

Another example, which illustrates a fully defined datatype. This just overrides the base type class TypeEngine:

<&|formatting.myt:code&> import sqlalchemy.types as types diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index 9a9e5423f2..e9bc6d89fa 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -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