toxygen/src/history.py

109 lines
3.3 KiB
Python
Raw Normal View History

2016-03-12 11:18:00 +00:00
# coding=utf-8
import sqlite3
from settings import Settings
import os
2016-03-12 15:25:16 +00:00
MESSAGE_OWNER = {
'ME': 0,
'FRIEND': 1
}
2016-03-12 11:18:00 +00:00
class History(object):
def __init__(self, name):
self._name = name
os.chdir(Settings.get_default_path())
db = sqlite3.connect(name + '.hstr')
cursor = db.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS friends('
' tox_id TEXT PRIMARY KEY'
')')
db.close()
def add_friend_to_db(self, tox_id):
2016-03-12 15:25:16 +00:00
os.chdir(Settings.get_default_path())
db = sqlite3.connect(self._name + '.hstr')
2016-03-12 11:18:00 +00:00
try:
cursor = db.cursor()
cursor.execute('INSERT INTO friends VALUES (?);', (tox_id, ))
cursor.execute('CREATE TABLE id' + tox_id + '('
' id INTEGER PRIMARY KEY,'
' message TEXT,'
' owner INTEGER,'
' unix_time INTEGER'
' message_type INTEGER'
')')
db.commit()
2016-03-12 16:22:33 +00:00
except:
2016-03-12 11:18:00 +00:00
db.rollback()
2016-03-12 16:22:33 +00:00
raise
2016-03-12 11:18:00 +00:00
finally:
db.close()
def delete_friend_from_db(self, tox_id):
2016-03-12 15:25:16 +00:00
os.chdir(Settings.get_default_path())
db = sqlite3.connect(self._name + '.hstr')
2016-03-12 11:18:00 +00:00
try:
cursor = db.cursor()
cursor.execute('DELETE FROM friends WHERE tox_id=?;', (tox_id, ))
cursor.execute('DROP TABLE id' + tox_id + ';')
db.commit()
2016-03-12 16:22:33 +00:00
except:
2016-03-12 11:18:00 +00:00
db.rollback()
2016-03-12 16:22:33 +00:00
raise
2016-03-12 11:18:00 +00:00
finally:
db.close()
2016-03-12 16:22:33 +00:00
def friend_exists_in_db(self, tox_id):
os.chdir(Settings.get_default_path())
db = sqlite3.connect(self._name + '.hstr')
cursor = db.cursor()
cursor.execute('SELECT 0 FROM friends WHERE tox_id=?', (tox_id, ))
result = cursor.fetchone()
db.close()
return result is not None
2016-03-12 11:18:00 +00:00
def save_messages_to_db(self, tox_id, messages_iter):
2016-03-12 15:25:16 +00:00
os.chdir(Settings.get_default_path())
db = sqlite3.connect(self._name + '.hstr')
2016-03-12 11:18:00 +00:00
try:
cursor = db.cursor()
cursor.executemany('INSERT INTO id' + tox_id + '(message, owner, unix_time) '
'VALUES (?, ?, ?);', messages_iter)
db.commit()
2016-03-12 16:22:33 +00:00
except:
2016-03-12 11:18:00 +00:00
db.rollback()
2016-03-12 16:22:33 +00:00
raise
2016-03-12 11:18:00 +00:00
finally:
db.close()
def messages_getter(self, tox_id):
2016-03-12 15:25:16 +00:00
return History.MessageGetter(self._name, tox_id)
2016-03-12 11:18:00 +00:00
2016-03-12 15:25:16 +00:00
class MessageGetter(object):
def __init__(self, name, tox_id):
os.chdir(Settings.get_default_path())
self._db = sqlite3.connect(name + '.hstr')
self._cursor = self._db.cursor()
self._cursor.execute('SELECT message, owner, unix_time FROM id' + tox_id +
' ORDER BY unix_time DESC;')
2016-03-12 11:18:00 +00:00
2016-03-12 15:25:16 +00:00
def get_one(self):
return self._cursor.fetchone()
2016-03-12 11:18:00 +00:00
2016-03-12 15:25:16 +00:00
def get_all(self):
return self._cursor.fetchall()
2016-03-12 11:18:00 +00:00
2016-03-12 15:25:16 +00:00
def get(self, count):
return self._cursor.fetchmany(count)
2016-03-12 11:18:00 +00:00
2016-03-12 15:25:16 +00:00
def __del__(self):
self._db.close()
2016-03-12 11:18:00 +00:00
if __name__ == '__main__':
h = History('test')
getter = h.messages_getter('42')
2016-03-12 16:22:33 +00:00
print h.friend_exists_in_db('42'), type(h.friend_exists_in_db('42'))