class UserDefinedType(util.with_metaclass(VisitableCheckKWArg, ExternalType, TypeEngine)):
Base for user defined types.
This should be the base of new types. Note that
for most cases, .TypeDecorator
is probably
more appropriate:
import sqlalchemy.types as types class MyType(types.UserDefinedType): cache_ok = True def __init__(self, precision = 8): self.precision = precision def get_col_spec(self, **kw): return "MYTYPE(%s)" % self.precision def bind_processor(self, dialect): def process(value): return value return process def result_processor(self, dialect, coltype): def process(value): return value return process
Once the type is made, it's immediately usable:
table = Table('foo', metadata_obj, Column('id', Integer, primary_key=True), Column('data', MyType(16)) )
The get_col_spec() method will in most cases receive a keyword
argument type_expression which refers to the owning expression
of the type as being compiled, such as a _schema.Column
or
.cast
construct. This keyword is only sent if the method
accepts keyword arguments (e.g. **kw) in its argument signature;
introspection is used to check for this in order to support legacy
forms of this function.
The .UserDefinedType.cache_ok
class-level flag indicates if this
custom .UserDefinedType
is safe to be used as part of a cache key.
This flag defaults to None which will initially generate a warning
when the SQL compiler attempts to generate a cache key for a statement
that uses this type. If the .UserDefinedType
is not guaranteed
to produce the same bind/result behavior and SQL generation
every time, this flag should be set to False; otherwise if the
class produces the same behavior each time, it may be set to True.
See .UserDefinedType.cache_ok
for further notes on how this works.
.ExternalType.cache_ok
flag so that it is available for both .TypeDecorator
as well
as .UserDefinedType
.Method | coerce_compared_value |
Suggest a type for a 'coerced' Python value in an expression. |
Class Variable | __visit_name__ |
Undocumented |
Class Variable | ensure_kwarg |
Undocumented |
Suggest a type for a 'coerced' Python value in an expression.
Default behavior for .UserDefinedType
is the
same as that of .TypeDecorator
; by default it returns
self, assuming the compared value should be coerced into
the same type as this one. See
.TypeDecorator.coerce_compared_value
for more detail.