Commit 858333ce authored by Danielvdh03's avatar Danielvdh03
Browse files

Partially implementation to search_filter + documentation

parent 5a32aa0e
......@@ -8,7 +8,7 @@ import mysql.connector as mysql
# Removes ( ) ' , characters from passed in string and returns it
# Used to neatly format table list
def trim_string(name):
def trim_string (name: str):
name = str(name)
......@@ -19,8 +19,8 @@ def trim_string(name):
return name
def trim(old_list):
# Removes leading/trailing whitespace from all elements in a list and returns updated list
def trim(old_list : list):
new_list = []
......@@ -33,12 +33,13 @@ def trim(old_list):
return new_list
def cleanup_type(string):
string = str(string)
string = string.replace('\'','') #removes quotation
string = string[1:] # removes b
return string
def cleanup_type(text):
text = str(text)
text = text.replace('\'','') #removes quotation
text = text[1:] # removes b
return text
# Hardcoded descriptions for when user runs help command
def generate_descriptions():
......@@ -73,7 +74,7 @@ class Database:
self.command_description = generate_descriptions() # List of user command descriptions
# Returns column names and types for a given table (view_column)
def get_column_and_type(self, table_name):
def get_column_and_type(self, table_name: str):
cursor.execute("Show columns from {0}" .format(table_name))
results = cursor.fetchall()
......@@ -88,7 +89,7 @@ class Database:
# print(fields)
return fields, type
def get_column(self, table_name):
def get_column(self, table_name: str):
cursor.execute("Show columns from {0}".format(table_name))
results = cursor.fetchall()
......@@ -113,7 +114,6 @@ class Database:
else:
type = []
max_length = []
num_floating = []
......@@ -127,8 +127,7 @@ class Database:
attributes = trim(attributes) # Removes white space and unnecessary characters like , ' ( )
for attribute in attributes: # Gets specifications for each attribute
for attribute in attributes: # Gets specific info on each attribute
new_type = input("Enter the type of attribute {0} (Integer, Decimal, Date, Character, Boolean) : ".format(attribute))
new_type = new_type.lower() #No longer case sensitive
......@@ -148,7 +147,7 @@ class Database:
user = input("Do you want to add a Primary Key to the table (Y/N) : ")
if user == "Y":
if user == "Y": # Adds primary key
print("Attributes created are : {0}".format(attributes))
input_pk = input("Enter attributes that you wish to use as Primary Key (separate by commas) : \n")
......@@ -167,13 +166,12 @@ class Database:
# Generates create query based off user information provided
def generate_create_query(self, table_name, attributes, type, max_lengths, num_floating, null, pk):
def generate_create_query(self, table_name: str, attributes: list, type: list, max_lengths: list, num_floating: list, null: list, pk: list):
query = "create table {0} ( \n".format(table_name)
for attribute, type, max_length, num_floating, null in zip(attributes, type, max_lengths, num_floating, null):
query += "{0} ".format(attribute)
type_string = self.format_type(type, max_length, num_floating)
query += "{0} ".format(type_string)
......@@ -181,22 +179,20 @@ class Database:
if attribute == attributes[-1]:
if len(pk)!=0:
if len(pk) != 0:
query +=",\n"
query += ",\n"
pk_string = self.handle_pk_string(pk)
query +="PRIMARY KEY ({0})".format(pk_string)
query += "PRIMARY KEY ({0})".format(pk_string)
else:
query +=",\n"
query += ",\n"
query +=");"
query += ");"
return query
# Given a type, produces output that will be used in create query clause
def format_type(self, type, max_length, num_floating):
def format_type(self, type: str, max_length: str, num_floating: str):
if type == "int":
......@@ -224,8 +220,8 @@ class Database:
# Date -> dateTime
# Character -> varchar + ask user for length
# Boolean -> tinyint(1)
def handle_type_insert(self, type):
# Based off the attribute this function drives what to ask the user
def handle_type_insert(self, type: str):
if type == "integer":
......@@ -239,7 +235,6 @@ class Database:
return "decimal", num_digits, num_decimal
elif type == "date":
return "dateTime", 0, 0
......@@ -259,7 +254,7 @@ class Database:
return "error", -1, -1
# Formats the Primary Key clause in the Create Table command
def handle_pk_string(self, pk):
def handle_pk_string(self, pk: list):
pk_string = ""
......@@ -334,7 +329,7 @@ class Database:
# From passed in parameters generates final query
def generate_insert_query(self, table_name, column_names, attributes):
def generate_insert_query(self, table_name: str, column_names: str, attributes: list):
query = "insert into {0}".format(table_name)
query += column_names
......@@ -360,8 +355,48 @@ class Database:
pass
def select_type(self):
table_name = input("Select the table that you wish to query : ")
if table_name not in self.table_list:
print("Table {0} does not exist".format(table_name))
else:
field_names = ""
filter_criteria = []
choice = input("See all columns when querying or only select few (All/Few) : ")
choice = choice.strip()
choice = choice.lower()
if choice == "all":
field_names += "*"
elif choice == "few":
all = self.get_column(table_name)
print("Attributes are {0}".format(all))
field_names += input("Select all attributes which you want to query from (seperate by comma)\n")
else:
print("Invalid option")
query = "select " + field_names + " from {0}".format(table_name)
restrict = input ("Do you want to filter your search results by conditions? (Y/N)")
if restrict == "Y":
self.setup_constraints(table_name, all)
# Creates constraints based off user inputs
def setup_constraints(self, table_name: str, column_names: list):
pass
def delete_records(self):
pass
......@@ -416,7 +451,9 @@ class Database:
field_names, types = self.get_column_and_type(table_name)
else:
print("Table does not exist")
return
print("Field Name, Type")
for field_name, type in zip(field_names, types):
......@@ -453,7 +490,7 @@ class Database:
print(table)
return
def execute_query(self, query):
def execute_query(self, query: str):
try:
cursor.execute(query)
......@@ -465,9 +502,6 @@ class Database:
print("Something went wrong: {}".format(err))
if __name__ == "__main__":
dotenv.load_dotenv(dotenv.find_dotenv())
......@@ -513,7 +547,6 @@ if __name__ == "__main__":
# implement "SHOW TABLES"in SQL
client.print_tables()
elif command == "create": # Done
client.create_table()
......@@ -565,7 +598,6 @@ if __name__ == "__main__":
print("Invalid command, please enter a valid command!")
db.close()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment