diff src/tools/sqlite.py @ 413:dd4caab17008

core: - individual parameters managed through sqlite - new asyncGetParamA method, which allow to get parameter for a not connected profile ==> individual parameters are cached in memory when the profile is connected, but must be accessed though sqlite else, and that must be done asynchronously primitivus: - profile manager updated to use asyncGetParamA /!\ still broken, need more work before being able to run again
author Goffi <goffi@goffi.org>
date Tue, 01 Nov 2011 20:39:22 +0100
parents 62b17854254e
children 32dc8b18c2ae
line wrap: on
line diff
--- a/src/tools/sqlite.py	Sun Oct 30 23:13:40 2011 +0100
+++ b/src/tools/sqlite.py	Tue Nov 01 20:39:22 2011 +0100
@@ -98,7 +98,9 @@
         @param params_gen: dictionary to fill
         @return: deferred"""
         def fillParams(result):
-            params_gen[(category, name)] = value
+            for param in result:
+                category,name,value = param
+                params_gen[(category, name)] = value
         debug(_("loading general parameters from database")) 
         return self.dbpool.runQuery("SELECT category,name,value FROM param_gen").addCallback(fillParams)
 
@@ -108,12 +110,25 @@
         @param profile: a profile which *must* exist
         @return: deferred"""
         def fillParams(result):
-            params_ind[profile][(category, name)] = value
+            for param in result:
+                category,name,value = param
+                params_ind[profile][(category, name)] = value
         debug(_("loading individual parameters from database")) 
         d = self.dbpool.runQuery("SELECT category,name,value FROM param_gen WHERE profile_id=?", self.profiles[profile])
         d.addCallback(fillParams)
         return d
 
+    def getIndParam(self, category, name, profile):
+        """Ask database for the value of one specific individual parameter
+        @param category: category of the parameter
+        @param name: name of the parameter
+        @param profile: %(doc_profile)s
+        @return: deferred"""
+        d = self.dbpool.runQuery("SELECT value FROM param_ind WHERE category=? AND name=? AND profile_id=?", (category,name,self.profiles[profile]))
+        d.addCallback(self.__getFirstResult)
+        return d
+
+
     def setGenParam(self, category, name, value):
         """Save the general parameters in database
         @param category: category of the parameter
@@ -134,3 +149,10 @@
         d = self.dbpool.runQuery("REPLACE INTO param_ind(category,name,profile_id,value) VALUES (?,?,?,?)", (category, name, self.profiles[profile], value))
         d.addErrback(lambda ignore: error(_("Can't set individual parameter (%(category)s/%(name)s) for [%(profile)s] in database" % {"category":category, "name":name, "profile":profile})))
         return d
+
+    ##Helper methods##
+
+    def __getFirstResult(self, result):
+        """Return the first result of a database query
+        Useful when we are looking for one specific value"""
+        return "" if not result else result[0][0]