本文整理汇总了Python中sqlalchemy.exc.StatementError方法的典型用法代码示例。如果您正苦于以下问题:Python exc.StatementError方法的具体用法?Python exc.StatementError怎么用?Python exc.StatementError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.exc
的用法示例。
在下文中一共展示了exc.StatementError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_subscription_by_id
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import StatementError [as 别名]
def get_subscription_by_id(subscription_id, session=None):
"""
Get a specific subscription by id.
:param subscription_id: The subscription_id to select.
:param session: The database session in use.
:raises: SubscriptionNotFound if no Subscription can be found.
"""
try:
subscription = session.query(models.Subscription).filter_by(id=subscription_id).one()
result = {}
for column in subscription.__table__.columns:
result[column.name] = getattr(subscription, column.name)
return result
except NoResultFound:
raise SubscriptionNotFound('No subscription with the id %s found' % (subscription_id))
except StatementError:
raise RucioException('Badly formatted subscription id (%s)' % (subscription_id))
示例2: list_rule_history
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import StatementError [as 别名]
def list_rule_history(rule_id, session=None):
"""
List the rule history of a rule.
:param rule_id: The id of the rule.
:param session: The database session in use.
:raises: RucioException
"""
query = session.query(models.ReplicationRuleHistoryRecent.updated_at,
models.ReplicationRuleHistoryRecent.state,
models.ReplicationRuleHistoryRecent.locks_ok_cnt,
models.ReplicationRuleHistoryRecent.locks_stuck_cnt,
models.ReplicationRuleHistoryRecent.locks_replicating_cnt).filter_by(id=rule_id).order_by(models.ReplicationRuleHistoryRecent.updated_at)
try:
for rule in query.yield_per(5):
yield {'updated_at': rule[0], 'state': rule[1], 'locks_ok_cnt': rule[2], 'locks_stuck_cnt': rule[3], 'locks_replicating_cnt': rule[4]}
except StatementError:
raise RucioException('Badly formatted input (IDs?)')
示例3: list_associated_rules_for_file
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import StatementError [as 别名]
def list_associated_rules_for_file(scope, name, session=None):
"""
List replication rules a file is affected from.
:param scope: Scope of the file.
:param name: Name of the file.
:param session: The database session in use.
:raises: RucioException
"""
rucio.core.did.get_did(scope=scope, name=name, session=session) # Check if the did acually exists
query = session.query(models.ReplicationRule).\
with_hint(models.ReplicaLock, "INDEX(LOCKS LOCKS_PK)", 'oracle').\
join(models.ReplicaLock, models.ReplicationRule.id == models.ReplicaLock.rule_id).\
filter(models.ReplicaLock.scope == scope, models.ReplicaLock.name == name).distinct()
try:
for rule in query.yield_per(5):
d = {}
for column in rule.__table__.columns:
d[column.name] = getattr(rule, column.name)
yield d
except StatementError:
raise RucioException('Badly formatted input (IDs?)')
示例4: get
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import StatementError [as 别名]
def get(self, instance_id):
"""
Retrieve a model corresponding at given ID and return it as a JSON
object.
"""
try:
instance = self.get_model_or_404(instance_id)
result = self.serialize_instance(instance)
self.check_read_permissions(result)
result = self.clean_get_result(result)
except StatementError as exception:
current_app.logger.error(str(exception), exc_info=1)
return {"message": str(exception)}, 400
except ValueError:
abort(404)
return result, 200
示例5: delete
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import StatementError [as 别名]
def delete(self, instance_id):
"""
Delete a model corresponding at given ID and return it as a JSON
object.
"""
instance = self.get_model_or_404(instance_id)
try:
instance_dict = instance.serialize()
self.check_delete_permissions(instance_dict)
self.pre_delete(instance_dict)
instance.delete()
self.emit_delete_event(instance_dict)
self.post_delete(instance_dict)
except IntegrityError as exception:
current_app.logger.error(str(exception), exc_info=1)
return {"message": str(exception)}, 400
except StatementError as exception:
current_app.logger.error(str(exception), exc_info=1)
return {"message": str(exception)}, 400
return "", 204
示例6: get_sequence_raw
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import StatementError [as 别名]
def get_sequence_raw(sequence_id):
"""
Return given sequence as an active record.
"""
sequence_type = get_sequence_type()
try:
sequence = Entity.get_by(
entity_type_id=sequence_type["id"], id=sequence_id
)
except StatementError:
raise SequenceNotFoundException
if sequence is None:
raise SequenceNotFoundException
return sequence
示例7: get_episode_raw
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import StatementError [as 别名]
def get_episode_raw(episode_id):
"""
Return given episode as an active record.
"""
episode_type = get_episode_type()
if episode_type is None:
episode_type = get_episode_type()
try:
episode = Entity.get_by(
entity_type_id=episode_type["id"], id=episode_id
)
except StatementError:
raise EpisodeNotFoundException
if episode is None:
raise EpisodeNotFoundException
return episode
示例8: get_instance
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import StatementError [as 别名]
def get_instance(model, instance_id, exception):
"""
Get instance of any model from its ID and raise given exception if not
found.
"""
if instance_id is None:
raise exception()
try:
instance = model.get(instance_id)
except StatementError:
raise exception()
if instance is None:
raise exception()
return instance
示例9: check_marker
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import StatementError [as 别名]
def check_marker(table, marker, session):
marker_query = select([table]).where(table.c.id == marker)
try:
marker_resultproxy = session.execute(marker_query)
marker = marker_resultproxy.fetchone()
if marker is None:
raise exceptions.MarkerNotFound(
'Marker %s could not be found' % marker)
except oslo_db_exception.DBError as e:
# Malformed UUIDs return StatementError wrapped in a
# DBError
if isinstance(e.inner_exception,
sqlalchemy_exc.StatementError):
raise exceptions.InvalidMarker()
else:
raise
return marker
示例10: test_process_bind_param_naive
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import StatementError [as 别名]
def test_process_bind_param_naive(self):
"""
Check if naive datetimes are prevented from saving to the db
"""
dag_id = 'test_process_bind_param_naive'
# naive
start_date = datetime.datetime.now()
dag = DAG(dag_id=dag_id, start_date=start_date)
dag.clear()
with self.assertRaises((ValueError, StatementError)):
dag.create_dagrun(
run_id=start_date.isoformat,
state=State.NONE,
execution_date=start_date,
start_date=start_date,
session=self.session
)
dag.clear()
示例11: test_raises_error_if_cannot_reconnect
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import StatementError [as 别名]
def test_raises_error_if_cannot_reconnect(
toxiproxy_db_session, disconnect, toxiproxy
):
if not toxiproxy:
pytest.skip('Toxiproxy not installed')
toxiproxy_db_session.add(ExampleModel(data='hello1'))
toxiproxy_db_session.add(ExampleModel(data='hello2'))
toxiproxy_db_session.commit()
disconnect(reconnect=False)
@transaction_retry
def get_model_count():
return toxiproxy_db_session.query(ExampleModel).count()
with pytest.raises(StatementError):
get_model_count()
示例12: sqlite_regex_match
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import StatementError [as 别名]
def sqlite_regex_match(element, compiler, **kw):
"""Compile the SQL expression representing a regular expression match
for the SQLite engine.
"""
# determine the name of a custom SQLite function to use for the operator
operator = element.operator.opstring
try:
func_name, _ = SQLITE_REGEX_FUNCTIONS[operator]
except (KeyError, ValueError) as e:
would_be_sql_string = ' '.join((compiler.process(element.left),
operator,
compiler.process(element.right)))
raise exc.StatementError(
"unknown regular expression match operator: %s" % operator,
would_be_sql_string, None, e)
# compile the expression as an invocation of the custom function
regex_func = getattr(func, func_name)
regex_func_call = regex_func(element.left, element.right)
return compiler.process(regex_func_call)
示例13: test_statement_error_w_code
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import StatementError [as 别名]
def test_statement_error_w_code(self):
try:
raise sa_exceptions.DBAPIError.instance(
"select * from table",
[{"x": 1}],
sa_exceptions.InvalidRequestError("hello", code="abcd"),
DatabaseError,
)
except sa_exceptions.StatementError as err:
eq_(
str(err),
"(sqlalchemy.exc.InvalidRequestError) hello\n"
"[SQL: select * from table]\n"
"[parameters: [{'x': 1}]]\n"
"(Background on this error at: http://sqlalche.me/e/%s/abcd)"
% sa_exceptions._version_token,
)
eq_(err.args, ("(sqlalchemy.exc.InvalidRequestError) hello",))
示例14: _assert_raises
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import StatementError [as 别名]
def _assert_raises(self, stmt, params):
assert_raises_message(
exc.StatementError,
"A value is required for bind parameter 'x'",
testing.db.execute,
stmt,
**params
)
assert_raises_message(
exc.StatementError,
"A value is required for bind parameter 'x'",
testing.db.execute,
stmt,
params,
)
示例15: test_insert_heterogeneous_params
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import StatementError [as 别名]
def test_insert_heterogeneous_params(self):
"""test that executemany parameters are asserted to match the
parameter set of the first."""
users = self.tables.users
assert_raises_message(
exc.StatementError,
r"\(sqlalchemy.exc.InvalidRequestError\) A value is required for "
"bind parameter 'user_name', in "
"parameter group 2\n"
r"\[SQL: u?INSERT INTO users",
users.insert().execute,
{"user_id": 7, "user_name": "jack"},
{"user_id": 8, "user_name": "ed"},
{"user_id": 9},
)
# this succeeds however. We aren't yet doing
# a length check on all subsequent parameters.
users.insert().execute(
{"user_id": 7}, {"user_id": 8, "user_name": "ed"}, {"user_id": 9}
)