diff --git a/tiramisu/storage/sqlite3/setting.py b/tiramisu/storage/sqlite3/setting.py index 0dca195..d73d810 100644 --- a/tiramisu/storage/sqlite3/setting.py +++ b/tiramisu/storage/sqlite3/setting.py @@ -27,52 +27,72 @@ class Properties(Sqlite3DB): # properties def setproperties(self, path, index, properties): - path = self._sqlite_encode_path(path) - self._storage.execute("DELETE FROM property WHERE path = ? AND tiram_index = ? AND session_id = ?", - (path, index, self._session_id), - False) + self.exportation() + self.delproperties(path, index, commit=False) + self.exportation() self._storage.execute("INSERT INTO property(path, tiram_index, properties, session_id) VALUES " "(?, ?, ?, ?)", (path, index, self._sqlite_encode(properties), self._session_id)) + self.exportation() def getproperties(self, path, index, default_properties): - path = self._sqlite_encode_path(path) - value = self._storage.select("SELECT properties FROM property WHERE " - "path = ? AND tiram_index = ? AND session_id = ? LIMIT 1", (path, index, self._session_id)) + sql = 'SELECT properties FROM property WHERE session_id = ? ' + params = [self._session_id] + if path is None: + sql += "AND path is NULL " + else: + sql += "AND path = ? " + params.append(path) + if index is None: + sql += "AND tiram_index is NULL LIMIT 1" + else: + sql += "AND tiram_index = ? LIMIT 1" + params.append(index) + value = self._storage.select(sql, params) if value is None: return set(default_properties) else: return set(self._sqlite_decode(value[0])) - def delproperties(self, path, index): - path = self._sqlite_encode_path(path) - self._storage.execute("DELETE FROM property WHERE path = ? AND tiram_index = ? AND session_id = ?", - (path, index, self._session_id)) + def delproperties(self, path, index, commit=True): + sql = 'DELETE FROM property WHERE session_id = ? ' + params = [self._session_id] + if path is None: + sql += 'AND path is NULL ' + else: + sql += 'AND path = ? ' + params.append(path) + if index is None: + sql += 'AND tiram_index is NULL' + else: + params.append(index) + sql += 'AND tiram_index = ?' + self._storage.execute(sql, params, commit) def exportation(self): """return all modified settings in a dictionary example: {'path1': set(['prop1', 'prop2'])} """ ret = {} - for path, properties, _ in self._storage.select("SELECT * FROM property " - "WHERE session_id = ?", - (self._session_id,), - only_one=False): - path = self._sqlite_decode_path(path) - ret[path] = self._sqlite_decode(properties) + for path, tiram_index, properties, _ in self._storage.select("SELECT * FROM property " + "WHERE session_id = ?", + (self._session_id,), + only_one=False): + ret.setdefault(path, {})[tiram_index] = self._sqlite_decode(properties) return ret def importation(self, properties): self._storage.execute("DELETE FROM property WHERE session_id = ?", (self._session_id,), commit=False) - for path, property_ in properties.items(): - path = self._sqlite_encode_path(path) - self._storage.execute("INSERT INTO property(path, properties, session_id) " - "VALUES (?, ?, ?)", (path, - self._sqlite_encode(property_), - self._session_id, - ), False) + for path, indexed_properties in properties.items(): + for index, property_ in indexed_properties.items(): + self._storage.execute("INSERT INTO property(path, tiram_index, properties, session_id) " + "VALUES (?, ?, ?, ?)", (path, + index, + self._sqlite_encode(property_), + self._session_id, + ), False) self._storage._conn.commit() @@ -81,24 +101,28 @@ class Permissives(Sqlite3DB): # permissive def setpermissives(self, path, index, permissive): - path = self._sqlite_encode_path(path) log.debug('setpermissive %s %s %s %s', path, index, permissive, id(self)) - self._storage.execute("DELETE FROM permissive WHERE path = ? AND tiram_index = ? AND session_id = ?", - (path, - index, - self._session_id), - False) + self.delpermissive(path, index, commit=False) self._storage.execute("INSERT INTO permissive(path, tiram_index, permissives, session_id) " "VALUES (?, ?, ?, ?)", (path, - index, - self._sqlite_encode(permissive), - self._session_id)) + index, + self._sqlite_encode(permissive), + self._session_id)) def getpermissives(self, path, index): - path = self._sqlite_encode_path(path) - permissives = self._storage.select("SELECT permissives FROM " - "permissive WHERE path = ? AND tiram_index = ? AND session_id = ? LIMIT 1", - (path, index, self._session_id)) + sql = 'SELECT permissives FROM permissive WHERE session_id = ? ' + params = [self._session_id] + if path is None: + sql += "AND path is NULL " + else: + sql += "AND path = ? " + params.append(path) + if index is None: + sql += "AND tiram_index is NULL LIMIT 1" + else: + sql += "AND tiram_index = ? LIMIT 1" + params.append(index) + permissives = self._storage.select(sql, params) if permissives is None: ret = frozenset() else: @@ -106,10 +130,20 @@ class Permissives(Sqlite3DB): log.debug('getpermissive %s %s %s', path, ret, id(self)) return ret - def delpermissive(self, path, index): - path = self._sqlite_encode_path(path) - self._storage.execute("DELETE FROM permissive WHERE path = ? AND tiram_index = ? AND session_id = ?", - (path, index, self._session_id)) + def delpermissive(self, path, index, commit=True): + sql = 'DELETE FROM permissive WHERE session_id = ? ' + params = [self._session_id] + if path is None: + sql += 'AND path is NULL ' + else: + sql += 'AND path = ? ' + params.append(path) + if index is None: + sql += 'AND tiram_index is NULL' + else: + params.append(index) + sql += 'AND tiram_index = ?' + self._storage.execute(sql, params, commit) def exportation(self): """return all modified permissives in a dictionary @@ -120,19 +154,18 @@ class Permissives(Sqlite3DB): "WHERE session_id = ?", (self._session_id,), only_one=False): - path = self._sqlite_decode_path(path) - ret[path] = self._sqlite_decode(permissives) + ret.setdefault(path, {})[index] = self._sqlite_decode(permissives) return ret def importation(self, permissives): self._storage.execute("DELETE FROM permissive WHERE session_id = ?", (self._session_id,), commit=False) - for path, permissive in permissives.items(): - path = self._sqlite_encode_path(path) - self._storage.execute("INSERT INTO permissive(path, tiram_index, permissives, session_id) " - "VALUES (?, ?, ?)", (path, - index, - self._sqlite_encode(permissive), - self._session_id, - ), False) + for path, indexed_permissives in permissives.items(): + for index, permissive in indexed_permissives.items(): + self._storage.execute("INSERT INTO permissive(path, tiram_index, permissives, session_id) " + "VALUES (?, ?, ?, ?)", (path, + index, + self._sqlite_encode(permissive), + self._session_id, + ), False) self._storage._conn.commit()