Files
tiramisu/tiramisu/storage/sqlite3/storage.py

148 lines
5.9 KiB
Python
Raw Normal View History

2013-08-19 11:01:21 +02:00
# -*- coding: utf-8 -*-
"default plugin for cache: set it in a simple dictionary"
2018-01-26 07:33:47 +01:00
# Copyright (C) 2013-2018 Team tiramisu (see AUTHORS for all contributors)
2013-08-19 11:01:21 +02:00
#
2013-09-22 22:33:09 +02:00
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
2013-08-19 11:01:21 +02:00
#
2013-09-22 22:33:09 +02:00
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
2013-08-19 11:01:21 +02:00
#
2013-09-22 22:33:09 +02:00
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2013-08-19 11:01:21 +02:00
# ____________________________________________________________
2018-09-07 08:42:14 +02:00
from ...i18n import _
2013-08-20 22:45:11 +02:00
from os import unlink
2017-07-04 19:59:42 +02:00
from os.path import basename, splitext, join, isfile
2013-08-19 11:01:21 +02:00
import sqlite3
from glob import glob
2018-09-07 08:42:14 +02:00
from ...error import ConflictError
2017-07-21 18:46:11 +02:00
class Setting(object):
2013-09-10 21:04:12 +02:00
""":param extension: database file extension (by default: db)
:param dir_database: root database directory (by default: /tmp)
"""
extension = 'db'
dir_database = '/tmp'
2017-07-04 19:59:42 +02:00
name = 'tiramisu'
2017-07-04 19:59:42 +02:00
SETTING = Setting()
2017-07-04 19:59:42 +02:00
def _gen_filename():
return join(SETTING.dir_database, '{0}.{1}'.format(SETTING.name, SETTING.extension))
def list_sessions():
2017-07-11 22:31:58 +02:00
cursor = CONN.cursor()
2018-09-07 08:42:14 +02:00
names = [row[0] for row in cursor.execute("SELECT session FROM session").fetchall()]
return names
2018-09-11 20:11:13 +02:00
def delete_session(session_id,
_session_id=None):
2017-07-11 22:31:58 +02:00
cursor = CONN.cursor()
2018-09-07 08:42:14 +02:00
if _session_id is None:
_session_id = cursor.execute("SELECT session_id FROM session WHERE session = ?",
(session_id,)).fetchone()
if _session_id is not None:
_session_id = _session_id[0]
if _session_id is not None:
cursor.execute("DELETE FROM property WHERE session_id = ?", (_session_id,))
cursor.execute("DELETE FROM permissive WHERE session_id = ?", (_session_id,))
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()
2018-09-11 20:11:13 +02:00
cursor.close()
2013-08-19 11:01:21 +02:00
2017-07-11 22:31:58 +02:00
global CONN
CONN = None
2013-08-19 11:01:21 +02:00
2013-08-20 22:45:11 +02:00
class Storage(object):
2018-09-07 08:42:14 +02:00
__slots__ = ('_conn', '_cursor', 'persistent', 'session_id', 'session_name')
storage = 'sqlite3'
2013-08-19 11:01:21 +02:00
2013-09-22 20:57:52 +02:00
def __init__(self, session_id, persistent, test=False):
2013-08-27 09:46:52 +02:00
self.persistent = persistent
2017-07-11 22:31:58 +02:00
global CONN
init = False
if CONN is None:
init = True
CONN = sqlite3.connect(_gen_filename())
CONN.text_factory = str
self._conn = CONN
2013-08-19 11:01:21 +02:00
self._cursor = self._conn.cursor()
2018-09-07 08:42:14 +02:00
self.session_name = session_id
2017-07-11 22:31:58 +02:00
if init:
2018-09-11 20:11:13 +02:00
session_table = 'CREATE TABLE IF NOT EXISTS session(session_id INTEGER, '
session_table += 'session TEXT UNIQUE, persistent BOOL, PRIMARY KEY(session_id))'
2017-07-11 22:31:58 +02:00
settings_table = 'CREATE TABLE IF NOT EXISTS property(path TEXT,'
2018-09-07 08:42:14 +02:00
settings_table += 'properties text, session_id INTEGER, PRIMARY KEY(path, session_id), '
settings_table += 'FOREIGN KEY(session_id) REFERENCES session(session_id))'
2017-07-11 22:31:58 +02:00
permissives_table = 'CREATE TABLE IF NOT EXISTS permissive(path TEXT,'
2018-09-07 08:42:14 +02:00
permissives_table += 'permissives TEXT, session_id INTEGER, PRIMARY KEY(path, session_id), '
permissives_table += 'FOREIGN KEY(session_id) REFERENCES session(session_id))'
2017-07-11 22:31:58 +02:00
values_table = 'CREATE TABLE IF NOT EXISTS value(path TEXT, '
2018-09-07 08:42:14 +02:00
values_table += 'value TEXT, owner TEXT, idx INTEGER, session_id INTEGER, '\
'PRIMARY KEY (path, idx, session_id), '
values_table += 'FOREIGN KEY(session_id) REFERENCES session(session_id))'
2017-09-17 15:55:32 +02:00
informations_table = 'CREATE TABLE IF NOT EXISTS information(key TEXT,'
2018-09-09 22:38:03 +02:00
informations_table += 'value TEXT, session_id INTEGER, path TEXT, '
2018-09-07 08:42:14 +02:00
informations_table += 'PRIMARY KEY (key, session_id), '
informations_table += 'FOREIGN KEY(session_id) REFERENCES session(session_id))'
self.execute(session_table, commit=False)
2017-07-11 22:31:58 +02:00
self.execute(values_table, commit=False)
self.execute(informations_table, commit=False)
self.execute(settings_table, commit=False)
2018-09-07 08:42:14 +02:00
self.execute(permissives_table, commit=False)
2018-09-11 20:11:13 +02:00
self.session_id = None
if self.persistent:
select = self.select("SELECT session_id FROM session WHERE session = ?", (session_id,))
if select is not None:
self.session_id = select[0]
if self.session_id is None:
try:
self.execute('INSERT INTO session(session, persistent) VALUES (?, ?)',
(session_id, persistent))
except sqlite3.IntegrityError:
raise ConflictError(_('session "{}" already used').format(session_id))
self.session_id = self._cursor.lastrowid
2013-08-20 22:45:11 +02:00
2017-07-16 23:11:12 +02:00
def commit(self):
self._conn.commit()
2013-08-20 22:45:11 +02:00
def execute(self, sql, params=None, commit=True):
2017-07-16 23:11:12 +02:00
#print(sql, params)
2013-08-20 22:45:11 +02:00
if params is None:
params = tuple()
self._cursor.execute(sql, params)
if commit:
2017-07-16 23:11:12 +02:00
self.commit()
2013-08-20 22:45:11 +02:00
def select(self, sql, params=None, only_one=True):
self.execute(sql, params=params, commit=False)
if only_one:
return self._cursor.fetchone()
else:
return self._cursor.fetchall()
def __del__(self):
self._cursor.close()
2013-08-27 09:46:52 +02:00
if not self.persistent:
2017-07-04 19:59:42 +02:00
if delete_session is not None:
2018-09-07 08:42:14 +02:00
session_id = getattr(self, 'session_id', None)
delete_session(self.session_name,
session_id)
2017-07-04 19:59:42 +02:00
def getsession():
pass