diff options
author | Devan Franchini <twitch153@gentoo.org> | 2015-08-05 02:22:24 -0400 |
---|---|---|
committer | Devan Franchini <twitch153@gentoo.org> | 2015-08-05 02:22:31 -0400 |
commit | 59ce97acb46ddef1b564ab4032c145ed70c92427 (patch) | |
tree | 137a7d2e91786b665b371e5385ee87ed44329201 | |
parent | Adds remove function for overlay removal from databases (diff) | |
download | layman-59ce97acb46ddef1b564ab4032c145ed70c92427.tar.gz layman-59ce97acb46ddef1b564ab4032c145ed70c92427.tar.bz2 layman-59ce97acb46ddef1b564ab4032c145ed70c92427.zip |
sqlite_db.py: read_db() connects to database with "with" command
-rw-r--r-- | layman/db_modules/sqlite_db/sqlite_db.py | 107 |
1 files changed, 54 insertions, 53 deletions
diff --git a/layman/db_modules/sqlite_db/sqlite_db.py b/layman/db_modules/sqlite_db/sqlite_db.py index aef9661..14bfa25 100644 --- a/layman/db_modules/sqlite_db/sqlite_db.py +++ b/layman/db_modules/sqlite_db/sqlite_db.py @@ -148,63 +148,64 @@ class DBHandler(object): ''' Read the overlay definitions from the database and generate overlays. ''' - connection = self.__connect__(path) - cursor = connection.cursor() + cursor = None overlay_id = None overlay = {} - cursor.execute('''SELECT Overlay_ID, Name, Priority, Status, Quality, - Homepage, IRC, License FROM Overlay''') - overlays_info = cursor.fetchall() - connection.commit() + with self.__connect__(path) as connection: + cursor = connection.cursor() + cursor.execute('''SELECT Overlay_ID, Name, Priority, Status, + Quality, Homepage, IRC, License FROM Overlay''') + overlays_info = cursor.fetchall() + connection.commit() - for overlay_info in overlays_info: - overlay = {} - overlay_id = overlay_info[0] - overlay['name'] = overlay_info[1] - - cursor.execute('''SELECT URL, Type, Branch FROM Overlay_Source - JOIN Overlay USING (Overlay_ID) JOIN Source USING (Source_ID) - WHERE Overlay_ID = ?''', (overlay_id,)) - overlay['source'] = cursor.fetchall() - - cursor.execute('''SELECT Owner_Name, Owner_Email FROM - Overlay_Owner JOIN Overlay USING (Overlay_ID) JOIN Owner USING - (Owner_ID) WHERE Overlay_ID = ?''', (overlay_id,)) - owner_info = cursor.fetchall() - - if len(owner_info): - owner_info = owner_info[0] - overlay['owner_name'] = owner_info[0] - overlay['owner_email'] = owner_info[1] - - cursor.execute('''SELECT Description FROM Description JOIN Overlay - USING (Overlay_ID) WHERE Overlay_ID = ?''', (overlay_id,)) - overlay['description'] = cursor.fetchall()[0] - - overlay['status'] = overlay_info[3] - overlay['quality'] = overlay_info[4] - overlay['priority'] = overlay_info[2] - - if overlay_info[7]: - overlay['license'] = overlay_info[7] - else: - overlay['license'] = None - - overlay['homepage'] = overlay_info[5] - overlay['IRC'] = overlay_info[6] - - cursor.execute('''SELECT Feed FROM Feed JOIN Overlay USING - (Overlay_ID) WHERE Overlay_ID = ?''', (overlay_id,)) - overlay['feed'] = cursor.fetchall() - - if len(overlay['feed']): - overlay['feed'] = overlay['feed'][0] - - self.overlays[overlay_info[1]] = Overlay(self.config, - ovl_dict=overlay, - ignore=self.ignore) - connection.close() + for overlay_info in overlays_info: + overlay = {} + overlay_id = overlay_info[0] + overlay['name'] = overlay_info[1] + + cursor.execute('''SELECT URL, Type, Branch FROM Overlay_Source + JOIN Overlay USING (Overlay_ID) JOIN Source USING (Source_ID) + WHERE Overlay_ID = ?''', (overlay_id,)) + overlay['source'] = cursor.fetchall() + + cursor.execute('''SELECT Owner_Name, Owner_Email FROM + Overlay_Owner JOIN Overlay USING (Overlay_ID) JOIN Owner USING + (Owner_ID) WHERE Overlay_ID = ?''', (overlay_id,)) + owner_info = cursor.fetchall() + + if len(owner_info): + owner_info = owner_info[0] + overlay['owner_name'] = owner_info[0] + overlay['owner_email'] = owner_info[1] + + cursor.execute('''SELECT Description FROM Description JOIN + Overlay USING (Overlay_ID) WHERE Overlay_ID = ?''', + (overlay_id,)) + overlay['description'] = cursor.fetchall()[0] + + overlay['status'] = overlay_info[3] + overlay['quality'] = overlay_info[4] + overlay['priority'] = overlay_info[2] + + if overlay_info[7]: + overlay['license'] = overlay_info[7] + else: + overlay['license'] = None + + overlay['homepage'] = overlay_info[5] + overlay['IRC'] = overlay_info[6] + + cursor.execute('''SELECT Feed FROM Feed JOIN Overlay USING + (Overlay_ID) WHERE Overlay_ID = ?''', (overlay_id,)) + overlay['feed'] = cursor.fetchall() + + if len(overlay['feed']): + overlay['feed'] = overlay['feed'][0] + + self.overlays[overlay_info[1]] = Overlay(self.config, + ovl_dict=overlay, + ignore=self.ignore) def add_new(self, document=None, origin=None): |