-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitDB.py
153 lines (136 loc) · 2.7 KB
/
initDB.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
import sqlite3
import json
import os
def p(x):
print(x);return x;
from basic import readJSON, folder
r = lambda n:readJSON(folder()+n)
connexion = sqlite3.connect(folder()+'data.sql')
c = connexion.cursor()
c.execute('PRAGMA foreign_keys = ON;')
#USER TABLE
userData = r("accounts")
users = [ user['username'] for user in userData]
c.execute( """
CREATE TABLE users (
username TEXT PRIMARY KEY,
password TEXT
);
""")
c.executemany(
"INSERT INTO users VALUES (?, ?);",
tuple( (user['username'], user['password']) for user in userData )
)
#PREFERENCE TABLE
preferenceData = (
[
tuple((
username,
'postnumber',
r('user/'+username+'/preferences')['postnumber']
))
for username in users
]
)
c.execute("""
CREATE TABLE preferences (
username TEXT,
preference TEXT,
value REAL,
PRIMARY KEY (username, preference),
FOREIGN KEY(username) REFERENCES users(username)
)
""")
c.executemany(
'INSERT INTO preferences VALUES (?, ?, ?);',
preferenceData
)
#MESSAGER TABLE
userPairs = [
(user1,user2)
for user1 in users
for user2 in users
if user1 in os.listdir(folder()+'user/'+user2+'/messages/')
]
c.execute('''
CREATE TABLE messagers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user1 TEXT,
user2 TEXT,
FOREIGN KEY(user1) REFERENCES users(username),
FOREIGN KEY(user2) REFERENCES users(username),
CHECK ( user1 < user2 )
)
;''')
c.executemany(
'''
INSERT INTO messagers(user1,user2)
VALUES (?, ?)
''' ,
[(min(user1,user2),max(user1,user2))
for user1,user2 in userPairs]
);
#MESSAGE TABLE
c.execute('''
CREATE TABLE messages (
usersid INTEGER,
time REAL PRIMARY KEY,
author TEXT,
content TEXT,
FOREIGN KEY(usersid) REFERENCES messagers(id)
FOREIGN KEY(author) REFERENCES users(username)
)
;''')
c.executemany(
'''
INSERT INTO messages
SELECT id, ?, ?, ?
FROM messagers
WHERE user1=? and user2=?
;''',
[
(
messageData['time'],
messageData['author'],
messageData['content'],
min(user1,user2),
max(user1,user2)
)
for user1,user2 in userPairs
for time in os.listdir(folder()+'user/'+user2+'/messages/'+user1)
for messageData in (r('user/'+user2+'/messages/'+user1+'/'+time),)
]
)
#POST TABLE
c.execute('''
CREATE TABLE posts (
time REAL PRIMARY KEY,
title TEXT,
author TEXT,
content TEXT,
comments TEXT,
FOREIGN KEY(author) REFERENCES users(username)
)
;''')
c.executemany(
'''
INSERT INTO posts
VALUES (?, ?, ?, ?, ?)
;''',
[
(
postData['time'],
postData['title'],
postData['author'],
postData['contents'],
comments
)
for postData,comments in (
(
r('posts/'+postFile),
json.dumps(r('comments/'+postFile)['comments']))
for postFile in os.listdir(folder()+'posts/')
)
]
)
connexion.commit()