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

View File

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

View File

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

View File

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

View File

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