def get_or_new(identifier):
    cache_key = CACHE_KEY_PREFIX + identifier

    project = db.get_cached_object(cache_key)
    if project:
        return project

    cursor = db.connection.cursor()
    cursor.execute(GET_PROJECT_SQL, (identifier, ))
    row = cursor.fetchone()

    # This will raise an error if the identifier is not valid
    # It's important to do this before inserting into the database.
    project = Project(identifier)

    if row is not None:
        project.db_id = row[0]
    else:
        try:
            cursor.execute(INSERT_PROJECT_SQL, {'identifier': identifier})

            project.db_id = cursor.lastrowid

        except mariadb.Error as e:
            db.connection.rollback()
            cursor.close()
            raise e

        db.connection.commit()

    cursor.close()
    db.set_object_in_cache(cache_key, project)
    return project
Example #2
0
def get_or_new(unique_column_val, cache_key, get_sql, insert_sql,
               new_obj_callback):

    obj = db.get_cached_object(cache_key)
    if obj:
        return obj

    cursor = db.connection.cursor()
    cursor.execute(get_sql, (unique_column_val, ))
    row = cursor.fetchone()

    # This will raise an error if the new object is not valid
    # It's important to do this before inserting into the database.
    obj = new_obj_callback()

    if row is not None:
        obj.db_id = row[0]
    else:
        try:
            cursor.execute(insert_sql, (unique_column_val, ))
            obj.db_id = cursor.lastrowid

        except mariadb.Error as e:
            db.connection.rollback()
            cursor.close()
            raise e

        db.connection.commit()

    cursor.close()
    db.set_object_in_cache(cache_key, obj)
    return obj
def get_or_new(language_code):
    cache_key = CACHE_KEY_PREFIX + language_code

    language = db.get_cached_object(cache_key)
    if language:
        return language

    cursor = db.connection.cursor()
    cursor.execute(GET_LANGUAGE_SQL, (language_code, ))
    row = cursor.fetchone()

    # This will raise an error if the identifier is not valid
    # It's important to do this before inserting into the database.
    language = Language(language_code)

    if row is not None:
        language.db_id = row[0]
    else:
        try:
            cursor.execute(INSERT_LANGUAGE_SQL,
                           {'language_code': language_code})

            language.db_id = cursor.lastrowid

        except mariadb.Error as e:
            db.connection.rollback()
            cursor.close()
            raise e

        db.connection.commit()

    cursor.close()
    db.set_object_in_cache(cache_key, language)
    return language
def get_or_new(country_code):
    cache_key = CACHE_KEY_PREFIX + country_code

    country = db.get_cached_object(cache_key)
    if country:
        return country

    cursor = db.connection.cursor()
    cursor.execute(GET_COUNTRY_SQL, (country_code, ))
    row = cursor.fetchone()

    # This will raise an error if the identifier is not valid
    # It's important to do this before inserting into the database.
    country = Country(country_code)

    if row is not None:
        country.db_id = row[0]
    else:
        try:
            cursor.execute(INSERT_COUNTRY_SQL, {'country_code': country_code})

            country.db_id = cursor.lastrowid

        except mariadb.Error as e:
            db.connection.rollback()
            cursor.close()
            raise e

        db.connection.commit()

    cursor.close()
    db.set_object_in_cache(cache_key, country)
    return country
def new(
        filename,
        directory,
        time,
        event_type,
        status = LogFileStatus.NEW,
        sample_rate = None,
        consumed_events = None,
        ignored_events = None,
        invalid_lines = None
    ):

    file = LogFile( filename, directory, time, event_type, sample_rate,
        status, consumed_events, ignored_events, invalid_lines )

    cursor = db.connection.cursor()

    try:
        cursor.execute( INSERT_FILE_SQL, {
            'filename': filename,
            'impressiontype': event_type.legacy_key,
            'timestamp': time,
            'directory': directory,
            'sample_rate': sample_rate,
            'status': status.value,
            'consumed_events': consumed_events,
            'ignored_events': ignored_events,
            'invalid_lines': invalid_lines
        } )

        file.db_id = cursor.lastrowid

    except mariadb.Error as e:
        db.connection.rollback()
        cursor.close()
        raise e

    db.connection.commit()
    cursor.close()

    db.set_object_in_cache( _make_cache_key( filename ), file )
    return file