db timeout

This commit is contained in:
ingvar1995 2016-09-08 00:10:32 +03:00
parent fb1caa244a
commit 42e0ec005b

View File

@ -1,4 +1,3 @@
# coding=utf-8
from sqlite3 import connect from sqlite3 import connect
import settings import settings
from os import chdir from os import chdir
@ -8,6 +7,8 @@ from toxencryptsave import ToxEncryptSave
PAGE_SIZE = 42 PAGE_SIZE = 42
TIMEOUT = 11
MESSAGE_OWNER = { MESSAGE_OWNER = {
'ME': 0, 'ME': 0,
'FRIEND': 1, 'FRIEND': 1,
@ -32,7 +33,7 @@ class History:
fout.write(data) fout.write(data)
except: except:
os.remove(path) os.remove(path)
db = connect(name + '.hstr') db = connect(name + '.hstr', timeout=TIMEOUT)
cursor = db.cursor() cursor = db.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS friends(' cursor.execute('CREATE TABLE IF NOT EXISTS friends('
' tox_id TEXT PRIMARY KEY' ' tox_id TEXT PRIMARY KEY'
@ -62,7 +63,7 @@ class History:
def add_friend_to_db(self, tox_id): def add_friend_to_db(self, tox_id):
chdir(settings.ProfileHelper.get_path()) chdir(settings.ProfileHelper.get_path())
db = connect(self._name + '.hstr') db = connect(self._name + '.hstr', timeout=TIMEOUT)
try: try:
cursor = db.cursor() cursor = db.cursor()
cursor.execute('INSERT INTO friends VALUES (?);', (tox_id, )) cursor.execute('INSERT INTO friends VALUES (?);', (tox_id, ))
@ -82,7 +83,7 @@ class History:
def delete_friend_from_db(self, tox_id): def delete_friend_from_db(self, tox_id):
chdir(settings.ProfileHelper.get_path()) chdir(settings.ProfileHelper.get_path())
db = connect(self._name + '.hstr') db = connect(self._name + '.hstr', timeout=TIMEOUT)
try: try:
cursor = db.cursor() cursor = db.cursor()
cursor.execute('DELETE FROM friends WHERE tox_id=?;', (tox_id, )) cursor.execute('DELETE FROM friends WHERE tox_id=?;', (tox_id, ))
@ -96,7 +97,7 @@ class History:
def friend_exists_in_db(self, tox_id): def friend_exists_in_db(self, tox_id):
chdir(settings.ProfileHelper.get_path()) chdir(settings.ProfileHelper.get_path())
db = connect(self._name + '.hstr') db = connect(self._name + '.hstr', timeout=TIMEOUT)
cursor = db.cursor() cursor = db.cursor()
cursor.execute('SELECT 0 FROM friends WHERE tox_id=?', (tox_id, )) cursor.execute('SELECT 0 FROM friends WHERE tox_id=?', (tox_id, ))
result = cursor.fetchone() result = cursor.fetchone()
@ -105,7 +106,7 @@ class History:
def save_messages_to_db(self, tox_id, messages_iter): def save_messages_to_db(self, tox_id, messages_iter):
chdir(settings.ProfileHelper.get_path()) chdir(settings.ProfileHelper.get_path())
db = connect(self._name + '.hstr') db = connect(self._name + '.hstr', timeout=TIMEOUT)
try: try:
cursor = db.cursor() cursor = db.cursor()
cursor.executemany('INSERT INTO id' + tox_id + '(message, owner, unix_time, message_type) ' cursor.executemany('INSERT INTO id' + tox_id + '(message, owner, unix_time, message_type) '
@ -119,7 +120,7 @@ class History:
def update_messages(self, tox_id, unsent_time): def update_messages(self, tox_id, unsent_time):
chdir(settings.ProfileHelper.get_path()) chdir(settings.ProfileHelper.get_path())
db = connect(self._name + '.hstr') db = connect(self._name + '.hstr', timeout=TIMEOUT)
try: try:
cursor = db.cursor() cursor = db.cursor()
cursor.execute('UPDATE id' + tox_id + ' SET owner = 0 ' cursor.execute('UPDATE id' + tox_id + ' SET owner = 0 '
@ -134,7 +135,7 @@ class History:
def delete_message(self, tox_id, time): def delete_message(self, tox_id, time):
chdir(settings.ProfileHelper.get_path()) chdir(settings.ProfileHelper.get_path())
db = connect(self._name + '.hstr') db = connect(self._name + '.hstr', timeout=TIMEOUT)
try: try:
cursor = db.cursor() cursor = db.cursor()
cursor.execute('DELETE FROM id' + tox_id + ' WHERE unix_time = ' + str(time) + ';') cursor.execute('DELETE FROM id' + tox_id + ' WHERE unix_time = ' + str(time) + ';')
@ -147,7 +148,7 @@ class History:
def delete_messages(self, tox_id): def delete_messages(self, tox_id):
chdir(settings.ProfileHelper.get_path()) chdir(settings.ProfileHelper.get_path())
db = connect(self._name + '.hstr') db = connect(self._name + '.hstr', timeout=TIMEOUT)
try: try:
cursor = db.cursor() cursor = db.cursor()
cursor.execute('DELETE FROM id' + tox_id + ';') cursor.execute('DELETE FROM id' + tox_id + ';')
@ -162,9 +163,10 @@ class History:
return History.MessageGetter(self._name, tox_id) return History.MessageGetter(self._name, tox_id)
class MessageGetter: class MessageGetter:
def __init__(self, name, tox_id): def __init__(self, name, tox_id):
chdir(settings.ProfileHelper.get_path()) chdir(settings.ProfileHelper.get_path())
self._db = connect(name + '.hstr') self._db = connect(name + '.hstr', timeout=TIMEOUT)
self._cursor = self._db.cursor() self._cursor = self._db.cursor()
self._cursor.execute('SELECT message, owner, unix_time, message_type FROM id' + tox_id + self._cursor.execute('SELECT message, owner, unix_time, message_type FROM id' + tox_id +
' ORDER BY unix_time DESC;') ' ORDER BY unix_time DESC;')