comparison sat/memory/sqlite.py @ 3120:0c29155ac68b

core: backend autoconnection: A new Connection/autoconnect_backend param can be set for a profile or component to be started automatically with backend. This is specially useful for components, but can be useful for client profile too (e.g. on Android we need to start profile with backend to get notifications, this part will come with following commits). The new Sqlite.getIndParamValues method allows to retrieve the same parameters for all profiles.
author Goffi <goffi@goffi.org>
date Sat, 25 Jan 2020 21:08:32 +0100
parents f8cc88c773c8
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3119:790489521b15 3120:0c29155ac68b
194 def __init__(self, db_filename, sat_version): 194 def __init__(self, db_filename, sat_version):
195 """Connect to the given database 195 """Connect to the given database
196 196
197 @param db_filename: full path to the Sqlite database 197 @param db_filename: full path to the Sqlite database
198 """ 198 """
199 self.initialized = defer.Deferred() # triggered when memory is fully initialised and ready 199 # triggered when memory is fully initialised and ready
200 self.profiles = {} # we keep cache for the profiles (key: profile name, value: profile id) 200 self.initialized = defer.Deferred()
201 # we keep cache for the profiles (key: profile name, value: profile id)
202 self.profiles = {}
201 203
202 log.info(_("Connecting database")) 204 log.info(_("Connecting database"))
203 new_base = not os.path.exists(db_filename) # do we have to create the database ? 205 new_base = not os.path.exists(db_filename) # do we have to create the database ?
204 if new_base: # the dir may not exist if it's not the XDG recommended one 206 if new_base: # the dir may not exist if it's not the XDG recommended one
205 dir_ = os.path.dirname(db_filename) 207 dir_ = os.path.dirname(db_filename)
229 231
230 init_defer.addCallback(lambda ignore: getNewBaseSql() if new_base else getUpdateSql()) 232 init_defer.addCallback(lambda ignore: getNewBaseSql() if new_base else getUpdateSql())
231 init_defer.addCallback(self.commitStatements) 233 init_defer.addCallback(self.commitStatements)
232 234
233 def fillProfileCache(ignore): 235 def fillProfileCache(ignore):
234 d = self.dbpool.runQuery("SELECT profile_id, entry_point FROM components").addCallback(self._cacheComponentsAndProfiles) 236 return self.dbpool.runQuery("SELECT profile_id, entry_point FROM components").addCallback(self._cacheComponentsAndProfiles)
235 d.chainDeferred(self.initialized)
236 237
237 init_defer.addCallback(fillProfileCache) 238 init_defer.addCallback(fillProfileCache)
239 init_defer.chainDeferred(self.initialized)
238 240
239 def commitStatements(self, statements): 241 def commitStatements(self, statements):
240 242
241 if statements is None: 243 if statements is None:
242 return defer.succeed(None) 244 return defer.succeed(None)
385 @param category: category of the parameter 387 @param category: category of the parameter
386 @param name: name of the parameter 388 @param name: name of the parameter
387 @param profile: %(doc_profile)s 389 @param profile: %(doc_profile)s
388 @return: deferred 390 @return: deferred
389 """ 391 """
390 d = self.dbpool.runQuery("SELECT value FROM param_ind WHERE category=? AND name=? AND profile_id=?", (category, name, self.profiles[profile])) 392 d = self.dbpool.runQuery(
393 "SELECT value FROM param_ind WHERE category=? AND name=? AND profile_id=?",
394 (category, name, self.profiles[profile]))
391 d.addCallback(self.__getFirstResult) 395 d.addCallback(self.__getFirstResult)
392 return d 396 return d
397
398 async def getIndParamValues(self, category, name):
399 """Ask database for the individual values of a parameter for all profiles
400
401 @param category: category of the parameter
402 @param name: name of the parameter
403 @return dict: profile => value map
404 """
405 result = await self.dbpool.runQuery(
406 "SELECT profiles.name, param_ind.value FROM param_ind JOIN profiles ON "
407 "param_ind.profile_id = profiles.id WHERE param_ind.category=? "
408 "and param_ind.name=?",
409 (category, name))
410 return dict(result)
393 411
394 def setGenParam(self, category, name, value): 412 def setGenParam(self, category, name, value):
395 """Save the general parameters in database 413 """Save the general parameters in database
396 414
397 @param category: category of the parameter 415 @param category: category of the parameter