Python sqlalchemy.exc 模块,StatementError() 实例源码

我们从Python开源项目中,提取了以下30个代码示例,用于说明如何使用sqlalchemy.exc.StatementError()

项目:aardvark    作者:Netflix-Skunkworks    | 项目源码 | 文件源码
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), 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)
项目:rucio    作者:rucio01    | 项目源码 | 文件源码
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?)')
项目:rucio    作者:rucio01    | 项目源码 | 文件源码
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
    """

    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?)')
项目:rucio    作者:rucio01    | 项目源码 | 文件源码
def get_rule(rule_id, session=None):
    """
    Get a specific replication rule.

    :param rule_id: The rule_id to select.
    :param session: The database session in use.
    :raises:        RuleNotFound if no Rule can be found.
    """

    try:
        rule = session.query(models.ReplicationRule).filter_by(id=rule_id).one()
        d = {}
        for column in rule.__table__.columns:
            d[column.name] = getattr(rule, column.name)
        return d

    except NoResultFound:
        raise RuleNotFound('No rule with the id %s found' % (rule_id))
    except StatementError:
        raise RucioException('Badly formatted rule id (%s)' % (rule_id))
项目:rucio    作者:rucio01    | 项目源码 | 文件源码
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))
项目:sqlalchemy_aio    作者:RazerM    | 项目源码 | 文件源码
def test_close(engine):
    conn = await engine.connect()
    assert not conn.closed

    result = await conn.execute(select([1]))
    assert await result.scalar() == 1

    await conn.close()
    assert conn.closed

    with pytest.raises(StatementError) as exc:
        await conn.execute(select([1]))
    assert "This Connection is closed" in str(exc)
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def post(self):
        """
        Create a model with data given in the request body. JSON format is
        expected. The model performs the validation automatically when
        instantiated.
        """

        try:
            data = request.json
            self.check_create_permissions(data)
            instance = self.model(**data)
            instance.save()
            return instance.serialize(), 201

        except TypeError as exception:
            current_app.logger.error(str(exception))
            return {"error": str(exception)}, 400

        except IntegrityError as exception:
            current_app.logger.error(str(exception))
            return {"error": str(exception)}, 400

        except StatementError as exception:
            current_app.logger.error(str(exception))
            return {"error": str(exception)}, 400

        except permissions.PermissionDenied:
            abort(403)
项目:zou    作者:cgwire    | 项目源码 | 文件源码
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)
            self.check_read_permissions(instance.serialize())
        except StatementError:
            return {"error": "Wrong id format"}, 400
        return instance.serialize(), 200
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def put(self, instance_id):
        """
        Update a model with data given in the request body. JSON format is
        expected. Model performs the validation automatically when fields are
        modified.
        """
        try:
            data = self.get_arguments()
            instance = self.get_model_or_404(instance_id)
            self.check_update_permissions(instance.serialize(), data)
            instance.update(data)
            return instance.serialize(), 200

        except StatementError:
            return {"error": "Wrong id format"}, 400

        except TypeError as exception:
            current_app.logger.error(str(exception))
            return {"error": str(exception)}, 400

        except IntegrityError as exception:
            current_app.logger.error(str(exception))
            return {"error": str(exception)}, 400

        except StatementError as exception:
            current_app.logger.error(str(exception))
            return {"error": str(exception)}, 400

        except permissions.PermissionDenied:
            abort(403)
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def delete(self, error_id):
        try:
            error = DataImportError.get(error_id)
        except StatementError:
            abort(404)

        if error is None:
            abort(404)
        error.delete()

        return {"deletion_success": True}, 204
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def get_asset_raw(entity_id):
    try:
        entity = Entity.get(entity_id)
    except StatementError:
        raise AssetNotFoundException

    if entity is None or not is_asset(entity):
        raise AssetNotFoundException

    return entity
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def get_person_raw(person_id):
    try:
        person = Person.get(person_id)
    except StatementError:
        raise PersonNotFoundException()

    if person is None:
        raise PersonNotFoundException()
    return person
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def get_by_email_raw(email):
    try:
        person = Person.get_by(email=email)
    except StatementError:
        raise PersonNotFoundException()

    if person is None:
        raise PersonNotFoundException()
    return person
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def get_by_desktop_login(desktop_login):
    try:
        person = Person.get_by(desktop_login=desktop_login)
    except StatementError:
        raise PersonNotFoundException()

    if person is None:
        raise PersonNotFoundException()
    return person.serialize()
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def get_shot_raw(shot_id):
    shot_type = get_shot_type()
    try:
        shot = Entity.get_by(
            entity_type_id=shot_type["id"],
            id=shot_id
        )
    except StatementError:
        raise SequenceNotFoundException

    if shot is None:
        raise ShotNotFoundException

    return shot
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def get_scene_raw(scene_id):
    scene_type = get_scene_type()
    try:
        scene = Entity.get_by(
            entity_type_id=scene_type["id"],
            id=scene_id
        )
    except StatementError:
        raise SequenceNotFoundException

    if scene is None:
        raise SceneNotFoundException

    return scene
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def get_episode_raw(episode_id):
    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
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def get_instance(model, instance_id, exception):
    try:
        instance = model.get(instance_id)
    except StatementError:
        raise exception()

    if instance is None:
        raise exception()

    return instance
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def get_preview_file(preview_file_id):
    try:
        preview_file = PreviewFile.get(preview_file_id)
    except StatementError:
        raise PreviewFileNotFoundException()

    if preview_file is None:
        raise PreviewFileNotFoundException()

    return preview_file.serialize()
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def get_model_raw(model, instance_id, exception):
    try:
        instance = model.get(instance_id)
    except StatementError:
        raise exception

    if instance is None:
        raise exception

    return instance
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def get_project_raw(project_id):
    try:
        project = Project.get(project_id)
    except StatementError:
        raise ProjectNotFoundException()

    if project is None:
        raise ProjectNotFoundException()

    return project
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def get_task_raw(task_id):
    try:
        task = Task.get(task_id)
    except StatementError:
        raise TaskNotFoundException()

    if task is None:
        raise TaskNotFoundException()

    return task
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def get_task_type_raw(task_type_id):
    try:
        task_type = TaskType.get(task_type_id)
    except StatementError:
        raise TaskTypeNotFoundException()

    if task_type is None:
        raise TaskTypeNotFoundException()

    return task_type
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def get_department(department_id):
    try:
        department = Department.get(department_id)
    except StatementError:
        raise DepartmentNotFoundException()

    if department is None:
        raise DepartmentNotFoundException()

    return department.serialize()
项目:zou    作者:cgwire    | 项目源码 | 文件源码
def get_comment(comment_id):
    try:
        comment = Comment.get(comment_id)
    except StatementError:
        raise CommentNotFoundException()

    if comment is None:
        raise CommentNotFoundException()
    return comment
项目:versionalchemy    作者:NerdWalletOSS    | 项目源码 | 文件源码
def test_json_encoded_object_value(self):
        o = TestClass('bar')
        m = TestModel(json_list=[o], json_dict={'a': o})
        self.session.add(m)
        try:
            self.session.flush()
        except StatementError as e:
            self.assertTrue(type(e.orig), TypeError)
            return
        self.assertTrue(False, 'Test should have raised StatementError')
项目:versionalchemy    作者:NerdWalletOSS    | 项目源码 | 文件源码
def test_json_encoded_write_failure(self):
        m = TestModel(json_list={'a': 'b'})
        self.session.add(m)
        try:
            self.session.flush()
        except StatementError as e:
            self.assertEqual(
                e.message,
                "(exceptions.ValueError) value of type <type 'dict'> is not <type 'list'>"
            )
            return
        self.assertTrue(False, 'Test should have raised StatementError')
项目:rucio    作者:rucio01    | 项目源码 | 文件源码
def list_rules(filters={}, session=None):
    """
    List replication rules.

    :param filters: dictionary of attributes by which the results should be filtered.
    :param session: The database session in use.
    :raises:        RucioException
    """

    query = session.query(models.ReplicationRule)
    if filters:
        for (key, value) in filters.items():
            if key == 'created_before':
                query = query.filter(models.ReplicationRule.created_at <= str_to_date(value))
                continue
            elif key == 'created_after':
                query = query.filter(models.ReplicationRule.created_at >= str_to_date(value))
                continue
            elif key == 'updated_before':
                query = query.filter(models.ReplicationRule.updated_at <= str_to_date(value))
                continue
            elif key == 'updated_after':
                query = query.filter(models.ReplicationRule.updated_at >= str_to_date(value))
                continue
            elif key == 'state':
                if isinstance(value, basestring):
                    value = RuleState.from_string(value)
                else:
                    try:
                        value = RuleState.from_sym(value)
                    except ValueError:
                        pass
            elif key == 'did_type' and isinstance(value, basestring):
                value = DIDType.from_string(value)
            elif key == 'grouping' and isinstance(value, basestring):
                value = RuleGrouping.from_string(value)
            query = query.filter(getattr(models.ReplicationRule, key) == value)

    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?)')
项目:rucio    作者:rucio01    | 项目源码 | 文件源码
def approve_rule(rule_id, approver=None, notify_approvers=True, session=None):
    """
    Approve a specific replication rule.

    :param rule_id:           The rule_id to approve.
    :param approver:          The account which is approving the rule.
    :param notify_approvers:  Notify the other approvers.
    :param session:           The database session in use.
    :raises:                  RuleNotFound if no Rule can be found.
    """

    try:
        rule = session.query(models.ReplicationRule).filter_by(id=rule_id).one()
        if rule.state == RuleState.WAITING_APPROVAL:
            rule.ignore_account_limit = True
            rule.state = RuleState.INJECT
            if approver:
                approver_email = get_account(account=approver, session=session).email
                if approver_email:
                    approver = '%s (%s)' % (approver, approver_email)
            else:
                approver = 'AUTOMATIC'
            with open('%s/rule_approved_user.tmpl' % config_get('common', 'mailtemplatedir'), 'r') as templatefile:
                template = Template(templatefile.read())
                email = get_account(account=rule.account, session=session).email
                if email:
                    text = template.safe_substitute({'rule_id': str(rule.id),
                                                     'expires_at': str(rule.expires_at),
                                                     'rse_expression': rule.rse_expression,
                                                     'comment': rule.comments,
                                                     'scope': rule.scope,
                                                     'name': rule.name,
                                                     'did_type': rule.did_type,
                                                     'approver': approver})
                    add_message(event_type='email',
                                payload={'body': text,
                                         'to': [email],
                                         'subject': '[RUCIO] Replication rule %s has been approved' % (str(rule.id))},
                                session=session)
            # Also notify the other approvers
            if notify_approvers:
                with open('%s/rule_approved_admin.tmpl' % config_get('common', 'mailtemplatedir'), 'r') as templatefile:
                    template = Template(templatefile.read())
                text = template.safe_substitute({'rule_id': str(rule.id),
                                                 'approver': approver})
                recipents = __create_recipents_list(rse_expression=rule.rse_expression, session=session)
                for recipent in recipents:
                    add_message(event_type='email',
                                payload={'body': text,
                                         'to': [recipent[0]],
                                         'subject': 'Re: [RUCIO] Request to approve replication rule %s' % (str(rule.id))},
                                session=session)
    except NoResultFound:
        raise RuleNotFound('No rule with the id %s found' % (rule_id))
    except StatementError:
        raise RucioException('Badly formatted rule id (%s)' % (rule_id))
项目:rucio    作者:rucio01    | 项目源码 | 文件源码
def examine_rule(rule_id, session=None):
    """
    Examine a replication rule for transfer errors.

    :param rule_id:            Replication rule id
    :param session:            Session of the db.
    :returns:                  Dictionary of informations
    """
    result = {'rule_error': None,
              'transfers': []}

    try:
        rule = session.query(models.ReplicationRule).filter_by(id=rule_id).one()
        if rule.state == RuleState.OK:
            result['rule_error'] = 'This replication rule is OK'
        elif rule.state == RuleState.REPLICATING:
            result['rule_error'] = 'This replication rule is currently REPLICATING'
        elif rule.state == RuleState.SUSPENDED:
            result['rule_error'] = 'This replication rule is SUSPENDED'
        else:
            result['rule_error'] = rule.error
            # Get the stuck locks
            stuck_locks = session.query(models.ReplicaLock).filter_by(rule_id=rule_id, state=LockState.STUCK).all()
            for lock in stuck_locks:
                # Get the count of requests in the request_history for each lock
                transfers = session.query(models.Request.__history_mapper__.class_).filter_by(scope=lock.scope, name=lock.name, dest_rse_id=lock.rse_id).order_by(models.Request.__history_mapper__.class_.created_at.desc()).all()
                transfer_cnt = len(transfers)
                # Get the error of the last request that has been tried and also the SOURCE used for the last request
                last_error, last_source, last_time, sources = None, None, None, []
                if transfers:
                    last_request = transfers[0]
                    last_error = last_request.state
                    last_time = last_request.created_at
                    last_source = None if last_request.source_rse_id is None else get_rse_name(last_request.source_rse_id, session=session)
                    available_replicas = session.query(models.RSEFileAssociation).filter_by(scope=lock.scope, name=lock.name, state=ReplicaState.AVAILABLE).all()
                    for replica in available_replicas:
                        sources.append((get_rse(None, rse_id=replica.rse_id, session=session).rse,
                                        True if get_rse(None, rse_id=replica.rse_id, session=session).availability >= 4 else False))
                result['transfers'].append({'scope': lock.scope,
                                            'name': lock.name,
                                            'rse': get_rse_name(lock.rse_id, session=session),
                                            'attempts': transfer_cnt,
                                            'last_error': str(last_error),
                                            'last_source': last_source,
                                            'sources': sources,
                                            'last_time': last_time})
        return result
    except NoResultFound:
        raise RuleNotFound('No rule with the id %s found' % (rule_id))
    except StatementError:
        raise RucioException('Badly formatted rule id (%s)' % (rule_id))