-
Notifications
You must be signed in to change notification settings - Fork 1
/
srcml_database.py
326 lines (276 loc) · 9 KB
/
srcml_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import sqlite3
import os
def dict_factory(cursor, row):
fields = [column[0] for column in cursor.description]
return {key: value for key, value in zip(fields, row)}
try:
connection = sqlite3.connect("data/srcml.db3",check_same_thread=False)
except sqlite3.OperationalError:
if not os.path.exists('data'):
os.makedirs('data')
file = open("data/srcml.db3",'wb')
file.close()
finally:
connection = sqlite3.connect("data/srcml.db3",check_same_thread=False)
connection.execute("PRAGMA foreign_keys = 1")
connection.row_factory = dict_factory
def _create_database():
cursor = connection.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS "repository" (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS "file" (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
language TEXT NOT NULL,
repo_id INTEGER,
CONSTRAINT fk_repo_id
FOREIGN KEY(repo_id)
REFERENCES repository(id)
ON DELETE CASCADE
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS "identifier" (
name TEXT NOT NULL,
type TEXT,
category TEXT NOT NULL,
file_id INTEGER,
line INTEGER NOT NULL,
column INTEGER NOT NULL,
stereotype TEXT,
nameChecker TEXT,
violationCode TEXT,
CONSTRAINT fk_filde_id
FOREIGN KEY(file_id)
REFERENCES file(id)
ON DELETE CASCADE,
PRIMARY KEY(name,category,file_id,line,column)
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS "tag_count" (
tag TEXT NOT NULL,
file_id INTEGER,
count INTEGER NOT NULL,
CONSTRAINT fk_file_id
FOREIGN KEY(file_id)
REFERENCES file(id)
ON DELETE CASCADE,
PRIMARY KEY(tag,file_id)
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS "query_run" (
id INTEGER PRIMARY KEY AUTOINCREMENT,
query TEXT NOT NULL,
query_type TEXT NOT NULL
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS "query_run_result" (
file_id INTEGER,
query_id INTEGER,
CONSTRAINT fk_file_id
FOREIGN KEY(file_id)
REFERENCES file(id)
ON DELETE CASCADE,
CONSTRAINT fk_query_id
FOREIGN KEY(query_id)
REFERENCES query_run(id)
ON DELETE CASCADE,
PRIMARY KEY(file_id,query_id)
);
""")
def commit():
connection.commit()
def add_repo(repo_name):
cursor = connection.cursor()
cursor.execute("""
INSERT INTO repository (name)
VALUES (?);
""",(repo_name,))
def get_repo_id_from_name(repo_name):
cursor = connection.cursor()
cursor.execute("""
SELECT id
FROM repository
WHERE name=?;
""", (repo_name,))
return cursor.fetchone()["id"]
def get_repo_name_from_id(repo_id):
cursor = connection.cursor()
cursor.execute("""
SELECT name
FROM repository
WHERE id=?;
""", (repo_id,))
return cursor.fetchone()["name"]
def get_repo_id_from_file_id(file_id):
cursor = connection.cursor()
cursor.execute("""
SELECT repository.id
FROM file
INNER JOIN repository ON file.repo_id = repository.id
WHERE file.id=?;
""", (file_id,))
return cursor.fetchone()["id"]
def get_repo_name_from_file_id(file_id):
cursor = connection.cursor()
cursor.execute("""
SELECT repository.name
FROM file
INNER JOIN repository ON file.repo_id = repository.id
WHERE file.id=?;
""", (file_id,))
return cursor.fetchone()["name"]
def add_file(name,language,repo_name):
cursor = connection.cursor()
cursor.execute("""
INSERT INTO file (name,language,repo_id)
VALUES (?,?,?);
""", (name,language,get_repo_id_from_name(repo_name)))
def get_file_id_from_name_and_repo(filename,repo_id):
cursor = connection.cursor()
cursor.execute("""
SELECT id
FROM file
WHERE name=? AND repo_id=?;
""", (filename,repo_id))
return cursor.fetchone()["id"]
def get_file_name_from_id(file_id):
cursor = connection.cursor()
cursor.execute("""
SELECT name
FROM file
WHERE id=?;
""", (file_id,))
return cursor.fetchone()["name"]
def add_identifier(name,type,category,file_id,line,column,stereotype, nameChecker, violationCode):
cursor = connection.cursor()
cursor.execute("""
INSERT INTO identifier (name,type,category,file_id,line,column,stereotype, nameChecker, violationCode)
VALUES (?,?,?,?,?,?,?,?,?)
""",(name,type,category,file_id,line,column,stereotype, nameChecker, violationCode))
def add_tag_count(tag,file_id,count):
cursor = connection.cursor()
cursor.execute("""
INSERT INTO tag_count (tag,file_id,count)
VALUES (?,?,?);
""",(tag,file_id,count))
def retrieve_repos():
cursor = connection.cursor()
cursor.execute("""
SELECT id, name
FROM repository;
""")
return cursor.fetchall()
def retrieve_files(repo_id):
cursor = connection.cursor()
cursor.execute("""
SELECT id, name, language
FROM file
WHERE repo_id = ?;
""", (repo_id,))
return cursor.fetchall()
def retrieve_identifiers(file_id):
cursor = connection.cursor()
cursor.execute("""
SELECT name, type, category, file_id, line, column, stereotype, nameChecker, violationCode
FROM identifier
WHERE file_id = ?
""", (file_id,))
return cursor.fetchall()
def retrieve_identifiers_from_repo(repo_id):
cursor = connection.cursor()
cursor.execute("""
SELECT identifier.name, type, category, file_id, line, column, file.name as filename, stereotype, nameChecker, violationCode
FROM file
INNER JOIN identifier ON file.id = identifier.file_id
WHERE repo_id = ?;
""", (repo_id,))
return cursor.fetchall()
def retrieve_tags(file_id):
cursor = connection.cursor()
cursor.execute("""
SELECT tag, file_id, count
FROM tag_count
WHERE file_id IN (SELECT id FROM file WHERE file_id = ? AND count != 0)
""", (file_id,))
return cursor.fetchall()
def retrieve_tags_from_repo(repo_id):
cursor = connection.cursor()
cursor.execute("""
SELECT tag, SUM(count) as count
FROM repository
INNER JOIN file ON repository.id = file.repo_id
INNER JOIN tag_count ON file.id = tag_count.file_id
WHERE repository.id = ? AND count != 0
GROUP BY repository.id, tag;
""", (repo_id,))
return cursor.fetchall()
def retrieve_queries(file_id):
cursor = connection.cursor()
cursor.execute("""
SELECT query_run.id, query, query_type, file_id
FROM query_run
INNER JOIN query_run_result ON query_run.id = query_run_result.query_id
WHERE file_id = ?;
""", (file_id,))
return cursor.fetchall()
def retrieve_queries_from_repo(repo_id):
cursor = connection.cursor()
cursor.execute("""
SELECT query_run.id, query, query_type, file_id, file.name as filename
FROM query_run
INNER JOIN query_run_result ON query_run.id = query_run_result.query_id
INNER JOIN file ON query_run_result.file_id = file.id
INNER JOIN repository ON file.repo_id = repository.id
WHERE repository.id = ?
""", (repo_id,))
return cursor.fetchall()
def create_query_run(query,type):
cursor = connection.cursor()
cursor.execute("""
INSERT INTO query_run(query,query_type)
VALUES (?,?);
""",(query,type))
return cursor.lastrowid
def add_query_result(file_id,query_id):
cursor = connection.cursor()
cursor.execute("""
INSERT INTO query_run_result(file_id,query_id)
VALUES (?,?);
""",(file_id,query_id))
def remove_repo(repo_id):
cursor = connection.cursor()
# # Delete associated tags
# cursor.execute("""
# DELETE FROM tag_count
# WHERE file_id IN (SELECT id FROM file WHERE repo_id = ?)
# """, (repo_id,))
# # Delete associated identifiers
# cursor.execute("""
# DELETE FROM identifier
# WHERE file_id IN (SELECT id FROM file WHERE repo_id = ?)
# """, (repo_id,))
# # Delete query data
# cursor.execute("""
# DELETE FROM identifier
# WHERE file_id IN (SELECT id FROM file WHERE repo_id = ?)
# """, (repo_id,))
# # Delete associated files
# cursor.execute("""
# DELETE FROM file
# WHERE repo_id = ?
# """, (repo_id,))
# Delete the repository
cursor.execute("""
DELETE FROM repository
WHERE id = ?
""", (repo_id,))
commit()