update sqlite3 storage

This commit is contained in:
Emmanuel Garette 2018-09-07 08:42:14 +02:00
parent 05404e89bc
commit e0cab0063e
5 changed files with 48 additions and 27 deletions

View File

@ -70,8 +70,9 @@ class Properties(Sqlite3DB):
return ret return ret
def importation(self, properties): def importation(self, properties):
self._storage.execute("DELETE FROM property", commit=False) self._storage.execute("DELETE FROM property WHERE session_id = ?", (self._session_id,), commit=False)
for path, property_ in properties.items(): for path, property_ in properties.items():
path = self._sqlite_encode_path(path)
self._storage.execute("INSERT INTO property(path, properties, session_id) " self._storage.execute("INSERT INTO property(path, properties, session_id) "
"VALUES (?, ?, ?)", (path, "VALUES (?, ?, ?)", (path,
self._sqlite_encode(property_), self._sqlite_encode(property_),
@ -84,7 +85,7 @@ class Permissives(Sqlite3DB):
__slots__ = tuple() __slots__ = tuple()
# permissive # permissive
def setpermissive(self, path, permissive): def setpermissives(self, path, permissive):
path = self._sqlite_encode_path(path) path = self._sqlite_encode_path(path)
if DEBUG: # pragma: no cover if DEBUG: # pragma: no cover
print('setpermissive', path, permissive, id(self)) print('setpermissive', path, permissive, id(self))
@ -96,7 +97,7 @@ class Permissives(Sqlite3DB):
self._sqlite_encode(permissive), self._sqlite_encode(permissive),
self._session_id)) self._session_id))
def getpermissive(self, path='_none'): def getpermissives(self, path='_none'):
path = self._sqlite_encode_path(path) path = self._sqlite_encode_path(path)
permissives = self._storage.select("SELECT permissives FROM " permissives = self._storage.select("SELECT permissives FROM "
"permissive WHERE path = ? AND session_id = ? LIMIT 1", "permissive WHERE path = ? AND session_id = ? LIMIT 1",
@ -123,8 +124,10 @@ class Permissives(Sqlite3DB):
return ret return ret
def importation(self, permissives): def importation(self, permissives):
self._storage.execute("DELETE FROM permissive", commit=False) self._storage.execute("DELETE FROM permissive WHERE session_id = ?", (self._session_id,),
commit=False)
for path, permissive in permissives.items(): for path, permissive in permissives.items():
path = self._sqlite_encode_path(path)
self._storage.execute("INSERT INTO permissive(path, permissives, session_id) " self._storage.execute("INSERT INTO permissive(path, permissives, session_id) "
"VALUES (?, ?, ?)", (path, "VALUES (?, ?, ?)", (path,
self._sqlite_encode(permissive), self._sqlite_encode(permissive),

View File

@ -16,10 +16,12 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# ____________________________________________________________ # ____________________________________________________________
from ...i18n import _
from os import unlink from os import unlink
from os.path import basename, splitext, join, isfile from os.path import basename, splitext, join, isfile
import sqlite3 import sqlite3
from glob import glob from glob import glob
from ...error import ConflictError
class Setting(object): class Setting(object):
@ -40,32 +42,34 @@ def _gen_filename():
def list_sessions(): def list_sessions():
cursor = CONN.cursor() cursor = CONN.cursor()
names = [row[0] for row in cursor.execute("SELECT DISTINCT session_id FROM value").fetchall()] names = [row[0] for row in cursor.execute("SELECT session FROM session").fetchall()]
return names return names
def delete_session(session_id, session): def delete_session(session_id, _session_id=None):
cursor = CONN.cursor() cursor = CONN.cursor()
cursor.execute("DELETE FROM property WHERE session_id = ?", if _session_id is None:
(session_id,)) _session_id = cursor.execute("SELECT session_id FROM session WHERE session = ?",
cursor.execute("DELETE FROM permissive WHERE session_id = ?", (session_id,)).fetchone()
(session_id,)) if _session_id is not None:
cursor.execute("DELETE FROM value WHERE session_id = ?", _session_id = _session_id[0]
(session_id,)) if _session_id is not None:
cursor.execute("DELETE FROM information WHERE session_id = ?", cursor.execute("DELETE FROM property WHERE session_id = ?", (_session_id,))
(session_id,)) cursor.execute("DELETE FROM permissive WHERE session_id = ?", (_session_id,))
CONN.commit() cursor.execute("DELETE FROM value WHERE session_id = ?", (_session_id,))
cursor.execute("DELETE FROM information WHERE session_id = ?", (_session_id,))
cursor.execute("DELETE FROM session WHERE session_id = ?", (_session_id,))
CONN.commit()
global CONN global CONN
CONN = None CONN = None
class Storage(object): class Storage(object):
__slots__ = ('_conn', '_cursor', 'persistent', 'session_id') __slots__ = ('_conn', '_cursor', 'persistent', 'session_id', 'session_name')
storage = 'sqlite3' storage = 'sqlite3'
def __init__(self, session_id, persistent, test=False): def __init__(self, session_id, persistent, test=False):
self.persistent = persistent self.persistent = persistent
self.session_id = session_id
global CONN global CONN
init = False init = False
if CONN is None: if CONN is None:
@ -74,21 +78,33 @@ class Storage(object):
CONN.text_factory = str CONN.text_factory = str
self._conn = CONN self._conn = CONN
self._cursor = self._conn.cursor() self._cursor = self._conn.cursor()
self.session_name = session_id
if init: if init:
session_table = 'CREATE TABLE IF NOT EXISTS session(session_id INTEGER, session TEXT UNIQUE, PRIMARY KEY(session_id))'
settings_table = 'CREATE TABLE IF NOT EXISTS property(path TEXT,' settings_table = 'CREATE TABLE IF NOT EXISTS property(path TEXT,'
settings_table += 'properties text, session_id TEXT, PRIMARY KEY(path, session_id))' settings_table += 'properties text, session_id INTEGER, PRIMARY KEY(path, session_id), '
settings_table += 'FOREIGN KEY(session_id) REFERENCES session(session_id))'
permissives_table = 'CREATE TABLE IF NOT EXISTS permissive(path TEXT,' permissives_table = 'CREATE TABLE IF NOT EXISTS permissive(path TEXT,'
permissives_table += 'permissives TEXT, session_id TEXT, PRIMARY KEY(path, session_id))' permissives_table += 'permissives TEXT, session_id INTEGER, PRIMARY KEY(path, session_id), '
permissives_table += 'FOREIGN KEY(session_id) REFERENCES session(session_id))'
values_table = 'CREATE TABLE IF NOT EXISTS value(path TEXT, ' values_table = 'CREATE TABLE IF NOT EXISTS value(path TEXT, '
values_table += 'value TEXT, owner TEXT, idx INTEGER, session_id TEXT NOT NULL, '\ values_table += 'value TEXT, owner TEXT, idx INTEGER, session_id INTEGER, '\
'PRIMARY KEY (path, idx, session_id))' 'PRIMARY KEY (path, idx, session_id), '
values_table += 'FOREIGN KEY(session_id) REFERENCES session(session_id))'
informations_table = 'CREATE TABLE IF NOT EXISTS information(key TEXT,' informations_table = 'CREATE TABLE IF NOT EXISTS information(key TEXT,'
informations_table += 'value TEXT, session_id TEXT NOT NULL, ' informations_table += 'value TEXT, session_id INTEGER, '
informations_table += 'PRIMARY KEY (key, session_id))' informations_table += 'PRIMARY KEY (key, session_id), '
informations_table += 'FOREIGN KEY(session_id) REFERENCES session(session_id))'
self.execute(session_table, commit=False)
self.execute(values_table, commit=False) self.execute(values_table, commit=False)
self.execute(informations_table, commit=False) self.execute(informations_table, commit=False)
self.execute(settings_table, commit=False) self.execute(settings_table, commit=False)
self.execute(permissives_table) self.execute(permissives_table, commit=False)
try:
self.execute('INSERT INTO session(session) VALUES (?)', (session_id,))
except sqlite3.IntegrityError:
raise ConflictError(_('session "{}" already used').format(session_id))
self.session_id = self._cursor.lastrowid
def commit(self): def commit(self):
self._conn.commit() self._conn.commit()
@ -113,9 +129,10 @@ class Storage(object):
#FIXME #FIXME
#self._conn.close() #self._conn.close()
if not self.persistent: if not self.persistent:
session = None
if delete_session is not None: if delete_session is not None:
delete_session(self.session_id, session) session_id = getattr(self, 'session_id', None)
delete_session(self.session_name,
session_id)
def getsession(): def getsession():

View File

@ -263,6 +263,7 @@ class Values(Sqlite3DB):
self._storage.execute("DELETE FROM value WHERE session_id = ?", (self._session_id,), self._storage.execute("DELETE FROM value WHERE session_id = ?", (self._session_id,),
commit=False) commit=False)
for idx, path in enumerate(export[0]): for idx, path in enumerate(export[0]):
path = self._sqlite_encode_path(path)
index = export[1][idx] index = export[1][idx]
value = export[2][idx] value = export[2][idx]
owner = export[3][idx] owner = export[3][idx]

View File

@ -16,7 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# ____________________________________________________________ # ____________________________________________________________
from time import time from time import time
from .dictionary.cache import Cache as DictCache from .cache.dictionary import Cache as DictCache
def _display_classname(obj): def _display_classname(obj):