From 947aa73411c6f396c5811f692e3b63273247fbfe Mon Sep 17 00:00:00 2001 From: bjschuma Date: Sat, 2 Jan 2010 00:01:13 -0500 Subject: [PATCH] Change how next ids are generated in extract.py, correct for names with apostrophes in them --- src/core/create.py | 12 ------------ src/core/extract.py | 29 +++++++++++++++++------------ src/core/scan.py | 5 +++-- src/core/tools/database.py | 19 +++++++++++++++---- 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/core/create.py b/src/core/create.py index 9d48edf0..f530ca12 100644 --- a/src/core/create.py +++ b/src/core/create.py @@ -52,16 +52,4 @@ def run(args=None): l = len(args) if args[0] == "library": library(args[1:]) - #name = "Default" - #root = "" - #if l >= 2: - # name = args[1] - #if l >= 3: - # space = ' ' - # root = expandPath( space.join(args[2:]) ) - #database.open() - #database.insert('library',(name,root) ) - #database.close() - #if not root=="": - # manager.run("scan",[root]) diff --git a/src/core/extract.py b/src/core/extract.py index edf87b23..390fc7c8 100644 --- a/src/core/extract.py +++ b/src/core/extract.py @@ -59,6 +59,14 @@ def close(): pass +def findId(table,name): + result = database.select("id",table,"name=\'"+name.replace("\'","\'\'")+"\'").fetchone() + if result == None: + return database.count(table),False + return result[0],True + + + # Called when the plugin needs to perform some action def run(file=None): if file == None: @@ -73,24 +81,21 @@ def run(file=None): except: return - artists = settings.get("artists") - # Find artist information - artist = t.artist + artist = t.artist#.replace("\'","\'\'") if artist == "": artist = "Unknown Artist" - # If artist does not exist yet, insert into database - if (artist in artists.keys())==False: - arid = database.count("artist") + arid,inserted = findId("artist",artist) + if inserted == False: database.insert("artist", (arid, artist) ) - artists[artist] = arid - else: - arid = artists[artist] - album = t.album + album = t.album#.replace("\'","\'\'") if album == "": album = "Unknown Album" - alid = database.count("album") - database.insert("album", (alid, album) ) + #alid = database.count("album") + alid,inserted = findId("album",album) + if inserted == False: + database.insert("album", (alid, album) ) + #database.commit() diff --git a/src/core/scan.py b/src/core/scan.py index 185c0314..d15abcf3 100644 --- a/src/core/scan.py +++ b/src/core/scan.py @@ -54,13 +54,14 @@ def run(args=None): space = ' ' path = expandPath( space.join(args) ) - settings.set("artists",dict()) database.open() scan(path) #count = str(database.count("files")) count = str(database.count("artist")) - write("Library contains "+count+" files.") + write("Library contains "+count+" artists.") + count = str(database.count("album")) + write("Library contains "+count+" albums.") database.close() diff --git a/src/core/tools/database.py b/src/core/tools/database.py index 7399bec0..83bf4489 100644 --- a/src/core/tools/database.py +++ b/src/core/tools/database.py @@ -56,7 +56,7 @@ def create(): CREATE TABLE library ( - libid INTEGER PRIMARY KEY AUTOINCREMENT, + id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE, path TEXT UNIQUE ); @@ -85,6 +85,13 @@ def close(): conn = None +def commit(): + global conn + if conn == None: + return + conn.commit() + + def init(): global db db = join(settings.get("ocarina"),"ocarina.db") @@ -119,6 +126,10 @@ def select(select,table,where=None): statement = 'select '+select+' from '+table if not (where==None): statement += ' where '+where - print statement - result = execute(statement) - return result \ No newline at end of file + #print statement + try: + result = execute(statement) + return result + except: + print statement + return None \ No newline at end of file