Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e663bb9

Browse files
fixed issues
1 parent fbe7d9b commit e663bb9

File tree

6 files changed

+437
-0
lines changed

6 files changed

+437
-0
lines changed

‎ConsoleSQL.py‎

Lines changed: 380 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,380 @@
1+
import os
2+
import shutil
3+
import errors
4+
5+
6+
def documentation():
7+
pass
8+
9+
10+
def create_database(database, *args):
11+
'''
12+
Console command
13+
CREATE DATABASE DataBaseName
14+
'''
15+
16+
try:
17+
18+
os.mkdir(f"databases/{database}"), os.mkdir(f"databases/{database}/files"), os.mkdir(f"databases/{database}/tables")
19+
20+
except FileExistsError:
21+
return "Database already exists"
22+
23+
return f"Database \"{database}\" was created"
24+
25+
26+
def use_database(database, *args):
27+
'''
28+
Console command
29+
USE DATABASE DataBaseName
30+
'''
31+
32+
if os.path.exists(f"databases/{database}/"):
33+
return [f"Currently working with database \"{database}\"", database]
34+
35+
raise errors.DataBaseNotFoundError(f"Database \"{database}\" not found!")
36+
37+
38+
def create_table(database, table, values, *args):
39+
'''
40+
Console command
41+
CREATE TABLE TableName(id: int, name: str, age: float, more...)
42+
'''
43+
44+
if os.path.exists(f"databases/{database}/tables/{table}.txt"):
45+
return f"Table already exists!"
46+
47+
table = open(f"databases/{database}/tables/{table}.txt", "a+")
48+
table.write(f"{values}\n\n")
49+
table.close()
50+
51+
return f"Table \"{table}\" was created!"
52+
53+
54+
def add_content_to_table(database, table, *content):
55+
'''
56+
Console command
57+
58+
ADD TableName VALUES (
59+
(id, name, age, more...)
60+
(id, name, age)
61+
);
62+
63+
'''
64+
65+
try:
66+
67+
with open(f"databases/{database}/tables/{table}.txt", "r") as file:
68+
69+
values = [line for line in file][0]
70+
values_dictionary = {}
71+
72+
for item in values[1:-2].split(", "):
73+
74+
key, value = item.split(": ")
75+
values_dictionary[key] = value
76+
77+
with open(f"databases/{database}/tables/{table}.txt", "a+") as write_file:
78+
79+
for content_list in content:
80+
81+
content_dict = {}
82+
83+
for index, item in enumerate(values_dictionary.keys()):
84+
85+
content_dict[item] = content_list[index]
86+
87+
if type(content_dict[item]) is int and values_dictionary[item] == "'int'" or \
88+
type(content_dict[item]) is str and values_dictionary[item] == "'str'" or \
89+
type(content_dict[item]) is float and values_dictionary[item] == "'float'":
90+
continue
91+
92+
raise errors.ItemValueDifferentThanTheSetValue(f"item \"{item}\" is type \'{type(content_dict[item])}\' and it must be \'{values_dictionary[item]}\'")
93+
94+
write_file.write(f"{content_dict}\n")
95+
96+
except Exception as e:
97+
raise e
98+
99+
return "Content added to table!"
100+
101+
102+
def create_file(database, file_name):
103+
'''
104+
Console command
105+
CREATE FILE FileName
106+
'''
107+
108+
if os.path.exists(f"databases/{database}/files/{file_name}.txt"):
109+
return "File already exists"
110+
111+
file = open(f"databases/{database}/files/{file_name}.txt", 'x')
112+
file.close()
113+
114+
return f"File \"{file_name}\" was created!"
115+
116+
117+
def write_in_file(database, file, *content):
118+
'''
119+
Console command
120+
WRITE IN FileName:
121+
Something isn't right.
122+
Some Messages!
123+
content, content, content,
124+
content, content,
125+
content,
126+
content,
127+
content;;;
128+
'''
129+
130+
if os.path.exists(f"databases/{database}/files/{file}.txt"):
131+
with open(f"databases/{database}/files/{file}.txt", "a+") as f:
132+
for line in content:
133+
f.write(f"{line}\n")
134+
135+
return "Content added to file!"
136+
137+
return f"Database \"{database}\" or File \"{file}\" not found!"
138+
139+
140+
def check_table_content(database, table, *args):
141+
'''
142+
Console command
143+
GET ALL TableName
144+
'''
145+
146+
if os.path.exists(f"databases/{database}/tables/{table}.txt"):
147+
file = open(f"databases/{database}/tables/{table}.txt", "r")
148+
149+
return [line for line in file][2:]
150+
151+
print("Table not found!")
152+
return []
153+
154+
155+
def check_file_content(database, file_name, *border):
156+
'''
157+
Console command
158+
GET FILE FileName
159+
'''
160+
161+
if os.path.exists(f"databases/{database}/files/{file_name}.txt"):
162+
file = open(f"databases/{database}/files/{file_name}.txt", "r")
163+
164+
return [line for line in file]
165+
166+
print("File not found!")
167+
return []
168+
169+
170+
def drop_database(*databases):
171+
'''
172+
Console command
173+
174+
One DataBase:
175+
FIRST WAY: DROP DB DataBaseName
176+
SECOND WAY: DROP DATABASE DataBaseName
177+
178+
More Than One DataBases:
179+
FIRST WAY: DROP DBS FirstDataBaseName SecondDataBaseName ThirdDataBaseName...
180+
SECOND WAY: DROP DATABASES FirstDataBaseName SecondDataBaseName ThirdDataBaseName...
181+
'''
182+
183+
for db in databases:
184+
if os.path.exists(f"databases/{db}/"):
185+
shutil.rmtree(f"databases/{db}/")
186+
187+
return "Database/s dropped!"
188+
189+
190+
def drop_table(database, *tables):
191+
'''
192+
Console command
193+
194+
One Table:
195+
DROP TABLE TableName
196+
197+
More Than One Table:
198+
DROP TABLES FirstTableName SecondTableName ThirdTableName...
199+
'''
200+
201+
for table in tables:
202+
if os.path.exists(f"databases/{database}/tables/{table}.txt"):
203+
os.remove(f"databases/{database}/tables/{table}.txt")
204+
205+
return "Table/s dropped!"
206+
207+
208+
def delete_file(database, *files):
209+
'''
210+
Console command
211+
212+
One File:
213+
DEL FILE TableName
214+
215+
More Than One File:
216+
DEL FILES FirstFileName SecondFileName ThirdFileName...
217+
'''
218+
219+
for file in files:
220+
if os.path.exists(f"databases/{database}/files/{file}.txt"):
221+
os.remove(f"databases/{database}/files/{file}.txt")
222+
223+
return "File/s deleted!"
224+
225+
226+
def code_saver(user_input, code_file, new_line):
227+
'''
228+
Saves the code in the code file.
229+
'''
230+
231+
file = open(f"src/{code_file}", "a+")
232+
file.write(f"{user_input}{new_line}")
233+
file.close()
234+
235+
236+
def run_program():
237+
while True:
238+
db = input("create or use database: ")
239+
240+
if db == 'create':
241+
create_db = input("database name: ")
242+
create_database(create_db)
243+
d = use_database(create_db)
244+
break
245+
246+
elif db == "use":
247+
d = use_database(input("database name: "))[-1]
248+
break
249+
250+
database = d
251+
252+
while True:
253+
file = input("Create or choose file where to save the code from your console experience:\n")
254+
255+
if not os.path.exists(f"src/{file}.txt"):
256+
f = open(f"src/{file}.txt", "x")
257+
f.close()
258+
259+
if file:
260+
break
261+
262+
file = f"{file}.txt"
263+
264+
while True:
265+
266+
operation_code = input()
267+
operation = operation_code.lower().split()
268+
269+
code_saver(operation_code, file, '\n')
270+
271+
if operation_code == "END":
272+
break
273+
274+
if operation_code == "docs":
275+
print(documentation().__doc__())
276+
277+
if len(operation) >= 3:
278+
if operation[-1]:
279+
280+
if operation[:-1] == ["create", "database"]:
281+
print(create_database(operation[-1]))
282+
283+
elif operation[:-1] == ["use", "database"]:
284+
285+
db = use_database(operation[-1])
286+
print(db[0])
287+
database = db[-1]
288+
289+
elif operation[:2] == ["create", "table"]:
290+
291+
table_name = ' '.join(operation[2:])[:' '.join(operation[2:]).index("(")]
292+
values = ' '.join(operation[2:])[' '.join(operation[2:]).index("(")+1:' '.join(operation[2:]).index(")")]
293+
values_tuple = values.split(", ")
294+
values_dict = {}
295+
296+
for items in values_tuple:
297+
298+
key, value = items.split(": ")
299+
values_dict[key] = value
300+
301+
print(create_table(database, table_name, values_dict))
302+
303+
elif operation[0] == "add" and operation[-2] == "values":
304+
305+
table = operation[1]
306+
307+
if operation[-1] == "(":
308+
309+
lst_values = []
310+
item = input()
311+
312+
while item != ");":
313+
314+
code_saver(item, file, '\n')
315+
items = item[1:-1].split(", ")
316+
317+
for index in range(len(items)):
318+
319+
if len(items[index].split(".")) == 2:
320+
if items[index].split(".")[0].isdigit() and items[index].split(".")[1].isdigit():
321+
items[index] = float(items[index])
322+
323+
elif items[index].isdigit():
324+
items[index] = int(items[index])
325+
326+
lst_values.append(items)
327+
item = input()
328+
329+
code_saver(item, file, '\n\n\n')
330+
print(add_content_to_table(database, table, *lst_values))
331+
332+
elif operation[:-1] == ["create", "file"]:
333+
print(create_file(database, operation[-1]))
334+
335+
elif operation[:-1] == ["write", "in"]:
336+
337+
content = []
338+
text = input()
339+
340+
while text[-3:] != ";;;":
341+
342+
code_saver(text, file, '\n')
343+
content.append(text)
344+
text = input()
345+
346+
content.append(text)
347+
348+
if operation[-1][-1] == ':':
349+
print(write_in_file(database, operation[-1][:-1], *content))
350+
351+
else:
352+
print(write_in_file(database, operation[-1], *content))
353+
354+
code_saver(content[-1], file, '\n\n\n')
355+
356+
elif operation[0] == "get" and operation[1] == "all":
357+
358+
lines = check_table_content(database, operation[-1])
359+
print()
360+
[print(line) for line in lines]
361+
362+
elif operation[:-1] == ["get", "file"]:
363+
364+
lines = check_file_content(database, operation[-1])
365+
print()
366+
[print(line) for line in lines]
367+
368+
elif operation[:-1] == ["drop", "db"] or operation[:-1] == ["drop", "database"] or operation[:2] == \
369+
["drop", "dbs"] or operation[:2] == ["drop", "databases"]:
370+
371+
dbs = operation[2:]
372+
print(drop_database(*dbs))
373+
374+
elif operation[:2] == ["drop", "table"] or operation[:2] == ["drop", "tables"]:
375+
print(drop_table(database, *operation[2:]))
376+
377+
elif operation[:2] == ["del", "file"] or operation[:2] == ["del", "files"]:
378+
print(delete_file(database, *operation[2:]))
379+
380+
code_saver('\n// everything bellow is made on new run.', file, '\n')

‎databases/mydb/files/myfile.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Everything is alright.
2+
I love to write in files!
3+
The End of This;;;

‎databases/mydb/tables/mytable.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{'id': 'int', 'name': 'str', 'age': 'int'}
2+
3+
{"'id'": 1, "'name'": 'gosheto', "'age'": 17}
4+
{"'id'": 5, "'name'": 'dimko', "'age'": 17}

‎errors.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class DataBaseNotFoundError(Exception):
2+
pass
3+
4+
5+
class ItemValueDifferentThanTheSetValue(Exception):
6+
pass

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /