本文整理汇总了Python中sqlalchemy.func方法的典型用法代码示例。如果您正苦于以下问题:Python sqlalchemy.func方法的具体用法?Python sqlalchemy.func怎么用?Python sqlalchemy.func使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy
的用法示例。
在下文中一共展示了sqlalchemy.func方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: postgresql_array_search
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import func [as 别名]
def postgresql_array_search(element, compiler, **kw):
needle, haystack = element.clauses
i = sa.func.generate_subscripts(haystack, 1).alias('i')
c0 = sa.column('i', type_=sa.INTEGER(), _selectable=i)
result = (
sa.func.coalesce(
sa.select([c0])
.where(haystack[c0].op('IS NOT DISTINCT FROM')(needle))
.order_by(c0)
.limit(1)
.as_scalar(),
0,
)
- 1
)
string_result = compiler.process(result, **kw)
return string_result
示例2: _generic_pad
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import func [as 别名]
def _generic_pad(arg, length, pad):
f = sa.func
arg_length = f.length(arg)
pad_length = f.length(pad)
number_of_zero_bytes = (
(length - arg_length - 1 + pad_length) / pad_length + 1
) / 2
return f.substr(
f.replace(
f.replace(
f.substr(f.quote(f.zeroblob(number_of_zero_bytes)), 3), "'", ''
),
'0',
pad,
),
1,
length - f.length(arg),
)
示例3: _variance_reduction
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import func [as 别名]
def _variance_reduction(func_name):
suffix = {'sample': 'samp', 'pop': 'pop'}
def variance_compiler(t, expr):
arg, how, where = expr.op().args
if arg.type().equals(dt.boolean):
arg = arg.cast('int32')
func = getattr(
sa.func, '{}_{}'.format(func_name, suffix.get(how, 'samp'))
)
if where is not None:
arg = where.ifelse(arg, None)
return func(t.translate(arg))
return variance_compiler
示例4: audit_table
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import func [as 别名]
def audit_table(self, table, exclude_columns=None):
args = [table.name]
if exclude_columns:
for column in exclude_columns:
if column not in table.c:
raise ImproperlyConfigured(
"Could not configure versioning. Table '{}'' does "
"not have a column named '{}'.".format(
table.name, column
)
)
args.append(array(exclude_columns))
if self.schema_name is None:
func = sa.func.audit_table
else:
func = getattr(getattr(sa.func, self.schema_name), 'audit_table')
query = sa.select([func(*args)])
if query not in cached_statements:
cached_statements[query] = StatementExecutor(query)
listener = (table, 'after_create', cached_statements[query])
if not sa.event.contains(*listener):
sa.event.listen(*listener)
示例5: set_activity_values
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import func [as 别名]
def set_activity_values(self, session):
dialect = session.bind.engine.dialect
table = self.transaction_cls.__table__
if not isinstance(dialect, PGDialect):
warnings.warn(
'"{0}" is not a PostgreSQL dialect. No versioning data will '
'be saved.'.format(dialect.__class__),
RuntimeWarning
)
return
values = convert_callables(self.get_transaction_values())
if values:
values['native_transaction_id'] = sa.func.txid_current()
values['issued_at'] = sa.text("now() AT TIME ZONE 'UTC'")
stmt = (
insert(table)
.values(**values)
.on_conflict_do_nothing(
constraint='transaction_unique_native_tx_id'
)
)
session.execute(stmt)
示例6: get_alarms
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import func [as 别名]
def get_alarms(self, meter=None, pagination=None, **kwargs):
"""Yields a lists of alarms that match filters."""
pagination = pagination or {}
session = self._engine_facade.get_session()
query = session.query(models.Alarm)
query = apply_filters(query, models.Alarm, **kwargs)
query = self._get_pagination_query(
session, query, pagination, alarm_api_models.Alarm, models.Alarm)
alarms = self._retrieve_alarms(query)
# TODO(cmart): improve this by using sqlalchemy.func factory
if meter is not None:
alarms = filter(lambda row:
row.rule.get('meter_name', None) == meter,
alarms)
return alarms
示例7: bind
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import func [as 别名]
def bind(self, cube):
""" When one column needs to match, use the key. """
if self.measure:
table, column = self.measure.bind(cube)
else:
table, column = cube.fact_table, cube.fact_pk
# apply the SQL aggregation function:
column = getattr(func, self.function)(column)
column = column.label(self.ref)
column.quote = True
return table, column
示例8: tag_show
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import func [as 别名]
def tag_show(context, data_dict):
'''Return the details of a tag and all its datasets.
:param id: the name or id of the tag
:type id: string
:param vocabulary_id: the id or name of the tag vocabulary that the tag is
in - if it is not specified it will assume it is a free tag.
(optional)
:type vocabulary_id: string
:param include_datasets: include a list of the tag's datasets. (Up to a
limit of 1000 - for more flexibility, use package_search - see
:py:func:`package_search` for an example.)
(optional, default: ``False``)
:type include_datasets: bool
:returns: the details of the tag, including a list of all of the tag's
datasets and their details
:rtype: dictionary
'''
model = context['model']
id = _get_or_bust(data_dict, 'id')
include_datasets = asbool(data_dict.get('include_datasets', False))
tag = model.Tag.get(id, vocab_id_or_name=data_dict.get('vocabulary_id'))
context['tag'] = tag
if tag is None:
raise NotFound
_check_access('tag_show', context, data_dict)
return model_dictize.tag_dictize(tag, context,
include_datasets=include_datasets)
示例9: config_option_show
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import func [as 别名]
def config_option_show(context, data_dict):
'''Show the current value of a particular configuration option.
Only returns runtime-editable config options (the ones returned by
:py:func:`~ckan.logic.action.get.config_option_list`), which can be updated with the
:py:func:`~ckan.logic.action.update.config_option_update` action.
:param key: The configuration option key
:type key: string
:returns: The value of the config option from either the system_info table
or ini file.
:rtype: string
:raises: :class:`ckan.logic.ValidationError`: if config option is not in
the schema (whitelisted as editable).
'''
_check_access('config_option_show', context, data_dict)
key = _get_or_bust(data_dict, 'key')
schema = ckan.logic.schema.update_configuration_schema()
# Only return whitelisted keys
if key not in schema:
raise ValidationError(
'Configuration option \'{0}\' can not be shown'.format(key))
# return the value from config
return config.get(key, None)
示例10: config_option_list
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import func [as 别名]
def config_option_list(context, data_dict):
'''Return a list of runtime-editable config options keys that can be
updated with :py:func:`~ckan.logic.action.update.config_option_update`.
:returns: A list of config option keys.
:rtype: list
'''
_check_access('config_option_list', context, data_dict)
schema = ckan.logic.schema.update_configuration_schema()
return schema.keys()
示例11: _substr
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import func [as 别名]
def _substr(t, expr):
f = sa.func.substr
arg, start, length = expr.op().args
sa_arg = t.translate(arg)
sa_start = t.translate(start)
if length is None:
return f(sa_arg, sa_start + 1)
else:
sa_length = t.translate(length)
return f(sa_arg, sa_start + 1, sa_length)
示例12: _string_find
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import func [as 别名]
def _string_find(t, expr):
arg, substr, start, _ = expr.op().args
if start is not None:
raise NotImplementedError
sa_arg = t.translate(arg)
sa_substr = t.translate(substr)
return sa.func.strpos(sa_arg, sa_substr) - 1
示例13: _second
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import func [as 别名]
def _second(t, expr):
# extracting the second gives us the fractional part as well, so smash that
# with a cast to SMALLINT
(sa_arg,) = map(t.translate, expr.op().args)
return sa.cast(sa.func.FLOOR(sa.extract('second', sa_arg)), sa.SMALLINT)
示例14: _millisecond
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import func [as 别名]
def _millisecond(t, expr):
# we get total number of milliseconds including seconds with extract so we
# mod 1000
(sa_arg,) = map(t.translate, expr.op().args)
return (
sa.cast(sa.func.floor(sa.extract('millisecond', sa_arg)), sa.SMALLINT)
% 1000
)
示例15: _timestamp_truncate
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import func [as 别名]
def _timestamp_truncate(t, expr):
arg, unit = expr.op().args
sa_arg = t.translate(arg)
try:
precision = _truncate_precisions[unit]
except KeyError:
raise com.UnsupportedOperationError(
'Unsupported truncate unit {!r}'.format(unit)
)
return sa.func.date_trunc(precision, sa_arg)