Exemplo n.º 1
0
    def new_funct(*args, **kwargs):

        if not isgeneratorfunction(function):
            raise ESSException(
                'stream_session decorator should be used only with generator. Use read_session instead.'
            )

        if not kwargs.get('session'):
            session = get_session()
            try:
                kwargs['session'] = session
                for row in function(*args, **kwargs):
                    yield row
            except TimeoutError as error:
                print(error)
                session.rollback()  # pylint: disable=maybe-no-member
                raise DatabaseException(str(error))
            except DatabaseError as error:
                print(error)
                session.rollback()  # pylint: disable=maybe-no-member
                raise DatabaseException(str(error))
            except:  # noqa: B901
                session.rollback()  # pylint: disable=maybe-no-member
                raise
            finally:
                session.remove()
        else:
            try:
                for row in function(*args, **kwargs):
                    yield row
            except:  # noqa: B901
                raise
Exemplo n.º 2
0
    def new_funct(*args, **kwargs):

        if isgeneratorfunction(function):
            raise ESSException(
                'read_session decorator should not be used with generator. Use stream_session instead.'
            )

        if not kwargs.get('session'):
            session = get_session()
            try:
                kwargs['session'] = session
                return function(*args, **kwargs)
            except TimeoutError as error:
                session.rollback()  # pylint: disable=maybe-no-member
                raise DatabaseException(str(error))
            except DatabaseError as error:
                session.rollback()  # pylint: disable=maybe-no-member
                raise DatabaseException(str(error))
            except:  # noqa: B901
                session.rollback()  # pylint: disable=maybe-no-member
                raise
            finally:
                session.remove()
        try:
            return function(*args, **kwargs)
        except:  # noqa: B901
            raise
Exemplo n.º 3
0
def psql_convert_decimal_to_float(dbapi_conn, connection_rec):
    """
    The default datatype returned by psycopg2 for numerics is decimal.Decimal.
    This type cannot be serialised to JSON, therefore we need to autoconvert to floats.
    :param dbapi_conn: DBAPI connection
    :param connection_rec: connection record
    """

    try:
        import psycopg2.extensions  # pylint: disable=import-error
    except:  # noqa: B901
        raise ESSException(
            'Trying to use PostgreSQL without psycopg2 installed!')

    DEC2FLOAT = psycopg2.extensions.new_type(
        psycopg2.extensions.DECIMAL.values, 'DEC2FLOAT',
        lambda value, curs: float(value) if value is not None else None)
    psycopg2.extensions.register_type(DEC2FLOAT)
Exemplo n.º 4
0
def mysql_convert_decimal_to_float(dbapi_conn, connection_rec):
    """
    The default datatype returned by mysql-python for numerics is decimal.Decimal.
    This type cannot be serialised to JSON, therefore we need to autoconvert to floats.
    Even worse, there's two types of decimals created by the MySQLdb driver, so we must
    override both.
    :param dbapi_conn: DBAPI connection
    :param connection_rec: connection record
    """

    try:
        import MySQLdb.converters  # pylint: disable=import-error
        from MySQLdb.constants import FIELD_TYPE  # pylint: disable=import-error
    except:  # noqa: B901
        raise ESSException(
            'Trying to use MySQL without mysql-python installed!')
    conv = MySQLdb.converters.conversions.copy()
    conv[FIELD_TYPE.DECIMAL] = float
    conv[FIELD_TYPE.NEWDECIMAL] = float
    dbapi_conn.converter = conv