diff --git a/ConsoleSQL.py b/ConsoleSQL.py index 170ac7f..0a6bb07 100644 --- a/ConsoleSQL.py +++ b/ConsoleSQL.py @@ -13,7 +13,7 @@ def create_database(database, *args): try: - os.mkdir(f"databases/{database}"), os.mkdir(f"databases/{database}/files"), os.mkdir(f"databases/{database}/tables") + os.mkdir(f"databases/{database}"), os.mkdir(f"databases/{database}/files"), os.mkdir(f"databases/{database}/tables"), os.mkdir(f"databases/{database}/queues") except FileExistsError: return Fore.LIGHTWHITE_EX + "Database already exists" @@ -138,6 +138,69 @@ def write_in_file(database, file, *content): return Fore.LIGHTWHITE_EX + f"Database \"{database}\" or File \"{file}\" not found!" +def create_queue(database, queue_file_name, *args): + ''' + Console command + + CREATE QUEUE QueueName + + or + + CREATE QUE QueueName + + or + + CREATE Q QueueName + ''' + + if os.path.exists(f"databases/{database}/queues/{queue_file_name}.txt"): + return Fore.LIGHTWHITE_EX + "Queue already exists" + + file = open(f"databases/{database}/queues/{queue_file_name}.txt", 'x') + file.close() + + return Fore.LIGHTWHITE_EX + f"Queue \"{queue_file_name}\" was created!" + + +def add_to_queue(database, queue, *items): + ''' + Console command + ADD TO QueueName firstItem secondItem thirdItem + ''' + + if os.path.exists(f"databases/{database}/queues/{queue}.txt"): + with open(f"databases/{database}/queues/{queue}.txt", "a+") as q: + for item in items: + q.write(f"{item}\n") + + return Fore.LIGHTWHITE_EX + "Items added to queue!" + + return Fore.LIGHTWHITE_EX + f"Database \"{database}\" or Queue \"{queue}\" not found!" + + +def remove_from_queue(database, queue, *args): + ''' + Console command + REMOVE FROM QueueName + ''' + + if os.path.exists(f"databases/{database}/queues/{queue}.txt"): + + q = [item for item in open(f"databases/{database}/queues/{queue}.txt", "r")][1:] + + os.remove(f"databases/{database}/queues/{queue}.txt") + f = open(f"databases/{database}/queues/{queue}.txt", "a+") + + for item in q: + f.write(f"{item}") + + f.close() + + return "First element from queue removed!" + + return f"Queue \"{queue}\" not found!" + + def check_table_content(database, table, *args): ''' Console command @@ -168,6 +231,19 @@ def check_file_content(database, file_name, *border): return [] +def check_queue_content(database, queue, *args): + ''' + Console command + GET QueueName + ''' + + if os.path.exists(f"databases/{database}/queues/{queue}.txt"): + q = [line for line in open(f"databases/{database}/queues/{queue}.txt", "r")] + return ', '.join(q) + + return f"Queue {queue} not found!" + + def delete_lines(database, path, file_name, *lines): ''' Console command @@ -271,6 +347,24 @@ def delete_file(database, *files): return Fore.LIGHTWHITE_EX + "File/s deleted!" +def delete_queue(database, *queues): + ''' + Console command + + One Queue: + DEL QUEUE QueueName + + More Than One Queue: + DEL QUEUES FirstQueueName SecondQueueName ThirdQueueName... + ''' + + for queue in queues: + if os.path.exists(f"databases/{database}/queues/{queue}.txt"): + os.remove(f"databases/{database}/queues/{queue}.txt") + + return Fore.LIGHTWHITE_EX + "Queue/s deleted!" + + def code_saver(user_input, code_file, new_line): ''' Saves the code in the code file. @@ -329,7 +423,7 @@ def run_program(): code_saver(operation_code, file, '\n') - if len(operation) >= 3: + if len(operation) >= 2: if operation[-1]: if operation[:-1] == ["create", "database"]: @@ -406,6 +500,15 @@ def run_program(): code_saver(text[-3:], file, '\n\n\n') + elif operation[0] == "create" and (operation[1] == "queue" or operation[1] == "que" or operation[1] == "q"): + print(create_queue(database, operation[-1])) + + elif operation[0] == "add" and operation[1] == "to": + print(add_to_queue(database, operation[2], *operation[3:])) + + elif operation[0] == "remove" and operation[1] == "from": + print(remove_from_queue(database, operation[-1])) + elif operation[0] == "get" and operation[1] == "all": lines = check_table_content(database, operation[-1]) @@ -418,6 +521,9 @@ def run_program(): print() [print(line) for line in lines] + elif operation[0] == "get": + print(check_queue_content(database, operation[-1])) + elif len(operation) >= 5: if operation[0] == "del" and operation[3] == "lines": try: @@ -440,3 +546,5 @@ def run_program(): if "lines" not in operation: print(delete_file(database, *operation[2:])) + elif operation[0] == "del" and operation[1] == "queue" or operation[1] == "queues": + print(delete_queue(database, *operation[2:])) diff --git a/ConsoleSQL_installer.zip b/ConsoleSQL_installer.zip new file mode 100644 index 0000000..ffc7f32 Binary files /dev/null and b/ConsoleSQL_installer.zip differ diff --git a/databases/mydb/queues/resturant.txt b/databases/mydb/queues/resturant.txt new file mode 100644 index 0000000..7d64c9a --- /dev/null +++ b/databases/mydb/queues/resturant.txt @@ -0,0 +1,5 @@ +client2 +client3 +client4 +client5 +client6 diff --git a/documentation.py b/documentation.py index 79144e7..ba2bfae 100644 --- a/documentation.py +++ b/documentation.py @@ -7,12 +7,17 @@ def documentation(): - Creating DataBase - Using/Changing DataBase -- Creating Tables and Files +- Creating Tables, Files and Queues - Writing in Tables and Files -- Checking the content of Tables and Files -- Deleting DataBases, Tables and Files +- Adding elements to Queues +- Checking the content of Tables, Files and Queues +- Deleting content from Tables, Files and Queues +- Deleting DataBases, Tables, Files and Queues + +- Can be used in python files. - Saves all the code in File. + {Fore.MAGENTA + "All commands can be in upper or lower case!"} {Fore.GREEN + "1.How to create DataBase:"} @@ -58,15 +63,43 @@ def documentation(): content ;;; -{Fore.GREEN + "5.How to see the content of my Tables and Files:"} +{Fore.GREEN + "5.How to create Queue:"} + - To create queue, you must use one of the following commands: + + {Fore.MAGENTA + "CREATE QUEUE QueueName"} + + or + + CREATE QUE QueueName + + or + + CREATE Q QueueName + + {Fore.RED + '"NOTE!" You cannot remove any element from queue, every time you try to remove element, it will remove only the first one!'} + +{Fore.GREEN + "6.How to add elements to queue:"} + - To add elements in THE END of the queue, you should use this command: + + {Fore.LIGHTMAGENTA_EX + "ADD TO QueueName firstItem secondItem thirdItem"} + +{Fore.GREEN + "7.How to remove element from queue:"} + - Use this command: + + REMOVE FROM QueueName + + {Fore.RED + '"NOTE!" You can only remove the first element in the queue!'} + +{Fore.GREEN + "8.How to see the content of my Tables, Files and Queues:"} - Use this command for Tables: GET ALL TableName - Use this command for Files: GET FILE FileName + - Use this command for Queues: GET QueueName -6.How to delete content from files/tables: +9.How to delete content from files/tables: - Use this command for Tables: DEL TABLE TableName LINES firstline secondline seventhline - Use this command for Files: DEL FILE FileName LINES firstline secondline seventhline -7.How to delete DataBases, Tables and Files: +10.How to delete DataBases, Tables, Files and Queues: - Delete DataBase/s: {Fore.MAGENTA + "One DataBase:"} @@ -93,9 +126,20 @@ def documentation(): More Than One File: DEL FILES FirstFileName SecondFileName ThirdFileName... + {Fore.GREEN + "- Delete Queues:"} + + {Fore.MAGENTA + "One Queue:"} + DEL QUEUE QueueName + + More Than One Queue: + DEL QUEUES FirstQueueName SecondQueueName ThirdQueueName... + +{Fore.LIGHTGREEN_EX + "11.How to use the ConsoleSQL in Python code?"} + - You can simply, just import the ConsoleSQL file and call the functions. + For example, see the test_usages/ folder. -{Fore.LIGHTGREEN_EX + "8.How to save the code?"} +{Fore.LIGHTGREEN_EX + "12.How to save the code?"} - The code is saving by itself in the chosen at the beginning by you file, to change the file you must stop the program and rerun it. The file can be found in the same directory "src/filename" diff --git a/readme.md b/readme.md index e5c6838..efb153c 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,26 @@ # Python-ConsoleSQL -This is an SQL for Python console. It's similar to MySql. +This is an SQL for Python console. It can be used in code too. + +### [Download Guide](#Download-guide) +### [Documentation](#Documentation) + + +# Download-guide + +**Go to the files above and click on ConsoleSQL_installer.zip, then click on view raw or download.** + +![Screenshot 2023-02-20 110454](https://user-images.githubusercontent.com/112943652/220061559-db5fc0cb-556f-4a66-90be-547d8322d00b.png) +![Screenshot 2023-02-20 110540](https://user-images.githubusercontent.com/112943652/220061591-54584fe9-cf62-4d99-b51f-3691ee4f2836.png) + + +# Documentation #### Run main.py to start the program. + #### To see the documentation, type "docs" on the console. +**This is what you're gonna see.** + +![Screenshot 2023-02-20 104656](https://user-images.githubusercontent.com/112943652/220061875-c2174bdf-adfa-4c63-9c40-0634180d7f21.png) +![Screenshot 2023-02-20 104735](https://user-images.githubusercontent.com/112943652/220061869-4544ef81-c73f-47a5-a418-0ac53f21d88a.png) +![Screenshot 2023-02-20 104806](https://user-images.githubusercontent.com/112943652/220061861-b48d93a0-5b35-4702-b297-ebca379e5393.png) +![Screenshot 2023-02-20 104839](https://user-images.githubusercontent.com/112943652/220061882-50771220-6d42-4c6e-b56f-1061db5f5c60.png) diff --git a/src/mydb.txt b/src/mydb.txt index 44be3f6..7194bc7 100644 --- a/src/mydb.txt +++ b/src/mydb.txt @@ -67,3 +67,11 @@ ADD testtable VALUES ( DEL TABLE testtable LINES 1 3 5 GET ALL testtable GET FILE testfile + +CREATE QUEUE resturant +ADD TO resturant client1 client2 client3 client4 client5 +ADD TO resturant client6 +REMOVE FROM resturant +GET resturant +CREATE QUEUE deletble +DEL QUEUE deletble diff --git a/test_usages/files/ConsoleSQL.py b/test_usages/files/ConsoleSQL.py new file mode 100644 index 0000000..c198fd7 --- /dev/null +++ b/test_usages/files/ConsoleSQL.py @@ -0,0 +1,432 @@ +import os +import shutil +from colorama import Fore + + +def create_database(database, *args): + ''' + Console command + CREATE DATABASE DataBaseName + ''' + + try: + + os.mkdir(f"databases/{database}"), os.mkdir(f"databases/{database}/files"), os.mkdir(f"databases/{database}/tables") + + except FileExistsError: + return Fore.LIGHTWHITE_EX + "Database already exists" + + return Fore.LIGHTWHITE_EX + f"Database \"{database}\" was created" + + +def use_database(database, *args): + ''' + Console command + USE DATABASE DataBaseName + ''' + + if os.path.exists(f"databases/{database}/"): + return [Fore.LIGHTWHITE_EX + f"Currently working with database \"{database}\"", database] + + return f"Database \"{database}\" not found!" + + +def create_table(database, table, values, *args): + ''' + Console command + CREATE TABLE TableName(id: int, name: str, age: float, more...) + ''' + + if os.path.exists(f"databases/{database}/tables/{table}.txt"): + return Fore.LIGHTWHITE_EX + f"Table already exists!" + + table = open(f"databases/{database}/tables/{table}.txt", "a+") + table.write(f"{values}\n\n") + table.close() + + return Fore.LIGHTWHITE_EX + f"Table \"{table.name.split('/')[-1][:-4]}\" was created!" + + +def add_content_to_table(database, table, *content): + ''' + Console command + + ADD TableName VALUES ( + (id, name, age, more...) + (id, name, age) + ); + + ''' + + try: + + with open(f"databases/{database}/tables/{table}.txt", "r") as file: + + values = [line for line in file][0] + values_dictionary = {} + + for item in values[1:-2].split(", "): + + key, value = item.split(": ") + values_dictionary[key] = value + + with open(f"databases/{database}/tables/{table}.txt", "a+") as write_file: + + for content_list in content: + + content_dict = {} + + for index, item in enumerate(values_dictionary.keys()): + + content_dict[item] = content_list[index] + + if type(content_dict[item]) is int and values_dictionary[item] == "'int'" or \ + type(content_dict[item]) is str and values_dictionary[item] == "'str'" or \ + type(content_dict[item]) is float and values_dictionary[item] == "'float'": + continue + + return f"item \"{item}\" is type \'{type(content_dict[item])}\' and it must be \'{values_dictionary[item]}\'" + + write_file.write(f"{content_dict}\n") + + except Exception as e: + raise e + + return Fore.LIGHTWHITE_EX + "Content added to table!" + + +def create_file(database, file_name): + ''' + Console command + CREATE FILE FileName + ''' + + if os.path.exists(f"databases/{database}/files/{file_name}.txt"): + return Fore.LIGHTWHITE_EX + "File already exists" + + file = open(f"databases/{database}/files/{file_name}.txt", 'x') + file.close() + + return Fore.LIGHTWHITE_EX + f"File \"{file_name}\" was created!" + + +def write_in_file(database, file, *content): + ''' + Console command + WRITE IN FileName: + Something isn't right. + + Some Messages! + content, content, content, + + content, content, + content, + content, + content + ;;; + ''' + + if os.path.exists(f"databases/{database}/files/{file}.txt"): + with open(f"databases/{database}/files/{file}.txt", "a+") as f: + for line in content: + f.write(f"{line}\n") + + return Fore.LIGHTWHITE_EX + "Content added to file!" + + return Fore.LIGHTWHITE_EX + f"Database \"{database}\" or File \"{file}\" not found!" + + +def check_table_content(database, table, *args): + ''' + Console command + GET ALL TableName + ''' + + if os.path.exists(f"databases/{database}/tables/{table}.txt"): + file = open(f"databases/{database}/tables/{table}.txt", "r") + + return [Fore.LIGHTWHITE_EX + line for line in file][2:] + + print(Fore.LIGHTWHITE_EX + "Table not found!") + return [] + + +def check_file_content(database, file_name, *border): + ''' + Console command + GET FILE FileName + ''' + + if os.path.exists(f"databases/{database}/files/{file_name}.txt"): + file = open(f"databases/{database}/files/{file_name}.txt", "r") + + return [Fore.LIGHTWHITE_EX + line for line in file] + + print(Fore.LIGHTWHITE_EX + "File not found!") + return [] + + +def delete_lines(database, path, file_name, *lines): + ''' + Console command + DEL TABLE/FILE FileName LINES firstline secondline seventhline + ''' + + if path == "table": + + if os.path.exists(f"databases/{database}/tables/{file_name}.txt"): + + file = [line[:-1] if line[-1] == '\n' else line for line in open(f"databases/{database}/tables/{file_name}.txt", "r")] + + for num, line in enumerate(lines): + if 0 <= (line+1)-num < len(file): + file.pop((line+1)-num) + + os.remove(f"databases/{database}/tables/{file_name}.txt") + f = open(f"databases/{database}/tables/{file_name}.txt", "a+") + + for line in file: + f.write(f"{line}\n") + + f.close() + + elif path == "file": + if os.path.exists(f"databases/{database}/files/{file_name}.txt"): + + file = [line[:-1] if line[-1] == '\n' else line for line in open(f"databases/{database}/files/{file_name}.txt", "r")] + + for num, line in enumerate(lines): + if 0 <= (line - 1) - num < len(file): + file.pop((line - 1) - num) + + os.remove(f"databases/{database}/files/{file_name}.txt") + f = open(f"databases/{database}/files/{file_name}.txt", "a+") + + for line in file: + f.write(f"{line}\n") + + f.close() + + else: + return Fore.LIGHTWHITE_EX + f"Invalid path name '{path}'" + + return Fore.LIGHTWHITE_EX + "lines removed!" + + +def drop_database(*databases): + ''' + Console command + + One DataBase: + FIRST WAY: DROP DB DataBaseName + SECOND WAY: DROP DATABASE DataBaseName + + More Than One DataBases: + FIRST WAY: DROP DBS FirstDataBaseName SecondDataBaseName ThirdDataBaseName... + SECOND WAY: DROP DATABASES FirstDataBaseName SecondDataBaseName ThirdDataBaseName... + ''' + + for db in databases: + if os.path.exists(f"databases/{db}/"): + shutil.rmtree(f"databases/{db}/") + + return Fore.LIGHTWHITE_EX + "Database/s dropped!" + + +def drop_table(database, *tables): + ''' + Console command + + One Table: + DROP TABLE TableName + + More Than One Table: + DROP TABLES FirstTableName SecondTableName ThirdTableName... + ''' + + for table in tables: + if os.path.exists(f"databases/{database}/tables/{table}.txt"): + os.remove(f"databases/{database}/tables/{table}.txt") + + return Fore.LIGHTWHITE_EX + "Table/s dropped!" + + +def delete_file(database, *files): + ''' + Console command + + One File: + DEL FILE FileName + + More Than One File: + DEL FILES FirstFileName SecondFileName ThirdFileName... + ''' + + for file in files: + if os.path.exists(f"databases/{database}/files/{file}.txt"): + os.remove(f"databases/{database}/files/{file}.txt") + + return Fore.LIGHTWHITE_EX + "File/s deleted!" + + +def code_saver(user_input, code_file, new_line): + ''' + Saves the code in the code file. + ''' + + file = open(f"src/{code_file}", "a+") + file.write(f"{user_input}{new_line}") + file.close() + + +def run_program(): + + while True: + db = input(Fore.LIGHTWHITE_EX + "create or use database: ") + + if db == 'create': + create_db = input(Fore.LIGHTWHITE_EX + "database name: ") + create_database(create_db) + d = use_database(create_db) + break + + elif db == "use": + d = use_database(input(Fore.LIGHTWHITE_EX + "database name: "))[-1] + break + + database = d + + while True: + file = input(Fore.LIGHTWHITE_EX + "Create or choose file where to save the code from your console experience:\n") + + if not os.path.exists(f"src/{file}.txt"): + f = open(f"src/{file}.txt", "x") + f.close() + + if file: + break + + file = f"{file}.txt" + + while True: + + operation_code = input() + + if operation_code == "END": + break + + operation = operation_code.lower().split() + + code_saver(operation_code, file, '\n') + + if len(operation) >= 3: + if operation[-1]: + + if operation[:-1] == ["create", "database"]: + print(create_database(operation[-1])) + + elif operation[:-1] == ["use", "database"]: + + db = use_database(operation[-1]) + print(db[0]) + database = db[-1] + + elif operation[:2] == ["create", "table"]: + + table_name = ' '.join(operation[2:])[:' '.join(operation[2:]).index("(")] + values = ' '.join(operation[2:])[' '.join(operation[2:]).index("(")+1:' '.join(operation[2:]).index(")")] + values_tuple = values.split(", ") + values_dict = {} + + for items in values_tuple: + + key, value = items.split(": ") + values_dict[key] = value + + print(create_table(database, table_name, values_dict)) + + elif operation[0] == "add" and operation[-2] == "values": + + table = operation[1] + + if operation[-1] == "(": + + lst_values = [] + item = input() + + while item != ");": + + code_saver(item, file, '\n') + items = item[1:-1].split(", ") + + for index in range(len(items)): + + if len(items[index].split(".")) == 2: + if items[index].split(".")[0].isdigit() and items[index].split(".")[1].isdigit(): + items[index] = float(items[index]) + + elif items[index].isdigit(): + items[index] = int(items[index]) + + lst_values.append(items) + item = input() + + code_saver(item, file, '\n\n\n') + print(add_content_to_table(database, table, *lst_values)) + + elif operation[:-1] == ["create", "file"]: + print(create_file(database, operation[-1])) + + elif operation[:-1] == ["write", "in"]: + + content = [] + text = input() + + while text[-3:] != ";;;": + + code_saver(text, file, '\n') + content.append(text) + text = input() + + if operation[-1][-1] == ':': + print(write_in_file(database, operation[-1][:-1], *content)) + + else: + print(write_in_file(database, operation[-1], *content)) + + code_saver(text[-3:], file, '\n\n\n') + + elif operation[0] == "get" and operation[1] == "all": + + lines = check_table_content(database, operation[-1]) + print() + [print(line) for line in lines] + + elif operation[:-1] == ["get", "file"]: + + lines = check_file_content(database, operation[-1]) + print() + [print(line) for line in lines] + + elif len(operation) >= 5: + if operation[0] == "del" and operation[3] == "lines": + try: + + print(delete_lines(database, operation[1], operation[2], *[int(l) for l in operation[4:]])) + + except ValueError: + print("line must be integer") + + elif operation[:-1] == ["drop", "db"] or operation[:-1] == ["drop", "database"] or operation[:2] == \ + ["drop", "dbs"] or operation[:2] == ["drop", "databases"]: + + dbs = operation[2:] + print(drop_database(*dbs)) + + elif operation[:2] == ["drop", "table"] or operation[:2] == ["drop", "tables"]: + print(drop_table(database, *operation[2:])) + + elif operation[:2] == ["del", "file"] or operation[:2] == ["del", "files"]: + if "lines" not in operation: + print(delete_file(database, *operation[2:])) + diff --git a/test_usages/files/__pycache__/ConsoleSQL.cpython-39.pyc b/test_usages/files/__pycache__/ConsoleSQL.cpython-39.pyc new file mode 100644 index 0000000..7d42b6e Binary files /dev/null and b/test_usages/files/__pycache__/ConsoleSQL.cpython-39.pyc differ diff --git a/test_usages/working_with_files.py b/test_usages/working_with_files.py new file mode 100644 index 0000000..d702f2a --- /dev/null +++ b/test_usages/working_with_files.py @@ -0,0 +1,10 @@ +import files.ConsoleSQL as CS + +CS.create_database("testdatabase") +CS.create_file("testdatabase", "testfile") +CS.write_in_file("testdatabase", "testfile", "firstLine", "secondLine", "thirdLine", "fourthLine") +CS.delete_lines("testdatabase", "file", "testfile", 1) +[print(line) for line in CS.check_file_content("testdatabase", "testfile")] +lines = [line for line in open("databases/testdatabase/files/testfile.txt", "r")][:-1] +[print(line) for line in lines] +CS.drop_database("testdatabase")