-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
59 lines (51 loc) · 2.18 KB
/
db.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
import sqlite3
import json
import os
from config import Config
class Db:
def __init__(self, removeDb = True):
self.config = Config()
if removeDb == True:
try:
os.remove("./files.db")
except PermissionError:
print("Looks like files.db is currently in use. Please release the resource and try again.")
os._exit(1)
self.conn = sqlite3.connect('./files.db')
self.cursor = self.conn.cursor()
self.ensureSchemaCreated()
def __del__(self):
self.conn.close()
def ensureSchemaCreated(self):
self.cursor.execute('''SELECT name FROM sqlite_master WHERE type='table' AND name='files';''')
result = self.cursor.fetchone()
if result is None:
self.cursor.execute('''CREATE TABLE files
([id] INTEGER PRIMARY KEY,[fileName] text,[extension] text, [filePath] text, [fileSize] integer, [fileHash] text, [createdDate] date, [modifiedDate] date)''')
def insertFile(self, name, extension, path, size, hash, createdDate, modifiedDate):
dataTuple = (name, extension, path, size, hash, createdDate, modifiedDate)
self.cursor.execute(''' INSERT INTO files (fileName, extension, filePath, fileSize, fileHash, createdDate, modifiedDate) values (?, ?, ?, ?, ?, ?, ?)''', dataTuple)
self.conn.commit()
def getDuplicatedDirectories(self):
self.cursor.execute('''SELECT
filesList, filePath,
COUNT(*) occurrences
FROM (select group_concat(fileName) as filesList, filePath
from files
group BY
filePath)
GROUP BY
filesList
HAVING
COUNT(*) > 1
order by occurrences desc
''')
return self.cursor.fetchall()
def getDirectoriesWithDuplicatedFiles(self, filesList):
self.cursor.execute('''select filePath from
(select group_concat(fileName) as filesList, filePath
from files
group BY
filePath)
where filesList = ? ''', (filesList, ))
return self.cursor.fetchall()