Commit a8e6c12f authored by Danielvdh03's avatar Danielvdh03
Browse files

Enabled Dynamic Table Lookup and added some documentation

parent 85959caa
......@@ -6,8 +6,10 @@ import dotenv
import mysql.connector as mysql
# Removes ( ) ' , characters from passed in string and returns it
# Used to neatly format table list
def trim_string(name):
# Removes ( ) ' , characters from passed in string and returns it
name = str(name)
name = name.replace('(','')
......@@ -18,6 +20,19 @@ def trim_string(name):
return name
def trim(old_list):
new_list = []
for element in old_list:
element = str(element)
element = element.strip()
element = trim_string(element)
new_list.append(element)
return new_list
def cleanup_type(string):
string = str(string)
......@@ -25,6 +40,7 @@ def cleanup_type(string):
string = string[1:] # removes b
return string
# Hardcoded descriptions for when user runs help command
def generate_descriptions():
descriptions = (" A filtering query that returns the average from a specific column ",
......@@ -45,27 +61,24 @@ def generate_descriptions():
return descriptions
# Database class : Used for interacting with the Database and performing queries
class Database:
# Creates a blank table with attributes
def __init__(self):
# Initial list of tables to be defined
self.table_list = ["TeamInfo", "Game", "GameGoals", "GameOfficials", "GamePenalties", "GamePlays", "GamePlaysPlayers", "GameShifts", "PlayerInfo", "TeamInfo"] # List all tables in the database
# TODO: Update list with final Relational Model Tables
self.command_list = ("average", "clear", "count", "create", "delete", "help", "insert", "list", "max", "min", "quit", "remove", "search_filter", "sum", "view_column")
self.table_list = self.get_list_tables() # List of tables in Database
self.command_description = generate_descriptions()
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_description = generate_descriptions() # List of user command descriptions
# Returns column names and types for a given table (view_column)
def get_column_names(self, table_name):
cursor.execute("Show columns from {0}" .format(table_name))
results = cursor.fetchall()
fields = [] # field name
type = [] # type
null = [] # Yes->Can be Null or No->Can't be Null
key = []
for result in results:
......@@ -75,8 +88,7 @@ class Database:
print(fields)
return fields, type
# Creates a Table based off user input
def create_table(self):
table_name = input("Enter name of new table : ")
......@@ -89,7 +101,6 @@ class Database:
else:
attributes = []
type = []
max_length = []
num_floating = []
......@@ -101,16 +112,7 @@ class Database:
input_string = input("Enter all fields that you want in table {0} seperated by commas : \n".format(table_name) )
attributes = input_string.split(",")
new_list = []
# Removes unnecessary leading or ending whitespaces
for attribute in attributes:
attribute = attribute.strip()
new_list.append(attribute)
attributes = new_list
attributes = trim(attributes) # Removes white space and unnecessary characters like , ' ( )
print(attributes)
......@@ -132,27 +134,28 @@ class Database:
else:
null.append("")
user = input("Do you want to add a Primary Key to the table (Y/N)")
user = input("Do you want to add a Primary Key to the table (Y/N) : ")
if user == "Y":
print("Attributes created are : {0}".format(attributes))
input_pk = input("Enter attributes that you wish to use as Primary Key (separate by commas and no spaces) : \n")
input_pk = input("Enter attributes that you wish to use as Primary Key (separate by commas : \n")
pk = input_pk.split(",")
query = self.generate_create_query(table_name, attributes, type, max_length, num_floating, null, pk)
print(query)
print(query) # For debugging purposes
cursor.execute(query)
results = cursor.fetchAll()
print(results)
# Generates create query based off user information provided
def generate_create_query(self, table_name, attributes, type, max_lengths, num_floating, null, pk):
query = "create table {0} ( \n".format(table_name)
counter = 0
for attribute, type, max_length, num_floating, null in zip(attributes, type, max_lengths, num_floating, null):
......@@ -182,7 +185,7 @@ class Database:
query +=");"
return query
# Returns datatype for query
# Given a type, produces output that will be used in create query clause
def format_type(self, type, max_length, num_floating):
if type == "int":
......@@ -245,6 +248,7 @@ class Database:
return "error", -1, -1
# Formats the Primary Key clause in the Create Table command
def handle_pk_string(self, pk):
pk_string = ""
......@@ -316,6 +320,9 @@ 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 : ")
......@@ -336,6 +343,8 @@ class Database:
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")
......@@ -344,6 +353,24 @@ class Database:
print ("{0} - {1}".format(command, description))
# Runs show tables and trims , from result
def get_list_tables(self):
cursor.execute("Show tables")
result = cursor.fetchall()
return trim(result)
# Prints list of all tables
def print_tables(self):
tables = self.table_list
for table in tables:
print(table)
return
if __name__ == "__main__":
......@@ -381,6 +408,7 @@ if __name__ == "__main__":
command = input("Please select a command : ")
command = command.lower() # No longer case sensitive
command = command.strip()
if command == "quit": # Done
......@@ -388,14 +416,10 @@ if __name__ == "__main__":
elif command == "list": # Done
# implement "SHOW TABLES"in SQL
cursor.execute("Show tables")
result = cursor.fetchall()
client.print_tables()
for table in result:
table = trim_string(table)
print(table)
elif command == "create": #TODO
elif command == "create": # Done
client.create_table()
......
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