本文整理汇总了Python中sqlalchemy.TypeDecorator方法的典型用法代码示例。如果您正苦于以下问题:Python sqlalchemy.TypeDecorator方法的具体用法?Python sqlalchemy.TypeDecorator怎么用?Python sqlalchemy.TypeDecorator使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy
的用法示例。
在下文中一共展示了sqlalchemy.TypeDecorator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_affinity_typedec
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import TypeDecorator [as 别名]
def test_affinity_typedec(self):
class MyType(TypeDecorator):
impl = CHAR
def load_dialect_impl(self, dialect):
if dialect.name == "sqlite":
return dialect.type_descriptor(Integer())
else:
return dialect.type_descriptor(CHAR(32))
uo = ops.AlterColumnOp("sometable", "somecol")
autogenerate.compare._compare_type(
self.autogen_context,
uo,
None,
"sometable",
"somecol",
Column("somecol", Integer, nullable=True),
Column("somecol", MyType()),
)
assert not uo.has_changes()
示例2: test_typedec_to_nonstandard
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import TypeDecorator [as 别名]
def test_typedec_to_nonstandard(self, impl_fixture):
class PasswordType(TypeDecorator):
impl = VARBINARY
def copy(self, **kw):
return PasswordType(self.impl.length)
def load_dialect_impl(self, dialect):
if dialect.name == "default":
impl = sqlite.NUMERIC(self.length)
else:
impl = VARBINARY(self.length)
return dialect.type_descriptor(impl)
impl_fixture.compare_type(
Column("x", sqlite.NUMERIC(50)), Column("x", PasswordType(50))
)
示例3: test_exception_wrapping_non_dbapi_statement
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import TypeDecorator [as 别名]
def test_exception_wrapping_non_dbapi_statement(self):
class MyType(TypeDecorator):
impl = Integer
def process_bind_param(self, value, dialect):
raise SomeException("nope")
def _go(conn):
assert_raises_message(
tsa.exc.StatementError,
r"\(.*.SomeException\) " r"nope\n\[SQL\: u?SELECT 1 ",
conn.execute,
select([1]).where(column("foo") == literal("bar", MyType())),
)
_go(testing.db)
with testing.db.connect() as conn:
_go(conn)
示例4: test_dont_wrap_mixin
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import TypeDecorator [as 别名]
def test_dont_wrap_mixin(self):
class MyException(Exception, tsa.exc.DontWrapMixin):
pass
class MyType(TypeDecorator):
impl = Integer
def process_bind_param(self, value, dialect):
raise MyException("nope")
def _go(conn):
assert_raises_message(
MyException,
"nope",
conn.execute,
select([1]).where(column("foo") == literal("bar", MyType())),
)
_go(testing.db)
conn = testing.db.connect()
try:
_go(conn)
finally:
conn.close()
示例5: _all_types
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import TypeDecorator [as 别名]
def _all_types(omit_special_types=False):
seen = set()
for typ in _types_for_mod(types):
if omit_special_types and typ in (
types.TypeDecorator,
types.TypeEngine,
types.Variant,
):
continue
if typ in seen:
continue
seen.add(typ)
yield typ
for dialect in _all_dialect_modules():
for typ in _types_for_mod(dialect):
if typ in seen:
continue
seen.add(typ)
yield typ
示例6: _adaptions
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import TypeDecorator [as 别名]
def _adaptions():
for typ in _all_types(omit_special_types=True):
# up adapt from LowerCase to UPPERCASE,
# as well as to all non-sqltypes
up_adaptions = [typ] + typ.__subclasses__()
yield "%s.%s" % (
typ.__module__,
typ.__name__,
), False, typ, up_adaptions
for subcl in typ.__subclasses__():
if (
subcl is not typ
and typ is not TypeDecorator
and "sqlalchemy" in subcl.__module__
):
yield "%s.%s" % (
subcl.__module__,
subcl.__name__,
), True, subcl, [typ]
示例7: test_user_defined_typedec_impl
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import TypeDecorator [as 别名]
def test_user_defined_typedec_impl(self):
class MyType(types.TypeDecorator):
impl = Float
def load_dialect_impl(self, dialect):
if dialect.name == "sqlite":
return String(50)
else:
return super(MyType, self).load_dialect_impl(dialect)
sl = dialects.sqlite.dialect()
pg = dialects.postgresql.dialect()
t = MyType()
self.assert_compile(t, "VARCHAR(50)", dialect=sl)
self.assert_compile(t, "FLOAT", dialect=pg)
eq_(
t.dialect_impl(dialect=sl).impl.__class__,
String().dialect_impl(dialect=sl).__class__,
)
eq_(
t.dialect_impl(dialect=pg).impl.__class__,
Float().dialect_impl(pg).__class__,
)
示例8: define_tables
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import TypeDecorator [as 别名]
def define_tables(cls, metadata):
class MyString(TypeDecorator):
impl = String
def bind_expression(self, bindvalue):
return func.lower(bindvalue)
def column_expression(self, col):
return func.upper(col)
Table(
"test_table",
metadata,
Column("x", String(50)),
Column("y", MyString(50)),
)
示例9: _test_result_processor
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import TypeDecorator [as 别名]
def _test_result_processor(self, cls, use_cache):
class MyType(TypeDecorator):
impl = String()
def process_result_value(self, value, dialect):
return "HI " + value
with self._proxy_fixture(cls):
with self.engine.connect() as conn:
if use_cache:
cache = {}
conn = conn.execution_options(compiled_cache=cache)
stmt = select([literal("THERE", type_=MyType())])
for i in range(2):
r = conn.execute(stmt)
eq_(r.scalar(), "HI THERE")
示例10: _get_column_type
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import TypeDecorator [as 别名]
def _get_column_type(col: MapperProperty) -> TypeEngine:
""" Get column's SQL type """
if isinstance(col.type, TypeDecorator):
# Type decorators wrap other types, so we have to handle them carefully
return col.type.impl
else:
return col.type
示例11: test_custom_type_compare
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import TypeDecorator [as 别名]
def test_custom_type_compare(self):
class MyType(TypeDecorator):
impl = Integer
def compare_against_backend(self, dialect, conn_type):
return isinstance(conn_type, Integer)
ac = ops.AlterColumnOp("sometable", "somecol")
autogenerate.compare._compare_type(
self.autogen_context,
ac,
None,
"sometable",
"somecol",
Column("somecol", INTEGER()),
Column("somecol", MyType()),
)
assert not ac.has_changes()
ac = ops.AlterColumnOp("sometable", "somecol")
autogenerate.compare._compare_type(
self.autogen_context,
ac,
None,
"sometable",
"somecol",
Column("somecol", String()),
Column("somecol", MyType()),
)
diff = ac.to_diff_tuple()
eq_(diff[0][0:4], ("modify_type", None, "sometable", "somecol"))
示例12: test_exception_event_ad_hoc_context
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import TypeDecorator [as 别名]
def test_exception_event_ad_hoc_context(self):
"""test that handle_error is called with a context in
cases where _handle_dbapi_error() is normally called without
any context.
"""
engine = engines.testing_engine()
listener = Mock(return_value=None)
event.listen(engine, "handle_error", listener)
nope = SomeException("nope")
class MyType(TypeDecorator):
impl = Integer
def process_bind_param(self, value, dialect):
raise nope
with engine.connect() as conn:
assert_raises_message(
tsa.exc.StatementError,
r"\(.*.SomeException\) " r"nope\n\[SQL\: u?SELECT 1 ",
conn.execute,
select([1]).where(column("foo") == literal("bar", MyType())),
)
ctx = listener.mock_calls[0][1][0]
assert ctx.statement.startswith("SELECT 1 ")
is_(ctx.is_disconnect, False)
is_(ctx.original_exception, nope)
示例13: test_limit_preserves_typing_information
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import TypeDecorator [as 别名]
def test_limit_preserves_typing_information(self):
class MyType(TypeDecorator):
impl = Integer
stmt = select([type_coerce(column("x"), MyType).label("foo")]).limit(1)
dialect = oracle.dialect()
compiled = stmt.compile(dialect=dialect)
assert isinstance(compiled._create_result_map()["foo"][-1], MyType)
示例14: define_tables
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import TypeDecorator [as 别名]
def define_tables(cls, metadata):
class ProcValue(TypeDecorator):
impl = cls.ARRAY(Integer, dimensions=2)
def process_bind_param(self, value, dialect):
if value is None:
return None
return [[x + 5 for x in v] for v in value]
def process_result_value(self, value, dialect):
if value is None:
return None
return [[x - 7 for x in v] for v in value]
Table(
"arrtable",
metadata,
Column("id", Integer, primary_key=True),
Column("intarr", cls.ARRAY(Integer)),
Column("strarr", cls.ARRAY(Unicode())),
Column("dimarr", ProcValue),
)
Table(
"dim_arrtable",
metadata,
Column("id", Integer, primary_key=True),
Column("intarr", cls.ARRAY(Integer, dimensions=1)),
Column("strarr", cls.ARRAY(Unicode(), dimensions=1)),
Column("dimarr", ProcValue),
)
示例15: test_type_coerce_preserve_subq
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import TypeDecorator [as 别名]
def test_type_coerce_preserve_subq(self):
class MyType(TypeDecorator):
impl = Integer
stmt = select([type_coerce(column("x"), MyType).label("foo")])
subq = stmt.subquery()
stmt2 = subq.select()
subq2 = stmt2.subquery()
assert isinstance(stmt._raw_columns[0].type, MyType)
assert isinstance(subq.c.foo.type, MyType)
assert isinstance(stmt2.selected_columns.foo.type, MyType)
assert isinstance(subq2.c.foo.type, MyType)