本文整理汇总了Python中sqlalchemy.sql.ClauseElement方法的典型用法代码示例。如果您正苦于以下问题:Python sql.ClauseElement方法的具体用法?Python sql.ClauseElement怎么用?Python sql.ClauseElement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.sql
的用法示例。
在下文中一共展示了sql.ClauseElement方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _render_potential_expr
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import ClauseElement [as 别名]
def _render_potential_expr(value, autogen_context, wrap_in_text=True):
if isinstance(value, sql.ClauseElement):
if compat.sqla_08:
compile_kw = dict(compile_kwargs={
'literal_binds': True, "include_table": False})
else:
compile_kw = {}
if wrap_in_text:
template = "%(prefix)stext(%(sql)r)"
else:
template = "%(sql)r"
return template % {
"prefix": _sqlalchemy_autogenerate_prefix(autogen_context),
"sql": compat.text_type(
value.compile(dialect=autogen_context.dialect,
**compile_kw)
)
}
else:
return repr(value)
示例2: _render_potential_expr
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import ClauseElement [as 别名]
def _render_potential_expr(
value, autogen_context, wrap_in_text=True, is_server_default=False
):
if isinstance(value, sql.ClauseElement):
if wrap_in_text:
template = "%(prefix)stext(%(sql)r)"
else:
template = "%(sql)r"
return template % {
"prefix": _sqlalchemy_autogenerate_prefix(autogen_context),
"sql": autogen_context.migration_context.impl.render_ddl_sql_expr(
value, is_server_default=is_server_default
),
}
else:
return repr(value)
示例3: apply_subset_if_needed
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import ClauseElement [as 别名]
def apply_subset_if_needed(self, sql, *, subscriber_identifier):
"""
Return a modified version of the input SQL query which has the subset applied.
Parameters
----------
sql : sqlalchemy.sql.ClauseElement
The SQL query to which the subset should be applied.
subscriber_identifier : str
The column in the parent table which contains the subscriber information.
Returns
----------
sqlalchemy.sql.ClauseElement
"""
assert isinstance(sql, ClauseElement)
assert len(sql.froms) == 1
parent_table = sql.froms[0]
return sql.where(parent_table.c[subscriber_identifier].in_(self.subscribers))
示例4: _render_potential_expr
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import ClauseElement [as 别名]
def _render_potential_expr(value, autogen_context, wrap_in_text=True):
if isinstance(value, sql.ClauseElement):
compile_kw = dict(compile_kwargs={
'literal_binds': True, "include_table": False})
if wrap_in_text:
template = "%(prefix)stext(%(sql)r)"
else:
template = "%(sql)r"
return template % {
"prefix": _sqlalchemy_autogenerate_prefix(autogen_context),
"sql": compat.text_type(
value.compile(dialect=autogen_context.dialect,
**compile_kw)
)
}
else:
return repr(value)
示例5: get_or_create
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import ClauseElement [as 别名]
def get_or_create(session, model, defaults=None, **kwargs):
instance = session.query(model).filter_by(**kwargs).first()
if instance:
return instance, False
else:
params = dict((k, v) for k, v in kwargs.items() if not isinstance(v, ClauseElement))
params.update(defaults or {})
instance = model(**params)
session.add(instance)
session.commit()
return instance, True
示例6: test_no_clause_element_on_clauseelement
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import ClauseElement [as 别名]
def test_no_clause_element_on_clauseelement(self):
# re [ticket:3802], there are in the wild examples
# of looping over __clause_element__, therefore the
# absence of __clause_element__ as a test for "this is the clause
# element" must be maintained
class Foo(ClauseElement):
pass
assert not hasattr(Foo(), "__clause_element__")
示例7: compile_query
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import ClauseElement [as 别名]
def compile_query(query, dialect=_dialect, inline=False):
if isinstance(query, str):
query_logger.debug(query)
return query, ()
elif isinstance(query, DDLElement):
compiled = query.compile(dialect=dialect)
new_query = compiled.string
query_logger.debug(new_query)
return new_query, ()
elif isinstance(query, ClauseElement):
query = execute_defaults(query) # default values for Insert/Update
compiled = query.compile(dialect=dialect)
compiled_params = sorted(compiled.params.items())
mapping = {key: '$' + str(i)
for i, (key, _) in enumerate(compiled_params, start=1)}
new_query = compiled.string % mapping
processors = compiled._bind_processors
new_params = [processors[key](val) if key in processors else val
for key, val in compiled_params]
query_logger.debug(new_query)
if inline:
return new_query
return new_query, new_params
示例8: _executemany
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import ClauseElement [as 别名]
def _executemany(self, query, dps, cursor):
"""
executemany
"""
result_map = None
if isinstance(query, str):
await cursor.executemany(query, dps)
elif isinstance(query, DDLElement):
raise exc.ArgumentError(
"Don't mix sqlalchemy DDL clause "
"and execution with parameters"
)
elif isinstance(query, ClauseElement):
compiled = query.compile(dialect=self._dialect)
params = []
is_update = isinstance(query, UpdateBase)
for dp in dps:
params.append(
self._base_params(
query,
dp,
compiled,
is_update,
)
)
await cursor.executemany(str(compiled), params)
result_map = compiled._result_columns
else:
raise exc.ArgumentError(
"sql statement should be str or "
"SQLAlchemy data "
"selection/modification clause"
)
ret = await create_result_proxy(
self,
cursor,
self._dialect,
result_map
)
self._weak_results.add(ret)
return ret
示例9: _execute
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import ClauseElement [as 别名]
def _execute(self, query, *multiparams, **params):
cursor = await self._get_cursor()
dp = _distill_params(multiparams, params)
if len(dp) > 1:
raise exc.ArgumentError("aiopg doesn't support executemany")
elif dp:
dp = dp[0]
result_map = None
if isinstance(query, str):
await cursor.execute(query, dp)
elif isinstance(query, ClauseElement):
compiled = query.compile(dialect=self._dialect)
# parameters = compiled.params
if not isinstance(query, DDLElement):
if dp and isinstance(dp, (list, tuple)):
if isinstance(query, UpdateBase):
dp = {c.key: pval
for c, pval in zip(query.table.c, dp)}
else:
raise exc.ArgumentError("Don't mix sqlalchemy SELECT "
"clause with positional "
"parameters")
compiled_parameters = [compiled.construct_params(dp)]
processed_parameters = []
processors = compiled._bind_processors
for compiled_params in compiled_parameters:
params = {key: (processors[key](compiled_params[key])
if key in processors
else compiled_params[key])
for key in compiled_params}
processed_parameters.append(params)
post_processed_params = self._dialect.execute_sequence_format(
processed_parameters)
# _result_columns is a private API of Compiled,
# but I couldn't find any public API exposing this data.
result_map = compiled._result_columns
else:
if dp:
raise exc.ArgumentError("Don't mix sqlalchemy DDL clause "
"and execution with parameters")
post_processed_params = [compiled.construct_params()]
result_map = None
await cursor.execute(str(compiled), post_processed_params[0])
else:
raise exc.ArgumentError("sql statement should be str or "
"SQLAlchemy data "
"selection/modification clause")
return ResultProxy(self, cursor, self._dialect, result_map)
示例10: _execute
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import ClauseElement [as 别名]
def _execute(self, query, *multiparams, **params):
cursor = await self._connection.cursor()
dp = _distill_params(multiparams, params)
if len(dp) > 1:
return await self._executemany(query, dp, cursor)
elif dp:
dp = dp[0]
result_map = None
if isinstance(query, str):
await cursor.execute(query, dp or None)
elif isinstance(query, ClauseElement):
if self._compiled_cache is not None:
key = query
compiled = self._compiled_cache.get(key)
if not compiled:
compiled = query.compile(dialect=self._dialect)
if dp and dp.keys() == compiled.params.keys() \
or not (dp or compiled.params):
# we only want queries with bound params in cache
self._compiled_cache[key] = compiled
else:
compiled = query.compile(dialect=self._dialect)
if not isinstance(query, DDLElement):
post_processed_params = self._base_params(
query,
dp,
compiled,
isinstance(query, UpdateBase)
)
result_map = compiled._result_columns
else:
if dp:
raise exc.ArgumentError("Don't mix sqlalchemy DDL clause "
"and execution with parameters")
post_processed_params = compiled.construct_params()
result_map = None
await cursor.execute(str(compiled), post_processed_params)
else:
raise exc.ArgumentError("sql statement should be str or "
"SQLAlchemy data "
"selection/modification clause")
ret = await create_result_proxy(
self, cursor, self._dialect, result_map
)
self._weak_results.add(ret)
return ret