本文整理匯總了Python中sqlalchemy.sql.expression.asc方法的典型用法代碼示例。如果您正苦於以下問題:Python expression.asc方法的具體用法?Python expression.asc怎麽用?Python expression.asc使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sqlalchemy.sql.expression
的用法示例。
在下文中一共展示了expression.asc方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: apply
# 需要導入模塊: from sqlalchemy.sql import expression [as 別名]
# 或者: from sqlalchemy.sql.expression import asc [as 別名]
def apply(self, q, bindings, ordering, distinct=None):
""" Sort on a set of field specifications of the type (ref, direction)
in order of the submitted list. """
info = []
for (ref, direction) in self.parse(ordering):
info.append((ref, direction))
table, column = self.cube.model[ref].bind(self.cube)
if distinct is not None and distinct != ref:
column = asc(ref) if direction == 'asc' else desc(ref)
else:
column = column.label(column.name)
column = column.asc() if direction == 'asc' else column.desc()
bindings.append(Binding(table, ref))
if self.cube.is_postgresql:
column = column.nullslast()
q = q.order_by(column)
if not len(self.results):
for column in q.columns:
column = column.asc()
if self.cube.is_postgresql:
column = column.nullslast()
q = q.order_by(column)
return info, q, bindings
示例2: status_detect
# 需要導入模塊: from sqlalchemy.sql import expression [as 別名]
# 或者: from sqlalchemy.sql.expression import asc [as 別名]
def status_detect(num_days, num_segments):
repos = Repo.query.filter(Repo.status == 'unknown')
for repo in repos:
result = db.session.query(RepoStars.day, RepoStars.stars)\
.filter(RepoStars.repo_id == repo.id)\
.order_by(expression.asc(RepoStars.day))\
.limit(num_days)\
.all()
val = 0 if not result else repo_mean(
result, num_days, num_segments, last_known_mean(repo.id)
)
status_old = repo.status
repo.status = 'hopeless' if val < 1 else 'promising'
log.info(
'Repository status of {0}({1}) has been changed to {2} (was: {3})'
.format(repo.id, repo.full_name, repo.status, status_old)
)
db.session.merge(
RepoMean(repo=repo, value=val, created_at=datetime.today().strftime('%Y-%m-%d'))
)
db.session.commit()
示例3: order
# 需要導入模塊: from sqlalchemy.sql import expression [as 別名]
# 或者: from sqlalchemy.sql.expression import asc [as 別名]
def order(self, ast):
if isinstance(ast, six.string_types):
ref, direction = ast, 'asc'
else:
ref, direction = ast[0], ast[2]
if ref not in self.cube.model:
raise QueryException('Invalid sorting criterion: %r' % ast)
self.results.append((ref, direction))
示例4: get_pk_constraint
# 需要導入模塊: from sqlalchemy.sql import expression [as 別名]
# 或者: from sqlalchemy.sql.expression import asc [as 別名]
def get_pk_constraint(self, connection, table_name, schema=None, **kw):
"""
Override
TODO: Check if we need PRIMARY Indices or PRIMARY KEY Indices
TODO: Check for border cases (No PK Indices)
"""
if schema is None:
schema = self.default_schema_name
stmt = select([column('ColumnName'), column('IndexName')],
from_obj=[text('dbc.Indices')]).where(
and_(text('DatabaseName = :schema'),
text('TableName=:table'),
text('IndexType=:indextype'))
).order_by(asc(column('IndexNumber')))
# K for Primary Key
res = connection.execute(stmt, schema=schema, table=table_name, indextype='K').fetchall()
index_columns = list()
index_name = None
for index_column in res:
index_columns.append(self.normalize_name(index_column['ColumnName']))
index_name = self.normalize_name(index_column['IndexName']) # There should be just one IndexName
return {
"constrained_columns": index_columns,
"name": index_name
}
示例5: get_unique_constraints
# 需要導入模塊: from sqlalchemy.sql import expression [as 別名]
# 或者: from sqlalchemy.sql.expression import asc [as 別名]
def get_unique_constraints(self, connection, table_name, schema=None, **kw):
"""
Overrides base class method
"""
if schema is None:
schema = self.default_schema_name
stmt = select([column('ColumnName'), column('IndexName')], from_obj=[text('dbc.Indices')]) \
.where(and_(text('DatabaseName = :schema'),
text('TableName=:table'),
text('IndexType=:indextype'))) \
.order_by(asc(column('IndexName')))
# U for Unique
res = connection.execute(stmt, schema=schema, table=table_name, indextype='U').fetchall()
def grouper(fk_row):
return {
'name': self.normalize_name(fk_row['IndexName']),
}
unique_constraints = list()
for constraint_info, constraint_cols in groupby(res, grouper):
unique_constraint = {
'name': self.normalize_name(constraint_info['name']),
'column_names': list()
}
for constraint_col in constraint_cols:
unique_constraint['column_names'].append(self.normalize_name(constraint_col['ColumnName']))
unique_constraints.append(unique_constraint)
return unique_constraints
示例6: get_indexes
# 需要導入模塊: from sqlalchemy.sql import expression [as 別名]
# 或者: from sqlalchemy.sql.expression import asc [as 別名]
def get_indexes(self, connection, table_name, schema=None, **kw):
"""
Overrides base class method
"""
if schema is None:
schema = self.default_schema_name
stmt = select(["*"], from_obj=[text('dbc.Indices')]) \
.where(and_(text('DatabaseName = :schema'),
text('TableName=:table'))) \
.order_by(asc(column('IndexName')))
res = connection.execute(stmt, schema=schema, table=table_name).fetchall()
def grouper(fk_row):
return {
'name': fk_row.IndexName or fk_row.IndexNumber, # If IndexName is None TODO: Check what to do
'unique': True if fk_row.UniqueFlag == 'Y' else False
}
# TODO: Check if there's a better way
indices = list()
for index_info, index_cols in groupby(res, grouper):
index_dict = {
'name': index_info['name'],
'column_names': list(),
'unique': index_info['unique']
}
for index_col in index_cols:
index_dict['column_names'].append(self.normalize_name(index_col['ColumnName']))
indices.append(index_dict)
return indices
示例7: __query__
# 需要導入模塊: from sqlalchemy.sql import expression [as 別名]
# 或者: from sqlalchemy.sql.expression import asc [as 別名]
def __query__(self):
if self.order == self.DESCENDANT:
query = desc(self.attribute)
elif self.order == self.ASCENDANT:
query = asc(self.attribute)
return query
示例8: release_waiting_requests_fifo
# 需要導入模塊: from sqlalchemy.sql import expression [as 別名]
# 或者: from sqlalchemy.sql.expression import asc [as 別名]
def release_waiting_requests_fifo(rse_id, activity=None, count=None, account=None, direction='destination', session=None):
"""
Release waiting requests. Transfer requests that were requested first, get released first (FIFO).
:param rse_id: The RSE id.
:param activity: The activity.
:param count: The count to be released.
:param account: The account name whose requests to release.
:param direction: Direction if requests are grouped by source RSE or destination RSE.
:param session: The database session.
"""
dialect = session.bind.dialect.name
rowcount = 0
if dialect == 'mysql':
subquery = session.query(models.Request.id)\
.filter(models.Request.state == RequestState.WAITING)\
.order_by(asc(models.Request.requested_at))
if direction == 'destination':
subquery = subquery.filter(models.Request.dest_rse_id == rse_id)
elif direction == 'source':
subquery = subquery.filter(models.Request.source_rse_id == rse_id)
if activity:
subquery = subquery.filter(models.Request.activity == activity)
if account:
subquery = subquery.filter(models.Request.account == account)
subquery = subquery.limit(count).subquery()
# join because IN and LIMIT cannot be used together
subquery = session.query(models.Request.id)\
.join(subquery, models.Request.id == subquery.c.id).subquery()
# wrap select to update and select from the same table
subquery = session.query(subquery.c.id).subquery()
rowcount = session.query(models.Request)\
.filter(models.Request.id.in_(subquery))\
.update({'state': RequestState.QUEUED},
synchronize_session=False)
else:
subquery = session.query(models.Request.id)\
.filter(models.Request.state == RequestState.WAITING)
if direction == 'destination':
subquery = subquery.filter(models.Request.dest_rse_id == rse_id)
elif direction == 'source':
subquery = subquery.filter(models.Request.source_rse_id == rse_id)
if activity:
subquery = subquery.filter(models.Request.activity == activity)
if account:
subquery = subquery.filter(models.Request.account == account)
subquery = subquery.order_by(asc(models.Request.requested_at))\
.limit(count)
rowcount = session.query(models.Request)\
.filter(models.Request.id.in_(subquery))\
.update({'state': RequestState.QUEUED},
synchronize_session=False)
return rowcount
示例9: get_foreign_keys
# 需要導入模塊: from sqlalchemy.sql import expression [as 別名]
# 或者: from sqlalchemy.sql.expression import asc [as 別名]
def get_foreign_keys(self, connection, table_name, schema=None, **kw):
"""
Overrides base class method
"""
if schema is None:
schema = self.default_schema_name
stmt = select([column('IndexID'), column('IndexName'), column('ChildKeyColumn'), column('ParentDB'),
column('ParentTable'), column('ParentKeyColumn')],
from_obj=[text('DBC.All_RI_ChildrenV')]) \
.where(and_(text('ChildTable = :table'),
text('ChildDB = :schema'))) \
.order_by(asc(column('IndexID')))
res = connection.execute(stmt, schema=schema, table=table_name).fetchall()
def grouper(fk_row):
return {
'name': fk_row.IndexName or fk_row.IndexID, #ID if IndexName is None
'schema': fk_row.ParentDB,
'table': fk_row.ParentTable
}
# TODO: Check if there's a better way
fk_dicts = list()
for constraint_info, constraint_cols in groupby(res, grouper):
fk_dict = {
'name': constraint_info['name'],
'constrained_columns': list(),
'referred_table': constraint_info['table'],
'referred_schema': constraint_info['schema'],
'referred_columns': list()
}
for constraint_col in constraint_cols:
fk_dict['constrained_columns'].append(self.normalize_name(constraint_col['ChildKeyColumn']))
fk_dict['referred_columns'].append(self.normalize_name(constraint_col['ParentKeyColumn']))
fk_dicts.append(fk_dict)
return fk_dicts