本文整理汇总了Python中sqlalchemy.sql.expression.ClauseElement方法的典型用法代码示例。如果您正苦于以下问题:Python expression.ClauseElement方法的具体用法?Python expression.ClauseElement怎么用?Python expression.ClauseElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.sql.expression
的用法示例。
在下文中一共展示了expression.ClauseElement方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _make_simple_select
# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import ClauseElement [as 别名]
def _make_simple_select(cls: Type[T], *args: ClauseElement) -> Select:
"""
Create a simple ``SELECT * FROM table WHERE <args>`` statement.
Args:
*args: The WHERE clauses. If there are many elements, they're joined with AND.
Returns:
The SQLAlchemy SELECT statement object.
"""
if len(args) > 1:
return cls.t.select().where(and_(*args))
elif len(args) == 1:
return cls.t.select().where(args[0])
else:
return cls.t.select()
示例2: test_query_return_type
# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import ClauseElement [as 别名]
def test_query_return_type(self, spelldb, sample_points):
"""Test if query() returns the correct type"""
core = spelldb.spell.get_core(spelldb.dburl)
engine = core.get_engine()
source_table, target_table = core.get_tables(
source_uri=spelldb.spell.source_table,
target=sample_points,
engine=engine,
)
# Perform the test
query = spelldb.spell.query(
source=source_table,
target=target_table,
core=core,
column="WKT",
pkey="__index_level_0__",
)
assert isinstance(query, ClauseElement)
示例3: test_callout_to_compiler
# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import ClauseElement [as 别名]
def test_callout_to_compiler(self):
class InsertFromSelect(ClauseElement):
def __init__(self, table, select):
self.table = table
self.select = select
@compiles(InsertFromSelect)
def visit_insert_from_select(element, compiler, **kw):
return "INSERT INTO %s (%s)" % (
compiler.process(element.table, asfrom=True),
compiler.process(element.select),
)
t1 = table("mytable", column("x"), column("y"), column("z"))
self.assert_compile(
InsertFromSelect(t1, select([t1]).where(t1.c.x > 5)),
"INSERT INTO mytable (SELECT mytable.x, mytable.y, mytable.z "
"FROM mytable WHERE mytable.x > :x_1)",
)
示例4: compiles
# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import ClauseElement [as 别名]
def compiles(class_, *specs):
"""Register a function as a compiler for a
given :class:`.ClauseElement` type."""
def decorate(fn):
existing = class_.__dict__.get('_compiler_dispatcher', None)
existing_dispatch = class_.__dict__.get('_compiler_dispatch')
if not existing:
existing = _dispatcher()
if existing_dispatch:
existing.specs['default'] = existing_dispatch
# TODO: why is the lambda needed ?
setattr(class_, '_compiler_dispatch',
lambda *arg, **kw: existing(*arg, **kw))
setattr(class_, '_compiler_dispatcher', existing)
if specs:
for s in specs:
existing.specs[s] = fn
else:
existing.specs['default'] = fn
return fn
return decorate
示例5: deregister
# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import ClauseElement [as 别名]
def deregister(class_):
"""Remove all custom compilers associated with a given
:class:`.ClauseElement` type."""
if hasattr(class_, '_compiler_dispatcher'):
# regenerate default _compiler_dispatch
visitors._generate_dispatch(class_)
# remove custom directive
del class_._compiler_dispatcher
示例6: db_create
# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import ClauseElement [as 别名]
def db_create(self, model, defaults=None, **kwargs):
params = dict((k, v) for k, v in kwargs.items() if not isinstance(v, ClauseElement))
params.update(defaults or {})
instance = model(**params)
self.session.add(instance)
self.session.flush()
return instance
示例7: _select_all
# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import ClauseElement [as 别名]
def _select_all(cls: Type[T], *args: ClauseElement) -> Iterator[T]:
"""
Select all rows with given conditions. This is intended to be used by table-specific
select methods.
Args:
*args: The WHERE clauses. If there are many elements, they're joined with AND.
Yields:
The objects representing the rows read with :meth:`scan`
"""
yield from cls._all(cls.db.execute(cls._make_simple_select(*args)))
示例8: _select_one_or_none
# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import ClauseElement [as 别名]
def _select_one_or_none(cls: Type[T], *args: ClauseElement) -> T:
"""
Select one row with given conditions. If no row is found, return ``None``. This is intended
to be used by table-specific select methods.
Args:
*args: The WHERE clauses. If there are many elements, they're joined with AND.
Returns:
The object representing the matched row read with :meth:`scan`, or ``None`` if no rows
matched.
"""
return cls._one_or_none(cls.db.execute(cls._make_simple_select(*args)))
示例9: _constraint_to_clause
# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import ClauseElement [as 别名]
def _constraint_to_clause(self, constraint: Constraint) -> ClauseElement:
return and_(*[column == self.__dict__[name]
for name, column in constraint.columns.items()])
示例10: _edit_identity
# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import ClauseElement [as 别名]
def _edit_identity(self: T) -> ClauseElement:
"""The SQLAlchemy WHERE clause used for editing and deleting individual rows.
Usually AND of primary keys."""
return self._constraint_to_clause(self.t.primary_key)
示例11: compiles
# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import ClauseElement [as 别名]
def compiles(class_, *specs):
"""Register a function as a compiler for a
given :class:`.ClauseElement` type."""
def decorate(fn):
# get an existing @compiles handler
existing = class_.__dict__.get('_compiler_dispatcher', None)
# get the original handler. All ClauseElement classes have one
# of these, but some TypeEngine classes will not.
existing_dispatch = getattr(class_, '_compiler_dispatch', None)
if not existing:
existing = _dispatcher()
if existing_dispatch:
def _wrap_existing_dispatch(element, compiler, **kw):
try:
return existing_dispatch(element, compiler, **kw)
except exc.UnsupportedCompilationError:
raise exc.CompileError(
"%s construct has no default "
"compilation handler." % type(element))
existing.specs['default'] = _wrap_existing_dispatch
# TODO: why is the lambda needed ?
setattr(class_, '_compiler_dispatch',
lambda *arg, **kw: existing(*arg, **kw))
setattr(class_, '_compiler_dispatcher', existing)
if specs:
for s in specs:
existing.specs[s] = fn
else:
existing.specs['default'] = fn
return fn
return decorate
示例12: __init__
# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import ClauseElement [as 别名]
def __init__(self, name):
"""
Builds the Executable/ClauseElement that represents the refresh command
Parameters
----------
name: str, required
The name of the view to refresh
"""
self.name = name
示例13: get_or_create
# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import ClauseElement [as 别名]
def get_or_create(self, model, **kwargs):
""" Simple helper to either get an existing record if it exists otherwise create and return a new instance """
instance = self.session.query(model).filter_by(**kwargs).first()
if instance:
return instance
else:
params = dict((k, v) for k, v in kwargs.items() if not isinstance(v, ClauseElement))
instance = model(**params)
return instance
示例14: deregister
# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import ClauseElement [as 别名]
def deregister(class_):
"""Remove all custom compilers associated with a given
:class:`_expression.ClauseElement` type.
"""
if hasattr(class_, "_compiler_dispatcher"):
class_._compiler_dispatch = class_._original_compiler_dispatch
del class_._compiler_dispatcher
示例15: test_unsupported_element_str_visit_name
# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import ClauseElement [as 别名]
def test_unsupported_element_str_visit_name(self):
from sqlalchemy.sql.expression import ClauseElement
class SomeElement(ClauseElement):
__visit_name__ = "some_element"
assert_raises_message(
exc.UnsupportedCompilationError,
r"Compiler <sqlalchemy.sql.compiler.StrSQLCompiler .*"
r"can't render element of type <class '.*SomeElement'>",
SomeElement().compile,
)