The Types System
Version: 0.1.0 Last Updated: 02/13/06 21:52:15
View: Paged  |  One Page

The package sqlalchemy.types defines the datatype identifiers which may be used when defining table metadata. This package includes a set of generic types, a set of SQL-specific subclasses of those types, and a small extension system used by specific database connectors to adapt these generic types into database-specific type objects.

Built-in Types

SQLAlchemy comes with a set of standard generic datatypes, which are defined as classes. They are specified to table meta data using either the class itself, or an instance of the class. Creating an instance of the class allows you to specify parameters for the type, such as string length, numerical precision, etc.

The standard set of generic types are:

class String(TypeEngine):
    def __init__(self, length=None)
    
class Integer(TypeEngine)
    
class Numeric(TypeEngine): 
    def __init__(self, precision=10, length=2)
    
class Float(TypeEngine):
    def __init__(self, precision=10)
    
class DateTime(TypeEngine)
    
class Binary(TypeEngine): 
    def __init__(self, length=None)
    
class Boolean(TypeEngine)

More specific subclasses of these types are available, to allow finer grained control over types:

class FLOAT(Numeric)
class TEXT(String)
class DECIMAL(Numeric)
class INT(Integer)
INTEGER = INT
class TIMESTAMP(DateTime)
class DATETIME(DateTime)
class CLOB(String)
class VARCHAR(String)
class CHAR(String)
class BLOB(Binary)
class BOOLEAN(Boolean)

When using a specific database engine, these types are adapted even further via a set of database-specific subclasses defined by the database engine.

back to section top
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 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:

import sqlalchemy.types as types

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, engine):
        return "PREFIX:" + value
    def convert_result_value(self, value, engine):
        return value[7:]

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

import sqlalchemy.types as types

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, engine):
        return value
    def convert_result_value(self, value, engine):
        return value
    def adapt(self, typeobj):
        """produces an adaptation of this object given a type which is a subclass of this object"""
        return typeobj(self.precision)
    def adapt_args(self):
        """allows for the adaptation of this TypeEngine object into a new kind of type depending on its arguments."""
        return self
back to section top