-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdb.py
101 lines (86 loc) · 2.87 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
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
# -*- coding: utf-8 -*-
import sqlite3
DB_TABEL = "packages"
def getDb(path):
def getCursor(func):
def __call(*args, **kwargs):
conn = sqlite3.connect(path)
ret = func(conn, *args, **kwargs)
conn.commit()
conn.close()
return ret
return __call
def getCs(func):
def __call(*args, **kwargs):
conn = sqlite3.connect(path)
cs = conn.cursor()
ret = func(cs, *args, **kwargs)
conn.commit()
cs.close()
conn.close()
return ret
return __call
class TrackerDb(object):
def __init__(self):
pass
@staticmethod
@getCursor
def init(conn):
conn.execute(
"CREATE TABLE IF NOT EXISTS " + DB_TABEL + "(num char, status int, user char, pType char, lastDate "
"char, packageName char) "
)
@staticmethod
@getCs
def getAllPackages(cs):
cs.execute(
"SELECT * FROM " + DB_TABEL
)
return cs.fetchall()
@staticmethod
@getCs
def getUnfinishAll(cs):
cs.execute(
"SELECT * FROM " + DB_TABEL + " WHERE status=1 AND status=0"
)
return cs.fetchall()
@staticmethod
@getCs
def getUserAll(cs, user):
cs.execute(
"SELECT * FROM " + DB_TABEL + " WHERE user='" + str(user) + "'"
)
return cs.fetchall()
@staticmethod
@getCs
def checkDump(cs, user, num, pType):
cs.execute(
"SELECT * FROM " + DB_TABEL + " WHERE num='" + str(num) + "' AND user='" + str(user)
+ "' AND pType='" + pType + "\'"
)
return len(cs.fetchall()) == 0
@staticmethod
@getCs
def newPackage(cs, user, num, pType, lastDate, packageName, status):
if not TrackerDb.checkDump(user, num, pType):
raise ValueError("Dump")
cs.execute(
"INSERT INTO " + DB_TABEL + " values (?,?,?,?,?,?)", (num, status, user, pType, lastDate, unicode(
packageName))
)
@staticmethod
@getCs
def removePackage(cs, user, num):
cs.execute(
"DELETE FROM " + DB_TABEL + " WHERE num='" + str(num) + "' AND user='" + str(user) + "'"
)
@staticmethod
@getCs
def update(cs, user, num, status, lastDate):
cs.execute(
"UPDATE " + DB_TABEL + " SET status='" + str(status) + "', lastDate='" +
str(lastDate) + "' WHERE user='" + str(user) + "' AND num='" + str(num) + "'"
)
obj = TrackerDb()
obj.init()
return obj