-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLHelper.py
93 lines (87 loc) · 3.44 KB
/
SQLHelper.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
81
82
83
84
85
86
87
88
89
90
91
92
93
import sqlite3
from config import *
def getCaseID(caseName):
conn = sqlite3.connect(DATABASE)
q = conn.execute("SELECT ID FROM CASES WHERE CASES.NAME = ?", (caseName,))
ID = q.fetchone()
conn.close()
if ID:
return ID[0]
return None
def getFileID(fileName, caseName):
caseID = getCaseID(caseName)
conn = sqlite3.connect(DATABASE)
conn.execute('pragma foreign_keys=ON')
q = conn.execute("SELECT ID FROM FILES WHERE FILES.CASEID = ? AND FILES.FILENAME = ?", (caseID, fileName,))
#q = conn.execute("SELECT ID FROM FILES WHERE FILES.CASEID = "+str(caseID)+" AND FILES.FILENAME = \'"+fileName+"\'")
fileID = q.fetchone()
if fileID is None:
conn.close()
return None
fileID = fileID[0]
conn.close()
return fileID
def getCaseAndFilterIDs(caseName):
conn = sqlite3.connect(DATABASE)
conn.execute('pragma foreign_keys=ON')
q = conn.execute("SELECT ID,FILTERID FROM CASES WHERE CASES.NAME = ?",(caseName,))
IDs = q.fetchone()
conn.close()
return IDs
def loadAllFiles(caseName = '*'):
conn = sqlite3.connect(DATABASE)
if caseName == '*':
q = conn.execute("SELECT * FROM FILES")
else:
q = conn.execute("SELECT ID FROM CASES WHERE CASES.NAME = ?",(caseName,))
caseID = q.fetchone()[0]
q = conn.execute("SELECT FILENAME FROM FILES WHERE CASEID = ?", (caseID,))
files = []
for row in q:
files.append(row[0])
conn.close()
return files
def updateFileInfo(fileID, filterID = None, size = None, dateTimes = None):
conn = sqlite3.connect(DATABASE)
conn.execute('pragma foreign_keys=ON')
if filterID != 'null':
q = conn.execute("UPDATE FILES SET FILTERID = ?, SIZE = ?, FIRST_PACKET_DATETIME = ?, LAST_PACKET_DATETIME = ? WHERE FILES.ID = ?", (filterID, size, dateTimes[0], dateTimes[1], fileID,))
else:
q = conn.execute("UPDATE FILES SET SIZE = ?, FIRST_PACKET_DATETIME = ?, LAST_PACKET_DATETIME = ? WHERE FILES.ID = ?", (size, dateTimes[0], dateTimes[1], fileID,))
conn.commit()
conn.close()
def updateFileDescription(fileID, description):
conn = sqlite3.connect(DATABASE)
conn.execute('pragma foreign_keys=ON')
conn.execute("UPDATE FILES SET DESCRIPTION = ? WHERE ID = ?",(description, fileID,))
conn.commit()
conn.close()
def getFileInfo(fileID):
conn = sqlite3.connect(DATABASE)
q = conn.execute("SELECT FILTERID, SIZE, FIRST_PACKET_DATETIME, LAST_PACKET_DATETIME, SOURCE_FILE, DESCRIPTION FROM FILES WHERE ID = ?",(fileID,))
info = q.fetchone()
conn.commit()
conn.close()
return info
def loadFiles(caseName = '*', type = "*", additionalColumn = ''):
conn = sqlite3.connect(DATABASE)
if caseName == '*':
if type == "*":
q = conn.execute("SELECT * FROM FILES")
else:
q = conn.execute("SELECT * FROM FILES WHERE TYPE = ?",(type,))
else:
q = conn.execute("SELECT ID FROM CASES WHERE CASES.NAME = ?", (caseName,))
caseID = q.fetchone()[0]
if type == "*":
q = conn.execute("SELECT FILENAME "+additionalColumn+" FROM FILES WHERE CASEID = ?",(str(caseID),))
else:
q = conn.execute("SELECT FILENAME "+additionalColumn+" FROM FILES WHERE CASEID = ? AND TYPE = ?",(caseID, type,))
files = []
for row in q:
if additionalColumn == '':
files.append(row[0])
else:
files.append((row[0],row[1]))
conn.close()
return files