-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
149 lines (137 loc) · 4.61 KB
/
database.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import configparser, os, sqlite3, sqlitebck
class database:
@staticmethod
def connect():
filePath = os.path.dirname(os.path.realpath(__file__))
config = configparser.ConfigParser()
config.read(os.path.join(filePath, 'totale_architektur.config'))
path = os.path.join(filePath, config.get('database', 'path'))
db = sqlite3.connect(path)
db.execute(
'''CREATE TABLE IF NOT EXISTS einheiten (einheit INTEGER UNIQUE, clipPaths, sumPaths)''')
db.execute(
'''CREATE TABLE IF NOT EXISTS clips (einheit, path)''')
db.execute(
'''CREATE TABLE IF NOT EXISTS sums (einheit, path)''')
db.execute(
'''CREATE TABLE IF NOT EXISTS recs (path)''')
db.execute(
'''CREATE TABLE IF NOT EXISTS dailies (date STRING UNIQUE, path)''')
return db
@staticmethod
def writeEinheit(einheit, clipPaths, sumPaths):
db = database.connect()
if len(clipPaths) > 0:
clips = ','.join(clip for clip in clipPaths)
else:
clips = None
if len(sumPaths) > 0:
sums = ','.join(sum for sum in sumPaths)
else:
sums = None
db.execute(
'''INSERT OR REPLACE INTO einheiten (einheit, clipPaths, sumPaths) VALUES (?, ?, ?)''',
[str(einheit), clips, sums])
db.commit()
db.close()
@staticmethod
def writeClip(einheit, path):
db = database.connect()
db.execute(
'''INSERT OR REPLACE INTO clips (einheit, path) VALUES (?, ?)''',
[str(einheit), path])
db.commit()
db.close()
@staticmethod
def writeSum(einheit, path):
db = database.connect()
db.execute(
'''INSERT OR REPLACE INTO sums (einheit, path) VALUES (?, ?)''',
[str(einheit), path])
db.commit()
db.close()
@staticmethod
def writeRec(path):
db = database.connect()
db.execute('''INSERT OR REPLACE INTO recs (path) VALUES (?)''', [path])
db.commit()
db.close()
@staticmethod
def writeDaily(date, path):
db = database.connect()
db.execute(
'''INSERT OR REPLACE INTO dailies (date, path) VALUES (?, ?)''',
[date, path])
db.commit()
db.close()
@staticmethod
def readEinheit(einheit):
db = database.connect()
entry = db.execute(
'''SELECT clipPaths, sumPaths FROM einheiten WHERE (einheit IS ?)''',
[einheit])
daten = entry.fetchone()
db.close()
if daten is None or daten[0] is None:
clips = []
else:
clips = daten[0].split(',')
if daten is None or daten[1] is None:
sums = []
else:
sums = daten[1].split(',')
return [clips, sums]
@staticmethod
def readClips(einheit):
db = database.connect()
entry = db.execute('''SELECT path FROM clips WHERE (einheit IS ?)''', [einheit])
daten = entry.fetchall()
db.close()
if daten is None:
return []
else:
return [path[0] for path in daten]
@staticmethod
def readSums(einheit):
db = database.connect()
entry = db.execute('''SELECT path FROM sums WHERE (einheit IS ?)''', [einheit])
daten = entry.fetchall()
db.close()
if daten is None:
return []
else:
return [path[0] for path in daten]
@staticmethod
def readRecs():
db = database.connect()
entry = db.execute('''SELECT * FROM recs''')
daten = entry.fetchall()
db.close()
if daten is None:
return []
else:
return [path[0] for path in daten]
@staticmethod
def readDaily(date):
db = database.connect()
entry = db.execute('''SELECT path FROM dailies WHERE (date IS ?)''', [date])
daten = entry.fetchone()
db.close()
if daten is None:
return None
else:
return daten[0]
@staticmethod
def backup(filename):
db = database.connect()
filePath = os.path.dirname(os.path.realpath(__file__))
config = configparser.ConfigParser()
config.read(os.path.join(filePath, 'totale_architektur.config'))
path = os.path.join(filePath, config.get('database', 'backupDir'))
if not os.path.exists(path):
os.makedirs(path)
path = os.path.join(path, filename)
backup = sqlite3.connect(path)
sqlitebck.copy(db, backup)
db.close()
backup.close()