forked from birdup000/Jql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jql.py
153 lines (116 loc) · 4.28 KB
/
jql.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 json
import os
import re
import sqlite3
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
class jql:
def __init__(self, sqlFile, sep = '.'):
self.sqlFile = sqlFile
self.sep = sep
self.connection = sqlite3.connect(self.sqlFile)
self.cursor = self.connection.cursor()
self.connection.row_factory = dict_factory
def read(self, path):
split = path.split(self.sep)
get = self.connection.execute(f"SELECT * FROM main WHERE key = '{split[0]}'").fetchall()
if len(get) == 0:
return {}
get = get[0]
if "value" in get:
split = split[1:]
js = json.loads(get["value"])
for i in range(len(split)):
if split[i] in js:
js = js[split[i]]
else:
return None
return js
return get
def get_all(self):
return self.connection.execute("SELECT * FROM main").fetchall()
def write(self, path, value):
main_split = path.split(self.sep)
get = self.connection.execute(f"SELECT * FROM main WHERE key = '{main_split[0]}'").fetchall()
if get != []:
get = get[0]
else:
get = {}
get["key"] = ""
get["value"] = "{}"
split = main_split[1:]
main_json = json.loads(get["value"])
js = main_json
for i in range(len(split)-1):
if split[i] in js:
js = js[split[i]]
else:
js[split[i]] = {}
js = js[split[i]]
if split == []:
js[value] = ""
else:
js[split[-1]] = value
self.set_value_raw(main_split[0], json.dumps(main_json))
return main_json
def delete(self, path):
main_split = path.split(self.sep)
get = self.connection.execute(f"SELECT * FROM main WHERE key = '{main_split[0]}'").fetchall()[0]
if "value" in get:
split = main_split[1:]
main_json = json.loads(get["value"])
js = main_json
for i in range(len(split)-1):
if split[i] in js:
js = js[split[i]]
else:
js[split[i]] = {}
js = js[split[i]]
del js[split[-1]]
self.set_value_raw(main_split[0], json.dumps(main_json))
return main_json
return get
def set_value_raw(self, key, value):
get = self.connection.execute(f"SELECT * FROM main WHERE key = '{key}'").fetchall()
if get == []:
self.connection.execute("INSERT INTO main VALUES (?, ?)", (key, value))
else:
self.connection.execute(f"UPDATE main SET value = '{value}' WHERE key = '{key}'")
self.save()
def rename_key(self, key, new_key):
self.connection.execute(f"UPDATE main SET key = '{new_key}' WHERE key = '{key}'")
self.save()
def delete_key(self, key):
self.connection.execute(f"DELETE FROM main WHERE key = '{key}'")
self.save()
def db_exist(self):
return os.path.isfile(self.sqlFile)
def create_db(self):
self.update('')
def save(self):
self.connection.commit()
def __exit__(self):
self.connection.close()
def create_table(self, tableName, columns):
sql = 'CREATE TABLE ' + tableName + '('
for i in range(len(columns)):
sql += columns[i]
if i < len(columns) - 1:
sql += ', '
sql += ');'
self.update(sql)
if __name__ == "__main__":
test = jql("my_database.db")
test.read("dfsaafasdf")
#test.write("eiko", 1465)
#test.write("eikosa.password","1234")
#test.write("eikosa.age","82")
#
#test.write("eikosa.books.technic.python","")
#test.write("eikosa.books.technic.physics","")
#test.write("eikosa.books.literature.dosteyovsky","")
#test.delete("eikosa.books.literature.tolstoy")
#test.read("eikosa.books.literature")