Commit 12f8ca18 authored by Danielvdh03's avatar Danielvdh03
Browse files

Removed custom aggregate options plus removed prompt for is Null on create menu

parent 858333ce
......@@ -56,7 +56,7 @@ def generate_descriptions():
" A filtering query that only returns the minimum from the search",
" Exits the CLI",
" Removes rows from a Table based on criteria",
" Performs an SQL query without aggregate functions",
" Returns all results from an SQL Table",
" Returns the sum of all entries of a specific column",
" Returns all columns and their associated datatypes for a specific Table")
......@@ -70,7 +70,7 @@ class Database:
self.table_list = self.get_list_tables() # List of tables in Database
self.command_list = ("average", "clear", "count", "create", "delete", "help", "insert", "list", "max", "min", "quit", "remove", "search_filter", "sum", "view_column") # List of available user commands
self.command_list = ("average", "clear", "count", "create", "delete", "help", "insert", "list", "max", "min", "quit", "remove", "search_all", "sum", "view_column") # List of available user commands
self.command_description = generate_descriptions() # List of user command descriptions
# Returns column names and types for a given table (view_column)
......@@ -122,7 +122,8 @@ class Database:
print ("Make sure the fields you entered below are seperated by commas (ex. \"first_name, last_name, age\")")
input_string = input("Enter all fields that you want in table {0} seperated by commas : \n".format(table_name) )
input_string = input("Enter all fields that you want in table {0} seperated by"
" commas (each field can only be one word) : \n".format(table_name))
attributes = input_string.split(",")
attributes = trim(attributes) # Removes white space and unnecessary characters like , ' ( )
......@@ -130,21 +131,13 @@ class Database:
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
new_type = new_type.lower() # No longer case sensitive
new_type, new_length, new_floating = self.handle_type_insert(new_type)
type.append(new_type)
max_length.append(new_length)
num_floating.append(new_floating)
new_null = input("Does this attribute always have to be defined (Y/N) : ")
if new_null == "Y":
null.append("not null")
else:
null.append("")
user = input("Do you want to add a Primary Key to the table (Y/N) : ")
if user == "Y": # Adds primary key
......@@ -154,7 +147,7 @@ class Database:
pk = input_pk.split(",")
query = self.generate_create_query(table_name, attributes, type, max_length, num_floating, null, pk)
query = self.generate_create_query(table_name, attributes, type, max_length, num_floating, pk)
print(query) # For debugging purposes
......@@ -163,10 +156,9 @@ class Database:
print(results)
self.table_list.append(table_name) # Add to list of tables
# Generates create query based off user information provided
def generate_create_query(self, table_name: str, attributes: list, type: list, max_lengths: list, num_floating: list, null: list, pk: list):
def generate_create_query(self, table_name: str, attributes: list, type: list, max_lengths: list, num_floating: list, pk: list):
query = "create table {0} ( \n".format(table_name)
......@@ -175,7 +167,6 @@ class Database:
query += "{0} ".format(attribute)
type_string = self.format_type(type, max_length, num_floating)
query += "{0} ".format(type_string)
query += "{0} ".format(null)
if attribute == attributes[-1]:
......@@ -192,7 +183,7 @@ class Database:
return query
# Given a type, produces output that will be used in create query clause
def format_type(self, type: str, max_length: str, num_floating: str):
def format_type (self, type: str, max_length: str, num_floating: str):
if type == "int":
......@@ -352,7 +343,15 @@ class Database:
# Get a number of results that meet criteria based off filtering
def select_count(self):
pass
table_name = input("Select the table that you wish to count results for : ")
if table_name not in self.table_list:
print("Table {0} does not exist".format(table_name))
else:
pass
def select_type(self):
......@@ -364,41 +363,59 @@ class Database:
else:
field_names = ""
filter_criteria = []
choice = input("See all columns when querying or only select few (All/Few) : ")
choice = choice.strip()
choice = choice.lower()
query = "select * from {0}".format(table_name)
limit = input("Limit amount of search results (Y/N) : ")
if choice == "all":
if limit == "Y":
field_names += "*"
query += self.limit_results()
elif choice == "few":
result = self.execute_query(query)
print(result)
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")
# Creates constraints based off user inputs
def setup_constraints(self, table_name: str, column_names: list):
query = "select " + field_names + " from {0}".format(table_name)
print("Columns in table {0} are : ".format(table_name))
number = input("Enter the number of requirements (each requirement will be anded with each other)")
restrict = input ("Do you want to filter your search results by conditions? (Y/N)")
for x in range(number):
if restrict == "Y":
print("Requirement {0}\n".format(x+1))
self.setup_constraints(table_name, all)
# Creates constraints based off user inputs
def limit_results(self):
def setup_constraints(self, table_name: str, column_names: list):
pass
num = input("Maximum results to be returned : ")
return "LIMIT {0}".format(num)
def delete_records(self):
pass
table_name = input("Select table to delete records from : ")
if table_name not in self.table_list:
print("Table {0} does not exist".format(table_name))
else:
columns = self.get_column(table_name)
query = "delete from {0}".format(table_name)
option = input("Delete entire table or partial? (All/Partial) : ")
option = option.strip()
option = option.lower()
if option == "all": # Do nothing
pass
elif option == "partial":
query += self.setup_constraints(table_name, columns)
else:
print("Invalid option")
return
# Deletes Table and all associated records
def delete_table(self):
......@@ -437,9 +454,7 @@ class Database:
else:
print("Table name does not exist")
# Given a valid table name, prints out all atributes and associated types
def show_columns(self):
table_name = input("Please select table to view columns of : ")
......@@ -461,18 +476,16 @@ class Database:
type = cleanup_type(type)
print("{0},{1}".format(field_name, type))
# Prints out the list of all commands for help
def display_command_list(self):
print ("Command Name - Command Description")
print("Command Name - Command Description")
for command, description in zip(self.command_list, self.command_description):
print ("{0} - {1}".format(command, description))
print("{0} - {1}".format(command, description))
# Runs show tables and trims , from result
# Runs show tables and trims, from result
def get_list_tables(self):
cursor.execute("Show tables")
......@@ -508,7 +521,6 @@ if __name__ == "__main__":
username = os.environ.get("DB_USER")
passphrase = os.environ.get("DB_PASS")
dbname = os.environ.get("DB_NAME")
keepExecuting = True
try:
db = mysql.connect(
......@@ -532,9 +544,7 @@ if __name__ == "__main__":
cursor = db.cursor()
client = Database()
while keepExecuting:
while True:
command = input("Please select a command : ")
command = command.lower() # No longer case sensitive
command = command.strip()
......@@ -574,19 +584,7 @@ if __name__ == "__main__":
client.select_count()
elif command == "average": # TODO
pass
elif command == "sum": # TODO
pass
elif command == "min": # TODO
pass
elif command == "max": # TODO
pass
elif command == "search_filter": # TODO
elif command == "search_all": # TODO
client.select_type()
......@@ -598,6 +596,4 @@ if __name__ == "__main__":
print("Invalid command, please enter a valid command!")
db.close()
db.close()
\ No newline at end of file
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