-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersistence.py
80 lines (68 loc) · 2.54 KB
/
Persistence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'''
Created on Sep 17, 2011
@author: boby
'''
import sqlite3
from Config import Config
from Task import Task
log = Config().getLogger('submitMaster.Persistence')
class DB(object):
'''
classdocs
'''
def connect(self):
self.__connection = sqlite3.connect('hash.db', detect_types = sqlite3.PARSE_DECLTYPES)
self.__cursor = self.__connection.cursor()
try:
self.__cursor.execute('CREATE TABLE tasks (tsk Task unique)')
self.__connection.commit()
except sqlite3.OperationalError:
log.debug("couldn't create table, it may already exist!")
sqlite3.register_adapter(Task, self.__taskAdapt)
sqlite3.register_converter("Task", self.__taskConvert)
def __taskAdapt(self,task):
return task.serialize()
def __taskConvert(self,text):
return Task.deserialize(text)
def addTask(self,Tsk):
if self.getTaskByID(Tsk.getIMEI(),Tsk.getHash()) == None:
self.__cursor.execute('INSERT INTO tasks(tsk) VALUES (?)',(Tsk,))
self.__connection.commit()
return True
else:
log.debug("Task already exist in DB!")
return False
def delTaskByID(self,imei,hash):
self.__cursor.execute("DELETE FROM tasks WHERE tsk LIKE ?",('%'+str(imei)+'|'+str(hash)+'%',))
self.__connection.commit()
def getTaskByID(self,imei,hash):
self.__cursor.execute("SELECT tsk FROM tasks WHERE tsk LIKE ?",('%'+str(imei)+'|'+str(hash)+'%',))
try:
tsk=self.__cursor.fetchone()[0]
return tsk
except:
log.debug("No element with imei: %s and hash: %s found"%(imei,hash))
return None
def getTasksWithID(self,hashID):
#TODO test this method
if hashID == None:
return self.getAllTasks()
resultTSK=[]
for item in hashID:
tsk=self.getTaskByID(item["imei"],item["hash"])
if not tsk == None:
resultTSK.append(tsk)
return resultTSK
def getAllTasks(self):
self.__cursor.execute("SELECT tsk FROM tasks")
tpl=self.__cursor.fetchall()
task= []
for t in tpl:
task.append(t[0])
return tuple(task)
def updateTask(self,Tsk):
self.__cursor.execute("UPDATE tasks SET tsk=? WHERE tsk LIKE ?",(Tsk,'%'+str(Tsk.getIMEI())+'|'+str(Tsk.getHash())+'%',))
self.__connection.commit()
def close(self):
self.__cursor.close()
self.__connection.close()