<commit_before>import sqlite3
from song_metadata import SongMetadata
from file_metadata import FileMetadata


class JukeboxDB:

    def __init__(self, metadata_db_file_path=None, debug_print=False):
        self.debug_print = debug_print
        self.db_connection = None
        if metadata_db_file_path is not None and len(metadata_db_file_path) > 0:
            self.metadata_db_file_path = metadata_db_file_path
        else:
            self.metadata_db_file_path = 'jukebox_db.sqlite3'

    def is_open(self):
        return self.db_connection is not None

    def open(self):
        self.close()
        open_success = False
        self.db_connection = sqlite3.connect(self.metadata_db_file_path)
        if self.db_connection is not None:
            if not self.have_tables():
                open_success = self.create_tables()
            else:
                open_success = True
        return open_success

    def close(self):
        did_close = False
        if self.db_connection is not None:
            self.db_connection.close()
            self.db_connection = None
            did_close = True
        return did_close

    def __enter__(self):
        # look for stored metadata in the storage system
        self.db_connection = sqlite3.connect(self.metadata_db_file_path)
        if self.db_connection is not None:
            if self.debug_print:
                print("have db connection")
        else:
            print("unable to connect to database")
        return self

    def __exit__(self, exception_type, exception_value, traceback):
        if self.db_connection is not None:
            self.db_connection.close()
            self.db_connection = None

    def create_table(self, sql):
        try:
            table_created = self.db_connection.execute(sql)
            if not table_created:
                print('creation of table failed')
                print('%s' % sql)
            return table_created
        except sqlite3.Error as e:
            print('error creating table: ' + e.args[0])
            return False

    def create_tables(self):
        if self.db_connection is not None:
            if self.debug_print:
                print("creating tables")

            create_genre_table = """CREATE TABLE genre (
                                 genre_uid TEXT UNIQUE NOT NULL,
                                 genre_name TEXT UNIQUE NOT NULL,
                                 genre_description TEXT)"""

            create_artist_table = """CREATE TABLE artist (
                                  artist_uid TEXT UNIQUE NOT NULL,
                                  artist_name TEXT UNIQUE NOT NULL,
                                  artist_description TEXT)"""

            create_album_table = """CREATE TABLE album (
                                 album_uid TEXT UNIQUE NOT NULL,
                                 album_name TEXT UNIQUE NOT NULL,
                                 album_description TEXT,
                                 artist_uid TEXT NOT NULL REFERENCES artist(artist_uid),
                                 genre_uid TEXT REFERENCES genre(genre_uid))"""

            create_song_table = """CREATE TABLE song (
                                song_uid TEXT UNIQUE NOT NULL,
                                file_time TEXT,
                                origin_file_size INTEGER,
                                stored_file_size INTEGER,
                                pad_char_count INTEGER,
                                artist_name TEXT,
                                artist_uid TEXT REFERENCES artist(artist_uid),
                                song_name TEXT NOT NULL,
                                md5_hash TEXT NOT NULL,
                                compressed INTEGER,
                                encrypted INTEGER,
                                container_name TEXT NOT NULL,
                                object_name TEXT NOT NULL,
                                album_uid TEXT REFERENCES album(album_uid))"""

            create_playlist_table = """CREATE TABLE playlist (
                                    playlist_uid TEXT UNIQUE NOT NULL,
                                    playlist_name TEXT UNIQUE NOT NULL,
                                    playlist_description TEXT)"""

            create_playlist_song_table = """CREATE TABLE playlist_song (
                                         playlist_song_uid TEXT UNIQUE NOT NULL,
                                         playlist_uid TEXT NOT NULL REFERENCES playlist(playlist_uid),
                                         song_uid TEXT NOT NULL REFERENCES song(song_uid))"""

            try:<commit_msg>Fixed construction of SQL statements<commit_after>import sqlite3
from song_metadata import SongMetadata
from file_metadata import FileMetadata


class JukeboxDB:

    def __init__(self, metadata_db_file_path=None, debug_print=False):
        self.debug_print = debug_print
        self.db_connection = None
        if metadata_db_file_path is not None and len(metadata_db_file_path) > 0:
            self.metadata_db_file_path = metadata_db_file_path
        else:
            self.metadata_db_file_path = 'jukebox_db.sqlite3'

    def is_open(self):
        return self.db_connection is not None

    def open(self):
        self.close()
        open_success = False
        self.db_connection = sqlite3.connect(self.metadata_db_file_path)
        if self.db_connection is not None:
            if not self.have_tables():
                open_success = self.create_tables()
            else:
                open_success = True
        return open_success

    def close(self):
        did_close = False
        if self.db_connection is not None:
            self.db_connection.close()
            self.db_connection = None
            did_close = True
        return did_close

    def __enter__(self):
        # look for stored metadata in the storage system
        self.db_connection = sqlite3.connect(self.metadata_db_file_path)
        if self.db_connection is not None:
            if self.debug_print:
                print("have db connection")
        else:
            print("unable to connect to database")
        return self

    def __exit__(self, exception_type, exception_value, traceback):
        if self.db_connection is not None:
            self.db_connection.close()
            self.db_connection = None

    def create_table(self, sql):
        try:
            table_created = self.db_connection.execute(sql)
            if not table_created:
                print('creation of table failed')
                print('%s' % sql)
            return table_created
        except sqlite3.Error as e:
            print('error creating table: ' + e.args[0])
            return False

    def create_tables(self):
        if self.db_connection is not None:
            if self.debug_print:
                print("creating tables")

            create_genre_table = "CREATE TABLE genre (" + \
                                 "genre_uid TEXT UNIQUE NOT NULL," + \
                                 "genre_name TEXT UNIQUE NOT NULL," + \
                                 "genre_description TEXT)"

            create_artist_table = "CREATE TABLE artist (" + \
                                  "artist_uid TEXT UNIQUE NOT NULL," + \
                                  "artist_name TEXT UNIQUE NOT NULL," + \
                                  "artist_description TEXT)"

            create_album_table = "CREATE TABLE album (" + \
                                 "album_uid TEXT UNIQUE NOT NULL," + \
                                 "album_name TEXT UNIQUE NOT NULL," + \
                                 "album_description TEXT," + \
                                 "artist_uid TEXT NOT NULL REFERENCES artist(artist_uid)," + \
                                 "genre_uid TEXT REFERENCES genre(genre_uid))"

            create_song_table = "CREATE TABLE song (" + \
                                "song_uid TEXT UNIQUE NOT NULL," + \
                                "file_time TEXT," + \
                                "origin_file_size INTEGER," + \
                                "stored_file_size INTEGER," + \
                                "pad_char_count INTEGER," + \
                                "artist_name TEXT," + \
                                "artist_uid TEXT REFERENCES artist(artist_uid)," + \
                                "song_name TEXT NOT NULL," + \
                                "md5_hash TEXT NOT NULL," + \
                                "compressed INTEGER," + \
                                "encrypted INTEGER," + \
                                "container_name TEXT NOT NULL," + \
                                "object_name TEXT NOT NULL," + \
                                "album_uid TEXT REFERENCES album(album_uid))"

            create_playlist_table = "CREATE TABLE playlist (" + \
                                    "playlist_uid TEXT UNIQUE NOT NULL," + \
                                    "playlist_name TEXT UNIQUE NOT NULL," + \
                                    "playlist_description TEXT)"

            create_playlist_song_table = "CREATE TABLE playlist_song (" + \
                                         "playlist_song_uid TEXT UNIQUE NOT NULL," + \
                                         "playlist_uid TEXT NOT NULL REFERENCES playlist(playlist_uid)," + \
                                         "song_uid TEXT NOT NULL REFERENCES song(song_uid))"

            try:
{'fraction_non_alphanumeric': 0.06105108570229728, 'fraction_numerical': 0.0016783803629497535, 'mean_word_length': 1.3062409288824384, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2521824', 'n_tokens_mistral': 2380, 'n_tokens_neox': 2261, 'n_words': 607}