Repository navigation
Expand file tree
/
Copy pathcollection.py
More file actions
60 lines (54 loc) · 1.46 KB
/
Copy pathcollection.py
File metadata and controls
60 lines (54 loc) · 1.46 KB
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
class collectionCmnd:
def __init__(self, new_instance, table):
self.db_self = new_instance
self.table = table
def drop(self):
try:
self.db_self.cursor.execute(f"DROP TABLE {self.table}")
except:
print("Collection does not exists...")
def insertOne(self, data):
unq_id = ""
for i in range(10):
if choice([True, False]):
unq_id += str(randint(0, 9))
else:
unq_id += choice(ascii_letters)
data_str = dumps({"_id": unq_id, **data})
self.db_self.cursor.execute(f'''
INSERT INTO {self.table} (data) VALUES (?)
''', (data_str,))
def insertMany(self, data):
def unq_id_generator():
unq_id = ""
for i in range(10):
if choice([True, False]):
unq_id += str(randint(0, 9))
else:
unq_id += choice(ascii_letters)
return unq_id
tupled_data = [(dumps({"_id": unq_id_generator(), **d}),) for d in data]
self.db_self.cursor.executemany(f"""
INSERT INTO {self.table} (data) VALUES (?)
""", tupled_data)
def find(self, query = {}, fields = {}):
def pretty():
pass
if len(query) > 0:
clauses = []
db_fields = []
placeholder = []
values = []
f, p, v = query_generator(query, fields)
"""raw_data = self.db_self.cursor.execute(f'''
SELECT
{",".join(f)}
FROM
{self.table}
WHERE
{p}
''', v).fetchall()"""
else:
raw_data = self.db_self.cursor.execute(f"SELECT data FROM {self.table}").fetchall()
data = [loads(row[0]) for row in raw_data]
pretty_print(data)