Change DatabaseConn to return the cursor instead

This commit is contained in:
angelblue05 2016-11-07 17:32:40 -06:00
parent 13e4ff5128
commit 6d0b2b96bc
8 changed files with 218 additions and 244 deletions

View file

@ -15,7 +15,6 @@ import requests
import image_cache_thread
from utils import window, settings, dialog, language as lang, JSONRPC
from database import DatabaseConn
from contextlib import closing
##################################################################################################
@ -166,8 +165,7 @@ class Artwork(object):
def _cache_all_video_entries(self, pdialog):
with DatabaseConn('video') as conn:
with closing(conn.cursor()) as cursor_video:
with DatabaseConn('video') as cursor_video:
cursor_video.execute("SELECT url FROM art WHERE media_type != 'actor'") # dont include actors
result = cursor_video.fetchall()
@ -189,8 +187,7 @@ class Artwork(object):
def _cache_all_music_entries(self, pdialog):
with DatabaseConn('music') as conn:
with closing(conn.cursor()) as cursor_music:
with DatabaseConn('music') as cursor_music:
cursor_music.execute("SELECT url FROM art")
result = cursor_music.fetchall()
@ -230,8 +227,7 @@ class Artwork(object):
log.debug("deleted: %s", filename)
# remove all existing data from texture DB
with DatabaseConn('texture') as conn:
with closing(conn.cursor()) as cursor_texture:
with DatabaseConn('texture') as cursor_texture:
cursor_texture.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"')
rows = cursor_texture.fetchall()
for row in rows:
@ -431,8 +427,7 @@ class Artwork(object):
@classmethod
def delete_cached_artwork(cls, url):
# Only necessary to remove and apply a new backdrop or poster
with DatabaseConn('texture') as conn:
with closing(conn.cursor()) as cursor_texture:
with DatabaseConn('texture') as cursor_texture:
try:
cursor_texture.execute("SELECT cachedurl FROM texture WHERE url = ?", (url,))
cached_url = cursor_texture.fetchone()[0]
@ -453,7 +448,6 @@ class Artwork(object):
except OperationalError:
log.debug("Issue deleting url from cache. Skipping.")
def get_people_artwork(self, people):
# append imageurl if existing
for person in people:

View file

@ -14,7 +14,6 @@ import musicutils as musicutils
from utils import settings, dialog, language as lang
from dialogs import context
from database import DatabaseConn
from contextlib import closing
#################################################################################################
@ -89,8 +88,7 @@ class ContextMenu(object):
if not item_id and kodi_id and item_type:
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
with DatabaseConn('emby') as cursor:
emby_db = embydb.Embydb_Functions(cursor)
item = emby_db.getItem_byKodiId(kodi_id, item_type)
try:
@ -165,8 +163,7 @@ class ContextMenu(object):
def _rate_song(self):
with DatabaseConn('music') as conn:
with closing(conn.cursor()) as cursor_music:
with DatabaseConn('music') as cursor_music:
query = "SELECT rating FROM song WHERE idSong = ?"
cursor_music.execute(query, (self.kodi_id,))
try:

View file

@ -4,7 +4,6 @@
import logging
import sqlite3
from contextlib import closing
import sys
import traceback
@ -98,7 +97,8 @@ class DatabaseConn(object):
self.conn = sqlite3.connect(self.path, timeout=self.timeout)
log.info("opened: %s - %s", self.path, id(self.conn))
return self.conn
self.cursor = self.conn.cursor()
return self.conn.cursor()
def _SQL(self, media_type):
@ -126,6 +126,7 @@ class DatabaseConn(object):
log.info("commit: %s", self.path)
log.info("closing: %s - %s", self.path, id(self.conn))
self.cursor.close()
self.conn.close()
@ -156,8 +157,7 @@ def db_reset():
# Wipe the kodi databases
log.warn("Resetting the Kodi video database.")
with DatabaseConn('video') as conn:
with closing(conn.cursor()) as cursor:
with DatabaseConn('video') as cursor:
cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"')
rows = cursor.fetchall()
for row in rows:
@ -167,8 +167,7 @@ def db_reset():
if settings('enableMusic') == "true":
log.warn("Resetting the Kodi music database.")
with DatabaseConn('music') as conn:
with closing(conn.cursor()) as cursor:
with DatabaseConn('music') as cursor:
cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"')
rows = cursor.fetchall()
for row in rows:
@ -178,8 +177,7 @@ def db_reset():
# Wipe the emby database
log.warn("Resetting the Emby database.")
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
with DatabaseConn('emby') as cursor:
cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"')
rows = cursor.fetchall()
for row in rows:

View file

@ -30,7 +30,6 @@ import api
from views import Playlist, VideoNodes
from utils import window, settings, dialog, language as lang
from database import DatabaseConn
from contextlib import closing
#################################################################################################
@ -235,8 +234,7 @@ def deleteItem():
log.info("Unknown type, unable to proceed.")
return
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
with DatabaseConn('emby') as cursor:
emby_db = embydb.Embydb_Functions(cursor)
item = emby_db.getItem_byKodiId(dbId, itemType)
@ -422,8 +420,7 @@ def getThemeMedia():
return
# Get every user view Id
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
with DatabaseConn('emby') as cursor:
emby_db = embydb.Embydb_Functions(cursor)
viewids = emby_db.getViews()

View file

@ -8,7 +8,6 @@ import read_embyserver as embyserver
from objects import Movies, MusicVideos, TVShows, Music
from utils import settings
from database import DatabaseConn
from contextlib import closing
#################################################################################################
@ -62,8 +61,7 @@ class Items(object):
# this is going to open a music connection even if it is not needed but
# I feel that is better than trying to sort out the login yourself
with DatabaseConn('music') as conn:
with closing(conn.cursor()) as cursor_music:
with DatabaseConn('music') as cursor_music:
for itemtype in items:

View file

@ -14,7 +14,6 @@ import playbackutils as pbutils
from utils import window, settings
from ga_client import log_error
from database import DatabaseConn
from contextlib import closing
#################################################################################################
@ -167,8 +166,7 @@ class KodiMonitor(xbmc.Monitor):
item_id = None
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
with DatabaseConn('emby') as cursor:
emby_db = embydb.Embydb_Functions(cursor)
db_item = emby_db.getItem_byKodiId(kodi_id, item_type)

View file

@ -14,6 +14,7 @@ import xbmcvfs
import api
import utils
import clientinfo
import database
import downloadutils
import itemtypes
import embydb_functions as embydb
@ -23,8 +24,6 @@ import views
from objects import Movies, MusicVideos, TVShows, Music
from utils import window, settings, language as lang, should_stop
from ga_client import GoogleAnalytics
import database
from contextlib import closing
##################################################################################################
@ -233,8 +232,8 @@ class LibrarySync(threading.Thread):
utils.sourcesXML()
# use emby and video DBs
with database.DatabaseConn('emby') as conn_emby, database.DatabaseConn('video') as conn_video:
with closing(conn_emby.cursor()) as cursor_emby, closing(conn_video.cursor()) as cursor_video:
with database.DatabaseConn('emby') as cursor_emby:
with database.DatabaseConn('video') as cursor_video:
# content sync: movies, tvshows, musicvideos, music
if manualrun:
@ -275,7 +274,7 @@ class LibrarySync(threading.Thread):
# Set views
views.Views(cursor_emby, cursor_video).maintain()
conn_emby.commit()
cursor_emby.connection.commit()
#self.maintainViews(cursor_emby, cursor_video)
# Sync video library
@ -312,8 +311,8 @@ class LibrarySync(threading.Thread):
if repair and 'music' not in repair_list:
pass
else:
with database.DatabaseConn('emby') as conn_emby, database.DatabaseConn('music') as conn_music:
with closing(conn_emby.cursor()) as cursor_emby, closing(conn_music.cursor()) as cursor_music:
with database.DatabaseConn('emby') as cursor_emby:
with database.DatabaseConn('music') as cursor_music:
startTime = datetime.now()
completed = self.music(cursor_emby, cursor_music, pDialog)
if not completed:
@ -332,8 +331,7 @@ class LibrarySync(threading.Thread):
if pDialog:
pDialog.close()
with database.DatabaseConn('emby') as conn_emby:
with closing(conn_emby.cursor()) as cursor_emby:
with database.DatabaseConn('emby') as cursor_emby:
emby_db = embydb.Embydb_Functions(cursor_emby)
current_version = emby_db.get_version(self.clientInfo.get_version())
@ -362,8 +360,8 @@ class LibrarySync(threading.Thread):
def refreshViews(self):
with database.DatabaseConn('emby') as conn_emby, database.DatabaseConn() as conn_video:
with closing(conn_emby.cursor()) as cursor_emby, closing(conn_video.cursor()) as cursor_video:
with database.DatabaseConn('emby') as cursor_emby:
with database.DatabaseConn() as cursor_video:
# Compare views, assign correct tags to items
views.Views(cursor_emby, cursor_video).maintain()
@ -515,8 +513,8 @@ class LibrarySync(threading.Thread):
# do a lib update if any items in list
totalUpdates = len(self.addedItems) + len(self.updateItems) + len(self.userdataItems) + len(self.removeItems)
if totalUpdates > 0:
with database.DatabaseConn('emby') as conn_emby, database.DatabaseConn('video') as conn_video:
with closing(conn_emby.cursor()) as cursor_emby, closing(conn_video.cursor()) as cursor_video:
with database.DatabaseConn('emby') as cursor_emby:
with database.DatabaseConn('video') as cursor_video:
emby_db = embydb.Embydb_Functions(cursor_emby)
@ -610,8 +608,7 @@ class LibrarySync(threading.Thread):
def _verify_emby_database(self):
# Create the tables for the emby database
with database.DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
with database.DatabaseConn('emby') as cursor:
# emby, view, version
cursor.execute(
"""CREATE TABLE IF NOT EXISTS emby(
@ -669,9 +666,7 @@ class LibrarySync(threading.Thread):
if (window('emby_dbCheck') != "true" and settings('SyncInstallRunDone') == "true"):
# Verify the validity of the database
log.info("Doing DB Version Check")
with database.DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
with database.DatabaseConn('emby') as cursor:
emby_db = embydb.Embydb_Functions(cursor)
currentVersion = emby_db.get_version()
###$ Begin migration $###

View file

@ -13,7 +13,6 @@ import embydb_functions as embydb
import read_embyserver as embyserver
from utils import window, JSONRPC
from database import DatabaseConn
from contextlib import closing
#################################################################################################
@ -31,8 +30,7 @@ class Playlist(object):
def play_all(self, item_ids, start_at):
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
with DatabaseConn('emby') as cursor:
emby_db = embydb.Embydb_Functions(cursor)
player = xbmc.Player()
@ -74,8 +72,7 @@ class Playlist(object):
def modify_playlist(self, item_ids):
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
with DatabaseConn('emby') as cursor:
emby_db = embydb.Embydb_Functions(cursor)
log.info("---*** ADD TO PLAYLIST ***---")