diff options
Diffstat (limited to 'module/database/DatabaseBackend.py')
-rw-r--r-- | module/database/DatabaseBackend.py | 123 |
1 files changed, 38 insertions, 85 deletions
diff --git a/module/database/DatabaseBackend.py b/module/database/DatabaseBackend.py index 9530390c3..df2660063 100644 --- a/module/database/DatabaseBackend.py +++ b/module/database/DatabaseBackend.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python """ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -34,13 +33,13 @@ except: DB_VERSION = 4 -class style(): +class style: db = None - + @classmethod def setDB(cls, db): cls.db = db - + @classmethod def inner(cls, f): @staticmethod @@ -48,7 +47,7 @@ class style(): if cls.db: return f(cls.db, *args, **kwargs) return x - + @classmethod def queue(cls, f): @staticmethod @@ -56,7 +55,7 @@ class style(): if cls.db: return cls.db.queue(f, *args, **kwargs) return x - + @classmethod def async(cls, f): @staticmethod @@ -65,14 +64,14 @@ class style(): return cls.db.async(f, *args, **kwargs) return x -class DatabaseJob(): +class DatabaseJob: def __init__(self, f, *args, **kwargs): self.done = Event() - + self.f = f self.args = args self.kwargs = kwargs - + self.result = None self.exception = False @@ -104,7 +103,7 @@ class DatabaseJob(): self.exception = e finally: self.done.set() - + def wait(self): self.done.wait() @@ -116,34 +115,34 @@ class DatabaseBackend(Thread): self.core = core self.jobs = Queue() - + self.setuplock = Event() - + style.setDB(self) - + def setup(self): self.start() self.setuplock.wait() - + def run(self): """main loop, which executes commands""" convert = self._checkVersion() #returns None or current version - + self.conn = sqlite3.connect("files.db") chmod("files.db", 0600) self.c = self.conn.cursor() #compatibility - + if convert is not None: self._convertDB(convert) - + self._createTables() self._migrateUser() self.conn.commit() - + self.setuplock.set() - + while True: j = self.jobs.get() if j == "quit": @@ -164,7 +163,7 @@ class DatabaseBackend(Thread): f.write(str(DB_VERSION)) f.close() return - + f = open("files.version", "rb") v = int(f.read().strip()) f.close() @@ -180,7 +179,7 @@ class DatabaseBackend(Thread): f.write(str(DB_VERSION)) f.close() return v - + def _convertDB(self, v): try: getattr(self, "_convertV%i" % v)() @@ -189,9 +188,9 @@ class DatabaseBackend(Thread): self.core.log.error(_("Filedatabase could NOT be converted.")) except: print "Filedatabase could NOT be converted." - - #--convert scripts start - + + #convert scripts start----------------------------------------------------- + def _convertV2(self): self.c.execute('CREATE TABLE IF NOT EXISTS "storage" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "identifier" TEXT NOT NULL, "key" TEXT NOT NULL, "value" TEXT DEFAULT "")') try: @@ -199,16 +198,16 @@ class DatabaseBackend(Thread): except: print "Database was converted from v2 to v3." self._convertV3() - + def _convertV3(self): self.c.execute('CREATE TABLE IF NOT EXISTS "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "name" TEXT NOT NULL, "email" TEXT DEFAULT "" NOT NULL, "password" TEXT NOT NULL, "role" INTEGER DEFAULT 0 NOT NULL, "permission" INTEGER DEFAULT 0 NOT NULL, "template" TEXT DEFAULT "default" NOT NULL)') try: self.manager.core.log.info(_("Database was converted from v3 to v4.")) except: print "Database was converted from v3 to v4." - - #--convert scripts end - + + #convert scripts end------------------------------------------------------- + def _createTables(self): """create tables for database""" @@ -222,7 +221,7 @@ class DatabaseBackend(Thread): SELECT p.id AS id, SUM(l.size) AS sizetotal, COUNT(l.id) AS linkstotal, linksdone, sizedone\ FROM packages p JOIN links l ON p.id = l.package LEFT OUTER JOIN\ (SELECT p.id AS id, COUNT(*) AS linksdone, SUM(l.size) AS sizedone \ - FROM packages p JOIN links l ON p.id = l.package AND l.status in (0,4,13) GROUP BY p.id) s ON s.id = p.id \ + FROM packages p JOIN links l ON p.id = l.package AND l.status in (0, 4, 13) GROUP BY p.id) s ON s.id = p.id \ GROUP BY p.id') #try to lower ids @@ -264,10 +263,10 @@ class DatabaseBackend(Thread): self.c.executemany("INSERT INTO users(name, password, email) VALUES (?, ?, ?)", users) move("pyload.db", "pyload.old.db") - + def createCursor(self): return self.conn.cursor() - + @style.async def commit(self): self.conn.commit() @@ -275,78 +274,32 @@ class DatabaseBackend(Thread): @style.queue def syncSave(self): self.conn.commit() - + @style.async def rollback(self): self.conn.rollback() - + def async(self, f, *args, **kwargs): - args = (self, ) + args + args = (self,) + args job = DatabaseJob(f, *args, **kwargs) self.jobs.put(job) - + def queue(self, f, *args, **kwargs): - args = (self, ) + args + args = (self,) + args job = DatabaseJob(f, *args, **kwargs) self.jobs.put(job) job.wait() return job.result - + @classmethod def registerSub(cls, klass): cls.subs.append(klass) - + @classmethod def unregisterSub(cls, klass): cls.subs.remove(klass) - + def __getattr__(self, attr): for sub in DatabaseBackend.subs: if hasattr(sub, attr): return getattr(sub, attr) - -if __name__ == "__main__": - db = DatabaseBackend() - db.setup() - - class Test(): - @style.queue - def insert(db): - c = db.createCursor() - for i in range(1000): - c.execute("INSERT INTO storage (identifier, key, value) VALUES (?, ?, ?)", ("foo", i, "bar")) - @style.async - def insert2(db): - c = db.createCursor() - for i in range(1000*1000): - c.execute("INSERT INTO storage (identifier, key, value) VALUES (?, ?, ?)", ("foo", i, "bar")) - - @style.queue - def select(db): - c = db.createCursor() - for i in range(10): - res = c.execute("SELECT value FROM storage WHERE identifier=? AND key=?", ("foo", i)) - print res.fetchone() - - @style.queue - def error(db): - c = db.createCursor() - print "a" - c.execute("SELECT myerror FROM storage WHERE identifier=? AND key=?", ("foo", i)) - print "e" - - db.registerSub(Test) - from time import time - start = time() - for i in range(100): - db.insert() - end = time() - print end-start - - start = time() - db.insert2() - end = time() - print end-start - - db.error() - |