-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface.py
157 lines (140 loc) · 7.2 KB
/
interface.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
154
155
156
157
from database import *
import time
def main():
while True:
# choose database
databases = ["SQL", "noSQL"]
db_to_use = input("Choose a database to use (SQL, noSQL) or exit:")
if db_to_use not in databases:
if db_to_use == 'exit':
break
print("not a existing database")
continue
if db_to_use=="SQL":
db = SQL_Database()
else:
db = noSQL_Database()
key_words = ["GET","FROM","CONNECT","WHEN","GATHER_BY","ASCEND_BY","DESCEND_BY","PUT","DROP","CHANGE"]
while True:
query = input("MyDB> ").strip()
start_time = time.time()
if query.lower() == 'exit':
break
# get key words index
parts = query.split(" ")
key_words_index = []
for i,k in enumerate(parts):
if k in key_words:
key_words_index.append(i)
key_words_index.append("end")
try:
if query.startswith("GET"):
# Parse and execute GET statement
# Example: GET id, name FROM users
from_index = parts.index("FROM")
# parse all the columns
columns = [i.replace(",","") for i in parts[1:from_index]]
# parse all the tables
i = 0
table = parts[from_index+1+i]
# tables = []
# while parts[from_index+1+i]:
# tables.append(parts[from_index+1+i])
# if parts[from_index+1+i].endswith(","):
# i = i + 1
# else:
# break
# parse join (CONNECT)
connect_table = None
on_condition = None
if "CONNECT" in parts:
connect_indexes = [i for i,p in enumerate(parts) if p=="CONNECT"]
#connect_index = parts.index("CONNECT")
#print(connect_index)
connect_table = []
on_condition = []
for connect_index in connect_indexes:
connect_table.append(parts[connect_index + 1])
#on_condition = parts[connect_index + 3:connect_index + 6]
if key_words_index[key_words_index.index(connect_index)+1] == "end":
on_condition+=parts[connect_index + 3:]
else:
on_condition+=parts[connect_index + 3:key_words_index[key_words_index.index(connect_index)+1]]
# parse condition (WHEN)
conditions = None
if "WHEN" in parts:
when_index = parts.index("WHEN")
if key_words_index[key_words_index.index(when_index)+1] == "end":
conditions = parts[when_index + 1:]
else:
conditions = parts[when_index + 1:key_words_index[key_words_index.index(when_index)+1]]
# parse grouping (GATHER BY)
grouping = None
if "GATHER_BY" in parts:
group_index = parts.index("GATHER_BY")
if key_words_index[key_words_index.index(group_index)+1] == "end":
grouping = parts[group_index + 1:]
else:
grouping = parts[group_index + 1:key_words_index[key_words_index.index(group_index)+1]]
# parse ordering (ASCEND_BY/DESCEND_BY)
ordering = None
order_by = None
if "ASCEND_BY" in parts:
ordering_index = parts.index("ASCEND_BY")
ordering = "ASC"
if key_words_index[key_words_index.index(ordering_index)+1] == "end":
order_by = parts[ordering_index + 1:]
else:
order_by = parts[ordering_index + 1:key_words_index[key_words_index.index(ordering_index)+1]]
elif "DESCEND_BY" in parts:
ordering_index = parts.index("DESCEND_BY")
ordering = "DSC"
if key_words_index[key_words_index.index(ordering_index)+1] == "end":
order_by = parts[ordering_index + 1:]
else:
order_by = parts[ordering_index + 1:key_words_index[key_words_index.index(ordering_index)+1]]
db.get(table, columns, connect_table, on_condition, conditions, grouping, ordering, order_by)
elif query.startswith("PUT"):
# Parse and execute PUT IN statement
# Example: PUT 1, 'Alice' IN users
IN_index = parts.index("IN")
table_name = parts[IN_index + 1]
# parse all the values
#values = [i.replace(",", "") for i in parts[1:IN_index]]
joined_string = " ".join(parts)
# Step 1: Remove 'PUT '
values_part = joined_string.split('PUT ')[1]
# Step 2: Remove ' IN tables'
values = values_part.split(' IN ')[0]
db.insert(table_name, values)
elif query.startswith("DROP"):
# Parse and execute DROP sth FROM table WHEN statement
# Example: DROP name FROM table WHEN id = 100
FROM_index = parts.index("FROM")
conditions = None
if "WHEN" in parts:
WHEN_index = parts.index("WHEN")
table_name = parts[FROM_index+1:WHEN_index]
conditions = parts[WHEN_index+1:]
else:
table_name = parts[FROM_index+1:]
# Parse the items
items = [i.replace(",", "") for i in parts[1:FROM_index]]
db.delete(table_name, items, conditions)
elif query.startswith("CHANGE"):
# Parse and execute CHANGE table WITH values WHEN
# Example: CHANGE users WITH name = steve
WITH_index = parts.index("WITH")
table_name = parts[1]
WHEN_index = parts.index("WHEN")
values = [i.replace(",", "") for i in parts[WITH_index + 1: WHEN_index]]
conditions = parts[WHEN_index+1:]
db.update(table_name, values, conditions)
else:
print("Invalid query.")
except Exception as e:
print(f"Error: {str(e)}")
end_time = time.time()
print(f"Run time: {end_time - start_time} seconds.")
if __name__ == "__main__":
main()