This repository has been archived by the owner on Apr 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.py
170 lines (138 loc) · 4.31 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import sqlite3
from ResourceRecord import ResourceRecord
from time import time
class Database:
def __init__(self, name: str):
"""Init a aatabase to cache ResourceRecord."""
# Connect databae
self._name = name
conn = sqlite3.connect(self._name)
# Create a cursor
c = conn.cursor()
# Create table
c.execute("""
CREATE TABLE IF NOT EXISTS Cache(
domain text,
type integer,
class integer,
ttl integer,
data text,
ttd integer
)
""")
# Commit connect
conn.commit()
# Close connect
conn.close()
def refresh(self):
"""
Refresh the database to remove out-dated caches.
"""
# Connect database
conn = sqlite3.connect(self._name)
# Create cursor
c = conn.cursor()
c.execute("SELECT strftime('%s','now')")
timestamp = (int(c.fetchone()[0]))
time = (timestamp,)
# sqlite3 doesn't support literal value comparision;
# therefore an temporary _Variables table is necessary.
c.execute("""
CREATE TABLE IF NOT EXISTS _Variables(timestamp INTEGER)
""")
c.execute("INSERT INTO _Variables VALUES (?)", time)
c.execute("""
DELETE FROM Cache
WHERE ttd < (SELECT timestamp FROM _Variables)
""")
c.execute("DROP TABLE _Variables")
# Commit connect
conn.commit()
# Close connect
conn.close()
def add_to_database(self, rr: ResourceRecord):
"""
Add an RR to database.
"""
if rr.ttl > 0:
# Connect database
conn = sqlite3.connect(self._name)
# Create cursor
c = conn.cursor()
ttd = int(time()) + rr.ttl
data = (rr.name, rr.rr_type, rr.rr_class,
rr.ttl, rr.rdata, ttd)
c.execute("INSERT INTO Cache VALUES (?,?,?,?,?,?)", data)
# Commit connect
conn.commit()
# Close connect
conn.close()
def query_from_database(self, name: str, rr_type: int = 1,
rr_class: int = 1) -> ResourceRecord:
"""
Query a tuple of (name, typr, class) from database for a match.
"""
# Connect database
conn = sqlite3.connect(self._name)
# Create cursor
c = conn.cursor()
data = (name, rr_type, rr_class)
# Create a temporary table for querying purposes
c.execute("""
CREATE TABLE IF NOT EXISTS _Variables(Name TEXT PRIMARY KEY, Class INTEGER, Type INTEGER)
""")
c.execute("INSERT INTO _Variables VALUES (?,?,?)", data)
c.execute("""
SELECT C.domain, C.type, C.class, C.ttl, C.data FROM Cache C
WHERE C.domain = (SELECT Name FROM _Variables)
AND C.type = (SELECT Type FROM _Variables)
AND C.class = (SELECT Class FROM _Variables)
""")
ans = c.fetchone()
c.execute("DROP TABLE _Variables")
# Commit connect
conn.commit()
# Close connect
conn.close()
if ans is None:
return ans
else:
return ResourceRecord(ans[0], ans[1], ans[2], ans[3], ans[4])
"""format use database sqlite3"""
# def default():
# # Connect database
# conn = sqlite3.connect('CacheSystem.db')
# # Create cursor
# c = conn.cursor()
""" code here """
# # Commit connect
# conn.commit()
# # Close connect
# conn.close()
if __name__ == '__main__':
print("Resolver Database")
# Connect database
conn = sqlite3.connect('DatabaseResolver.db')
# Create cursor
c = conn.cursor()
c.execute("SELECT * FROM Cache")
data = c.fetchall()
for dat in data:
print(dat)
# Commit connect
conn.commit()
# Close connect
conn.close()
print("NameServer Database")
# Connect database
conn = sqlite3.connect('DatabaseNS.db')
# Create cursor
c = conn.cursor()
c.execute("SELECT * FROM Cache")
data = c.fetchall()
for dat in data:
print(dat)
# Commit connect
conn.commit()
# Close connect
conn.close()