def is_db_valid(self): if not os.path.exists(get_db_path()): return False if self._pragma_schema_version() < 21: return False else: return True
def dump(self): global lock_db lock_db.acquire() conn = sqlite3.connect(get_db_path()) for line in conn.iterdump(): print_out('%s\n' % line) lock_db.release()
def query(self, query, t=(), dict_factory_arg=None): conn = sqlite3.connect(get_db_path()) if dict_factory_arg != None: conn.row_factory = dict_factory_arg c = conn.cursor() c.execute(query, t) res_l = c.fetchall() c.close() return res_l
def build_database(self): global lock_db lock_db.acquire() if self._insert_session: lock_db.release() raise Exception( "Error: database.build_database It's impossible to build the database during an insertion !" ) db = self.dr14_db_main_structure_v1() conn = sqlite3.connect(get_db_path()) conn.executescript(db) conn.commit() self.ungrade_db() lock_db.release()
def commit_insert_session(self): global lock_db lock_db.acquire() if self._insert_session == False: lock_db.release() raise Exception( "Error: database.commit_insert_session the session has not been opened !" ) conn = sqlite3.connect(get_db_path()) c = conn.cursor() for k_dr in self._dr.keys(): c.execute("insert into DR ( Id , DR ) values ( ? , ? ) ", (k_dr, self._dr[k_dr])) for k_artist in self._artists.keys(): c.execute("insert into Artist ( Id , Name ) values ( ? , ? ) ", (k_artist, self._artists[k_artist])) for k_codec in self._codec.keys(): c.execute("insert into Codec ( Id , Name ) values ( ? , ? ) ", (k_codec, self._codec[k_codec])) for k_genre in self._genre.keys(): c.execute("insert into Genre ( Id , Name ) values ( ? , ? ) ", (k_genre, self._genre[k_genre])) for k_date in self._date.keys(): c.execute("insert into Date ( Id , Date ) values ( ? , ? ) ", (k_date, self._date[k_date])) for k_album in self._albums.keys(): q = "insert into Album ( Id , sha1 , title <s1> ) values ( :Id , :sha1 , :Title <s2> ) " if self._albums[k_album]["disk_nr"] != None: q = q.replace("<s1>", " , disk_nr <s1> ") q = q.replace("<s2>", " , :disk_nr <s2> ") q = q.replace("<s1>", "") q = q.replace("<s2>", "") c.execute(q, self._albums[k_album]) c.execute( "insert into DR_Album ( IdDr , IdAlbum ) values ( :dr_id , :Id )", self._albums[k_album]) if self._albums[k_album]["artist_id"] >= 0: c.execute( "insert into Artist_Album ( IdArtist , IdAlbum ) values ( :artist_id , :Id ) ", self._albums[k_album]) for k_track in self._tracks.keys(): q = """insert into Track ( Id , Title , rms , peak , duration , bit , bitrate , sampling_rate , sha1 , size <1s> ) values ( :id , :title , :rms , :peak , :duration , :bit , :bitrate , :sampling_rate , :sha1 , :size <2s> ) """ if self._tracks[k_track].get("track_nr", None) != None: q = q.replace("<1s>", " , track_nr <1s> ") q = q.replace("<2s>", " , :track_nr <2s> ") q = q.replace("<1s>", "") q = q.replace("<2s>", "") c.execute(q, self._tracks[k_track]) c.execute( "insert into DR_Track ( IdDr , IdTrack ) values ( :dr_id , :id )", self._tracks[k_track]) c.execute( "insert into Codec_Track ( IdCodec , IdTrack ) values ( :codec_id , :id )", self._tracks[k_track]) if self._tracks[k_track]["genre_id"] >= 0: c.execute( "insert into Genre_Track ( IdGenre , IdTrack ) values ( :genre_id , :id ) ", self._tracks[k_track]) if self._tracks[k_track]["date_id"] >= 0: c.execute( "insert into Date_Track ( IdDate , IdTrack ) values ( :date_id , :id ) ", self._tracks[k_track]) if self._tracks[k_track]["artist_id"] >= 0: c.execute( "insert into Artist_Track ( IdArtist , IdTrack ) values ( :artist_id , :id ) ", self._tracks[k_track]) if self._tracks[k_track]["album_id"] >= 0: c.execute( "insert into Album_Track ( IdAlbum , IdTrack ) values ( :album_id , :id ) ", self._tracks[k_track]) conn.commit() conn.close() self._insert_session = False lock_db.release()