-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagdb.py
150 lines (132 loc) · 4.31 KB
/
tagdb.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
'''
The db used to store the USR
'''
import sqlite3
class TagDb:
db=''
conn = None
def __exe(self, sql):
c = self.conn.cursor()
c.execute(sql)
c.close()
def commit(self):
self.conn.commit()
def __fetch_one(self, sql):
c = self.conn.cursor()
c.execute(sql)
for r in c:
c.close()
return list(r)
c.close()
return None
def __fetch_all(self, sql):
c = self.conn.cursor()
c.execute(sql)
rows = []
for r in c:
rows.append(list(r))
return rows
def __init__(self, db='usr.db'):
self.db = db
self.conn = sqlite3.connect(self.db)
sql = 'create table if not exists tb_func_defs (usr text, src text, line integer, offset integer, tag text, is_definition integer)'
self.__exe(sql)
sql = 'create index if not exists index_func_defs on tb_func_defs(usr ASC)'
self.__exe(sql)
sql = 'create table if not exists tb_refs (usr text, src text, offset integer, tag text)'
self.__exe(sql)
sql = 'create index if not exists index_refs_usr on tb_refs(usr ASC)'
self.__exe(sql)
sql = 'create index if not exists index_refs_src on tb_refs(src,offset, tag)'
self.__exe(sql)
sql = 'create table if not exists tb_includes (src text, include text, offset integer)'
self.__exe(sql)
def add_func_def(self, usr, file_name, line, offset, tag, is_definition):
sql = 'insert into tb_func_defs values ("%s","%s", %d, %d, "%s", %d)'%(usr, file_name, line,offset,tag, is_definition)
self.__exe(sql)
def add_ref(self, usr, file_name, offset, tag):
sql = 'insert into tb_refs values ("%s","%s", %d, "%s")'%(usr, file_name, offset, tag)
self.__exe(sql)
def add_include(self, src, include, offset):
sql = 'insert into tb_includes values ("%s","%s", %d)'%(src, include, offset)
self.__exe(sql)
def get_defs(self, f, line):
sql = 'select * from tb_func_defs where src="%s" and line=%d'%(f, line)
return self.__fetch_all(sql)
def get_ref(self, f, offset, tag):
sql = 'select * from tb_refs where src="%s" and offset=%d and tag="%s"'%(f, offset, tag)
row = self.__fetch_one(sql)
#regular reference
if row is not None:
sql = 'select src from tb_func_defs where usr="%s"' % row[0]
r = self.__fetch_one(sql)
if r is not None:row[1] = r[0]
return row
#declare and implement reference
else:
sql = 'select * from tb_func_defs where src="%s" and offset=%d and tag="%s" and is_definition=0'%(f, offset, tag)
row = self.__fetch_one(sql)
if row is None:
return None
sql = 'select src from tb_func_defs where usr="%s" and is_definition=1' % row[0]
r = self.__fetch_one(sql)
if r is None: return None
row[1] = r[0]
return row
def get_include(self, src, offset):
sql = 'select * from tb_includes where src = "%s" and offset=%d'%(src, offset)
return self.__fetch_one(sql)
def is_processed(self, src):
sql = 'select * from tb_includes where src="%s" or include="%s"'%(src, src)
res = self.__fetch_one(sql)
return res is not None
def files(self):
res = set()
sql = 'select distinct(src) from tb_func_defs'
rows = self.__fetch_all(sql)
for r in rows:
res.add(r[0])
sql = 'select distinct(src) from tb_refs'
rows = self.__fetch_all(sql)
for r in rows:
res.add(r[0])
sql = 'select distinct(src) from tb_includes'
rows = self.__fetch_all(sql)
for r in rows:
res.add(r[0])
sql = 'select distinct(include) from tb_includes'
rows = self.__fetch_all(sql)
for r in rows:
res.add(r[0])
for f in res:
yield f
def clear(self):
sql = 'delete from tb_refs'
self.__exe(sql)
sql = 'delete from tb_func_defs'
self.__exe(sql)
def close(self):
self.conn.close()
def dump(self):
sql = 'select * from tb_refs'
rows = self.__fetch_all(sql)
print "tb_refs"
for r in rows:
print '[%s]' % ', '.join(map(str, r))
sql = 'select * from tb_func_defs'
rows = self.__fetch_all(sql)
print "tb_func_defs"
for r in rows:
print '[%s]' % ', '.join(map(str, r))
sql = 'select * from tb_includes'
rows = self.__fetch_all(sql)
print "tb_includes"
for r in rows:
print '[%s]' % ', '.join(map(str, r))
'''
db = TagDb()
db.dump()
for f in db.files():
print f
db.close()
'''