How to use the etlhelper.db_helper_factory.DB_HELPER_FACTORY.from_conn function in etlhelper

To help you get started, we’ve selected a few etlhelper examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github BritishGeologicalSurvey / etlhelper / test / unit / test_helper_factory.py View on Github external
def test_from_conn_not_registered():
    """
    Tests helpful error message on attempt to choose unregistered conn type.
    """
    conn = Mock()
    conn.__class__ = "Not a real class"

    with pytest.raises(ETLHelperHelperError,
                       match=r'Unsupported connection type.*'):
        DB_HELPER_FACTORY.from_conn(conn)
github BritishGeologicalSurvey / etlhelper / test / unit / test_helper_factory.py View on Github external
def test_from_conn(expected_helper, db_class):
    """
    Tests correct helper produced given a conn object
    """
    conn = Mock()
    # conn.__class__ = cx_Oracle.Connection
    conn.__class__ = db_class
    helper = DB_HELPER_FACTORY.from_conn(conn)
    assert isinstance(helper, expected_helper)
github BritishGeologicalSurvey / etlhelper / test / unit / test_helper_factory.py View on Github external
def test_from_conn_bad_type():
    with pytest.raises(ETLHelperHelperError,
                       match=r'Expected connection-like object.*'):
        DB_HELPER_FACTORY.from_conn('some string')
github BritishGeologicalSurvey / etlhelper / etlhelper / etl.py View on Github external
def execute(query, conn, parameters=()):
    """
    Run SQL query against connection.

    :param query: str, SQL query to execute
    :param conn: dbapi connection
    :param parameters: sequence or dict of bind variables to insert in the query
    """
    logger.info("Executing query")
    logger.debug(f"Executing:\n\n{query}\n\nwith parameters:\n\n"
                 f"{parameters}\n\nagainst\n\n{conn}")

    helper = DB_HELPER_FACTORY.from_conn(conn)
    with helper.cursor(conn) as cursor:
        # Run query
        try:
            cursor.execute(query, parameters)
            conn.commit()
        except helper.sql_exceptions as exc:
            # Even though we haven't modified data, we have to rollback to
            # clear the failed transaction before any others can be started.
            conn.rollback()
            msg = f"SQL query raised an error.\n\n{query}\n\n{exc}\n"
            raise ETLHelperQueryError(msg)
github BritishGeologicalSurvey / etlhelper / etlhelper / etl.py View on Github external
required to access results of some Oracle Spatial functions.

    :param select_query: str, SQL query to execute
    :param conn: dbapi connection
    :param parameters: sequence or dict of bind variables to insert in the query
    :param row_factory: function that accepts a cursor and returns a function
                        for parsing each row
    :param transform: function that accepts an iterable (e.g. list) of rows and
                      returns an iterable of rows (possibly of different shape)
    :param read_lob: bool, convert Oracle LOB objects to strings
    """
    logger.info("Fetching rows")
    logger.debug(f"Fetching:\n\n{select_query}\n\nwith parameters:\n\n"
                 f"{parameters}\n\nagainst\n\n{conn}")

    helper = DB_HELPER_FACTORY.from_conn(conn)
    with helper.cursor(conn) as cursor:
        # Run query
        try:
            cursor.execute(select_query, parameters)
        except helper.sql_exceptions as exc:
            # Even though we haven't modified data, we have to rollback to
            # clear the failed transaction before any others can be started.
            conn.rollback()
            msg = f"SQL query raised an error.\n\n{select_query}\n\n{exc}\n"
            raise ETLHelperExtractError(msg)

        # Set row factory
        create_row = row_factory(cursor)

        # Parse results
        first_pass = True