-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
147 lines (111 loc) · 3.58 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
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
import sqlite3
import constants
class DBException(Exception):
pass
class DBObject(object):
def __init__(self, conn, id, body=None):
self.conn = conn
self.id = id
self.body = None
if not body:
self._fetch()
else:
self._save(body)
@classmethod
def create(cls, conn, val):
return cls(conn, val[0], val)
def _fetch(self):
c = self.conn.cursor()
c.execute('SELECT * FROM ? WHERE id=? LIMIT 1', (self.table, self.id))
body = c.fetchone()
if not body:
raise DBException('Could not find row')
return self._save(body)
def _save(self, body):
self.body = {}
for i, sch in self.schema:
name, _ = sch
self.body[name] = body[i]
return self.body
def refresh(self):
self.body = None
self._fetch()
def get(self, name, default=None):
return self.body.get(name, default)
class Node(DBObject):
obj_type = 'node'
table = 'nodes'
schema = [
('id', 'integer primary key'),
('root', 'integer'),
('parent', 'integer'),
('body', 'text'),
('logic_entrance_id', 'integer'),
('logic_exit_id', 'integer'),
('logic_subnode_id', 'integer'),
]
def get_outbound_edges(self):
c = self.conn.cursor()
c.execute('SELECT * FROM edges WHERE source_node_id = ?', (self.id, ))
while 1:
body = c.fetchone()
if not body:
return
yield Edge.create(body)
def get_subnodes(self):
c = self.conn.cursor()
c.execute('SELECT * FROM nodes WHERE parent = ?', (self.id, ))
while 1:
body = c.fetchone()
if not body:
return
yield Node.create(self.conn, body)
def is_root(self):
return self.body['root'] == 1
def get_parent(self):
return Node(self.conn, self.body['parent']) if self.body['parent'] else None
def get_subnode_logic(self):
return Logic(self.conn, self.body['logic_subnode_id']) if self.body['logic_subnode_id'] else None
def get_entrance_logic(self):
return Logic(self.conn, self.body['logic_entrance_id']) if self.body['logic_entrance_id'] else None
def get_exit_logic(self):
return Logic(self.conn, self.body['logic_exit_id']) if self.body['logic_exit_id'] else None
class Edge(DBObject):
obj_type = 'edge'
table = 'edges'
schema = [
('id', 'integer primary key'),
('body', 'text'),
('source_node_id', 'integer'),
('destination_node_id', 'integer'),
('logic_trigger_id', 'integer'),
]
def get_text(self):
return self.body['body']
def get_source_node(self):
return Node(self.conn, self.body['source_node_id'])
def get_destination_node(self):
return Node(self.conn, self.body['destination_node_id'])
def get_trigger_logic(self):
return Logic(self.conn, self.body['logic_trigger_id']) if self.body['logic_trigger_id'] else None
class Logic(DBObject):
obj_type = 'logic'
table = 'logic'
schema = [
('id', 'integer primary key'),
('body', 'text'),
]
def get_content(self):
return self.body['body']
def get_root_node(conn):
c = conn.cursor()
c.execute('SELECT * FROM nodes WHERE root=1 LIMIT 1')
body = c.fetchone()
if not body:
return None
return Node.create(conn, body)
def setup():
return sqlite3.connect(constants.DB_PATH)
def teardown(conn):
if conn:
conn.close()