some corrections in sqlite3 storage

This commit is contained in:
Emmanuel Garette 2013-08-21 23:21:28 +02:00
parent b6bb685ca5
commit 1ddd88fc99
5 changed files with 38 additions and 17 deletions

View File

@ -191,7 +191,7 @@ class Settings(object):
:param context: the root config :param context: the root config
:param storage: the storage type :param storage: the storage type
- dictionnary -> in memory - dictionary -> in memory
- sqlite3 -> persistent - sqlite3 -> persistent
""" """
# generic owner # generic owner
@ -271,7 +271,7 @@ class Settings(object):
(never save properties if same has option properties) (never save properties if same has option properties)
""" """
if opt is None: if opt is None:
self._p_.setproperties(path, properties) self._p_.setproperties(None, properties)
else: else:
if set(opt._properties) == properties: if set(opt._properties) == properties:
self._p_.reset_properties(path) self._p_.reset_properties(path)
@ -380,7 +380,6 @@ class Settings(object):
# filters the callbacks # filters the callbacks
setting = Property(self, self._getproperties(opt, path, False), opt, path=path) setting = Property(self, self._getproperties(opt, path, False), opt, path=path)
descr = self.context.cfgimpl_get_description()
for requires in opt._requires: for requires in opt._requires:
matches = False matches = False
for require in requires: for require in requires:

View File

@ -35,6 +35,7 @@ class Settings(Cache):
# propertives # propertives
def setproperties(self, path, properties): def setproperties(self, path, properties):
path = self._sqlite_encode_path(path)
self.storage.execute("DELETE FROM property WHERE path = ?", (path,), self.storage.execute("DELETE FROM property WHERE path = ?", (path,),
False) False)
self.storage.execute("INSERT INTO property(path, properties) VALUES " self.storage.execute("INSERT INTO property(path, properties) VALUES "
@ -42,6 +43,7 @@ class Settings(Cache):
self._sqlite_encode(properties))) self._sqlite_encode(properties)))
def getproperties(self, path, default_properties): def getproperties(self, path, default_properties):
path = self._sqlite_encode_path(path)
value = self.storage.select("SELECT properties FROM property WHERE " value = self.storage.select("SELECT properties FROM property WHERE "
"path = ?", (path,)) "path = ?", (path,))
if value is None: if value is None:
@ -50,6 +52,7 @@ class Settings(Cache):
return set(self._sqlite_decode(value[0])) return set(self._sqlite_decode(value[0]))
def hasproperties(self, path): def hasproperties(self, path):
path = self._sqlite_encode_path(path)
return self.storage.select("SELECT properties FROM property WHERE " return self.storage.select("SELECT properties FROM property WHERE "
"path = ?", (path,)) is not None "path = ?", (path,)) is not None
@ -57,6 +60,7 @@ class Settings(Cache):
self.storage.execute("DELETE FROM property") self.storage.execute("DELETE FROM property")
def reset_properties(self, path): def reset_properties(self, path):
path = self._sqlite_encode_path(path)
self.storage.execute("DELETE FROM property WHERE path = ?", (path,)) self.storage.execute("DELETE FROM property WHERE path = ?", (path,))
def get_properties(self, context): def get_properties(self, context):
@ -65,17 +69,14 @@ class Settings(Cache):
ret = {} ret = {}
for path, properties in self.storage.select("SELECT * FROM property", for path, properties in self.storage.select("SELECT * FROM property",
only_one=False): only_one=False):
if path == '_none': path = self._sqlite_decode_path(path)
opt = None
else:
opt = context.cfgimpl_get_description().impl_get_opt_by_path(
path)
properties = self._sqlite_decode(properties) properties = self._sqlite_decode(properties)
ret[opt] = properties ret[path] = properties
return ret return ret
# permissive # permissive
def setpermissive(self, path, permissive): def setpermissive(self, path, permissive):
path = self._sqlite_encode_path(path)
self.storage.execute("DELETE FROM permissive WHERE path = ?", (path,), self.storage.execute("DELETE FROM permissive WHERE path = ?", (path,),
False) False)
self.storage.execute("INSERT INTO permissive(path, permissives) " self.storage.execute("INSERT INTO permissive(path, permissives) "

View File

@ -66,6 +66,18 @@ class Cache(object):
self.storage.execute(cache_table) self.storage.execute(cache_table)
# value # value
def _sqlite_decode_path(self, path):
if path == '_none':
return None
else:
return path
def _sqlite_encode_path(self, path):
if path is None:
return '_none'
else:
return path
def _sqlite_decode(self, value): def _sqlite_decode(self, value):
return loads(value) return loads(value)
@ -76,6 +88,7 @@ class Cache(object):
def setcache(self, cache_type, path, val, time): def setcache(self, cache_type, path, val, time):
convert_value = self._sqlite_encode(val) convert_value = self._sqlite_encode(val)
path = self._sqlite_encode_path(path)
self.storage.execute("DELETE FROM cache_{0} WHERE path = ?".format( self.storage.execute("DELETE FROM cache_{0} WHERE path = ?".format(
cache_type), (path,), False) cache_type), (path,), False)
self.storage.execute("INSERT INTO cache_{0}(path, value, time) " self.storage.execute("INSERT INTO cache_{0}(path, value, time) "
@ -83,6 +96,7 @@ class Cache(object):
(path, convert_value, time)) (path, convert_value, time))
def getcache(self, cache_type, path, exp): def getcache(self, cache_type, path, exp):
path = self._sqlite_encode_path(path)
cached = self.storage.select("SELECT value FROM cache_{0} WHERE " cached = self.storage.select("SELECT value FROM cache_{0} WHERE "
"path = ? AND time >= ?".format( "path = ? AND time >= ?".format(
cache_type), (path, exp)) cache_type), (path, exp))
@ -92,6 +106,7 @@ class Cache(object):
return True, self._sqlite_decode(cached[0]) return True, self._sqlite_decode(cached[0])
def hascache(self, cache_type, path): def hascache(self, cache_type, path):
path = self._sqlite_encode_path(path)
return self.storage.select("SELECT value FROM cache_{0} WHERE " return self.storage.select("SELECT value FROM cache_{0} WHERE "
"path = ?".format(cache_type), "path = ?".format(cache_type),
(path,)) is not None (path,)) is not None
@ -105,13 +120,13 @@ class Cache(object):
def get_cached(self, cache_type, context): def get_cached(self, cache_type, context):
"""return all values in a dictionary """return all values in a dictionary
example: {option1: ('value1', 'time1'), option2: ('value2', 'time2')} example: {'path1': ('value1', 'time1'), 'path2': ('value2', 'time2')}
""" """
ret = {} ret = {}
for path, value, time in self.storage.select("SELECT * FROM cache_{0}" for path, value, time in self.storage.select("SELECT * FROM cache_{0}"
"".format(cache_type), "".format(cache_type),
only_one=False): only_one=False):
opt = context.cfgimpl_get_description().impl_get_opt_by_path(path) path = self._sqlite_decode_path(path)
value = self._sqlite_decode(value) value = self._sqlite_decode(value)
ret[opt] = (value, time) ret[path] = (value, time)
return ret return ret

View File

@ -45,6 +45,7 @@ class Values(Cache):
a specified value must be associated to an owner a specified value must be associated to an owner
""" """
self.resetvalue(path) self.resetvalue(path)
path = self._sqlite_encode_path(path)
self.storage.execute("INSERT INTO value(path, value, owner) VALUES " self.storage.execute("INSERT INTO value(path, value, owner) VALUES "
"(?, ?, ?)", (path, self._sqlite_encode(value), "(?, ?, ?)", (path, self._sqlite_encode(value),
str(owner))) str(owner)))
@ -53,17 +54,20 @@ class Values(Cache):
"""get value for an option """get value for an option
return: only value, not the owner return: only value, not the owner
""" """
path = self._sqlite_encode_path(path)
return self._sqlite_decode(self._sqlite_select(path)[0]) return self._sqlite_decode(self._sqlite_select(path)[0])
def hasvalue(self, path): def hasvalue(self, path):
"""if opt has a value """if opt has a value
return: boolean return: boolean
""" """
path = self._sqlite_encode_path(path)
return self._sqlite_select(path) is not None return self._sqlite_select(path) is not None
def resetvalue(self, path): def resetvalue(self, path):
"""remove value means delete value in storage """remove value means delete value in storage
""" """
path = self._sqlite_encode_path(path)
self.storage.execute("DELETE FROM value WHERE path = ?", (path,)) self.storage.execute("DELETE FROM value WHERE path = ?", (path,))
def get_modified_values(self, context): def get_modified_values(self, context):
@ -73,17 +77,18 @@ class Values(Cache):
ret = {} ret = {}
for path, value, owner in self.storage.select("SELECT value", for path, value, owner in self.storage.select("SELECT value",
only_one=False): only_one=False):
opt = context.cfgimpl_get_description().impl_get_opt_by_path(path) path = self._sqlite_decode_path(path)
owner = getattr(owners, owner) owner = getattr(owners, owner)
value = self._sqlite_decode(value) value = self._sqlite_decode(value)
ret[opt] = (owner, value) ret[path] = (owner, value)
return ret return ret
# owner # owner
def setowner(self, path, owner): def setowner(self, path, owner):
"""change owner for an option """change owner for an option
""" """
path = self._sqlite_encode_path(path)
self.storage.execute("UPDATE value SET owner = ? WHERE path = ?", self.storage.execute("UPDATE value SET owner = ? WHERE path = ?",
(str(owner), path)) (str(owner), path))
@ -91,6 +96,7 @@ class Values(Cache):
"""get owner for an option """get owner for an option
return: owner object return: owner object
""" """
path = self._sqlite_encode_path(path)
owner = self.storage.select("SELECT owner FROM value WHERE path = ?", owner = self.storage.select("SELECT owner FROM value WHERE path = ?",
(path,)) (path,))
if owner is None: if owner is None:

View File

@ -376,11 +376,11 @@ class Multi(list):
value_slave.append(slave.impl_getdefault_multi(), value_slave.append(slave.impl_getdefault_multi(),
force=True) force=True)
def __setitem__(self, path, value): def __setitem__(self, key, value):
self._validate(value) self._validate(value)
#assume not checking mandatory property #assume not checking mandatory property
super(Multi, self).__setitem__(path, value) super(Multi, self).__setitem__(key, value)
self.context.cfgimpl_get_values()._setvalue(self.opt, path, self) self.context.cfgimpl_get_values()._setvalue(self.opt, self.path, self)
def append(self, value, force=False): def append(self, value, force=False):
"""the list value can be updated (appened) """the list value can be updated (appened)