Пример #1
0
    def query_inner(*args, **kwargs):  #1
        start = time()
        stmp_id = id_gen()
        session = operation.session
        sessionid = id(session)

        # set distributed transaction id to 0 for single transaction
        try:
            operation.id
        except:
            operation.id = str(uuid4())

        try:
            # get runtime info
            cp = current_process()
            ct = current_thread()

            # format request params
            params = []
            for item in args:
                params.append(str(item))
            for k, v in kwargs.iteritems():
                params.append(u"'%s':'%s'" % (k, v))

            # call internal function
            res = fn(*args, **kwargs)
            elapsed = round(time() - start, 4)
            logger.debug(u'%s.%s - %s - query - %s - %s - OK - %s' %
                         (operation.id, stmp_id, sessionid, fn.__name__,
                          params, elapsed))
            return res
        except ModelError as ex:
            elapsed = round(time() - start, 4)
            logger.error(u'%s.%s - %s - query - %s - %s - KO - %s' %
                         (operation.id, stmp_id, sessionid, fn.__name__,
                          params, elapsed))
            logger.error(ex.desc, exc_info=1)
            logger.error(ex.desc)
            raise QueryError(ex.desc, code=ex.code)
        except DBAPIError as ex:
            elapsed = round(time() - start, 4)
            logger.error(u'%s.%s - %s - query - %s - %s - KO - %s' %
                         (operation.id, stmp_id, sessionid, fn.__name__,
                          params, elapsed))
            logger.error(ex.orig, exc_info=1)
            logger.error(ex.orig)
            raise QueryError(ex.orig, code=400)
        except Exception as ex:
            elapsed = round(time() - start, 4)
            logger.error(u'%s.%s - %s - query - %s - %s - KO - %s' %
                         (operation.id, stmp_id, sessionid, fn.__name__,
                          params, elapsed))
            logger.error(ex, exc_info=1)
            logger.error(ex)

            raise QueryError(ex, code=400)
Пример #2
0
    def query_wrap(*args, **kwargs): #1
        start = time()
        stmp_id = id_gen()
        session = operation.session
        sessionid = id(session)

        # set distributed transaction id to 0 for single transaction
        try:
            operation.id
        except: 
            operation.id = str(uuid4())
        
        try:
            # format request params
            params = []
            for item in args:
                params.append(str(item))
            for k, v in kwargs.items():
                params.append(u"'%s':'%s'" % (k, v))

            # call internal function
            res = fn(*args, **kwargs)
            elapsed = round(time() - start, 4)
            logger.debug2(OK_Q_MSG % (operation.id, stmp_id, sessionid, fn.__name__, truncate(params),  elapsed))
            return res
        except ModelError as ex:
            elapsed = round(time() - start, 4)
            logger.error(KO_Q_MSG % (operation.id, stmp_id, sessionid, fn.__name__, truncate(params), elapsed))
            logger.error(ex.desc)
            raise QueryError(ex.desc, code=ex.code)
        except ArgumentError as ex:
            elapsed = round(time() - start, 4)
            logger.error(KO_Q_MSG % (operation.id, stmp_id, sessionid, fn.__name__, truncate(params), elapsed))
            logger.error(str(ex))
            raise QueryError(str(ex), code=400)
        except DBAPIError as ex:
            elapsed = round(time() - start, 4)
            logger.error(KO_Q_MSG % (operation.id, stmp_id, sessionid, fn.__name__, truncate(params), elapsed))
            logger.error(str(ex))
            raise QueryError(str(ex), code=400)
        except TypeError as ex:
            elapsed = round(time() - start, 4)
            logger.error(KO_Q_MSG % (operation.id, stmp_id, sessionid, fn.__name__, truncate(params), elapsed))
            logger.error(ex)
            raise QueryError(ex, code=400)
        except Exception as ex:
            elapsed = round(time() - start, 4)
            logger.error(KO_Q_MSG % (operation.id, stmp_id, sessionid, fn.__name__, truncate(params), elapsed))
            logger.error(str(ex))
            raise QueryError(str(ex), code=400)
Пример #3
0
    def add_all(self, entities):
        """Add a list of entities using as input parameter the list of orm entity instances

        :param entities: list of orm entity instance
        :return: num of orm entity instance
        :raises TransactionError: raise :class:`TransactionError`
        """
        if entities is None:
            raise QueryError('Error: can\'t not add None entity')

        session = self.get_session()
        session.add_all(entities)
        session.flush()
        self.logger.debug2('Add all %s entity %s' %
                           (entities[0].__class__.__name__, len(entities)))
        return len(entities)
Пример #4
0
    def add(self, entity):
        """Add an entity using as input parameter the orm entity instance

        :param entity: orm entity instance
        :return: orm entity instance
        :raises TransactionError: raise :class:`TransactionError`
        """
        if entity is None:
            raise QueryError('Error: can\'t not add None entity')

        session = self.get_session()
        session.add(entity)
        session.flush()
        self.logger.debug2('Add %s entity %s' %
                           (entity.__class__.__name__, entity))
        return entity
Пример #5
0
    def update(self, entity):
        """Update an entity using as input parameter the orm entity instance

        :param entity: orm entity instance
        :return: orm entity instance
        :raises TransactionError: raise :class:`TransactionError`
        """
        if entity is None:
            raise QueryError('Error: can\'t not update None entity')

        self.logger.info('Update %s entity %s' %
                         (entity.__class__.__name__, entity))
        if isinstance(entity, AuditData):
            entity.modification_date = datetime.now()

        session = self.get_session()
        session.merge(entity)
        session.flush()
        self.logger.info('Updated')
        return entity
Пример #6
0
    def bulk_save_entities(self, entities):
        """Perform a bulk save of the given list of entities

        :param entities: list of orm entity instance
        :return: list of orm entity instance
        :raises TransactionError: raise :class:`TransactionError`
        """
        if entities is None:
            raise QueryError('Error: can \'t not bulk update None entities')

        for entity in entities:
            self.logger.debug('Update %s entity %s' %
                              (entities.__class__.__name__, entity))
            if isinstance(entity, BaseEntity):
                entity.modification_date = datetime.today()

        session = self.get_session()
        session.bulk_save_objects(entities)
        session.flush()
        self.logger.info('Bulk updated %s entities' % len(entities))
        return entities
Пример #7
0
    def delete(self, entity):
        """Delete entity

        :param entity: orm entity instance
        :return: orm entity instance
        :raises TransactionError: raise :class:`TransactionError`
        """
        if entity is None:
            raise QueryError('Error: can\'t not delete None entity')

        self.logger.debug2('Delete %s entity %s' %
                           (entity.__class__.__name__, entity))
        if isinstance(entity, BaseEntity):
            if entity.is_active():
                entity.disable()
                self.update(entity)
                logger.info('Disable entity %s' % entity.id)
            else:
                logger.info('Nothing to do on %s !' % entity)
        else:
            self.purge(entity)
            logger.info('Purge entity %s' % entity.id)
        return entity