Commit dad508a3 authored by Danielvdh03's avatar Danielvdh03
Browse files

Finished Create, pending a few bugs

parent 34471517
......@@ -88,7 +88,7 @@ class Database:
else:
print("Creating table named {0}".format(table_name))
executing = True
attributes = []
type = []
......@@ -99,11 +99,11 @@ class Database:
while executing:
var = input("Continue inputting attributes (Y/N)")
var = input("Continue inputting attributes into {0} (Y/N) : ".format(table_name))
if var == "N" and attributes.length <2: # Not enough attributes
if var == "N" and len(attributes) < 2: # Not enough attributes
print("Only {0} attributes. Need at least 2 attributes to create table.".format(attributes.length))
print("Only {0} attributes. Need at least 2 attributes to create table.".format(len(attributes)))
elif var == "N": # Enough attributes leaving....
......@@ -113,16 +113,16 @@ class Database:
new_attribute = input("Enter the name of the next attribute to add : ")
new_type = input("Enter the type of attribute {0} (Integer, Decimal, Date, Character, Boolean".format(new_attribute))
new_type = input("Enter the type of attribute {0} (Integer, Decimal, Date, Character, Boolean) : ".format(new_attribute))
new_type = new_type.lower() #No longer case sensitive
new_type, new_length, new_floating = self.handle_type_insert(new_type)
attributes.append(new_attribute)
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)")
new_null = input("Does this attribute always have to be defined (Y/N) : ")
if new_null == "Y":
null.append("not null")
......@@ -137,40 +137,98 @@ class Database:
print("Invalid command. Make sure to enter either \"Y\" or \"N\"")
no_pk = True
no_pk = True
while no_pk:
while no_pk:
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":
if user == "Y":
variable = input("Enter attribute to append to PK : ")
variable = input("Enter attribute to append to PK : ")
pk.append(variable)
if variable in attributes:
pk.append(variable)
else:
break
if pk.length == 0: #If no Primary Key assigned just make first attribute PK
print("Error attribute {0} does not exist.")
print("Attributes that exist are {0}".format(attributes))
else:
break
self.generate_create_query(table_name, attributes, type, max_length, num_floating, null, pk)
# 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):
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]:
if len(pk)==0:
break
else:
query +=",\n"
pk_string = self.handle_pk_string(pk)
query +="PRIMARY KEY ({0})".format(pk_string)
pk.append(attributes[0])
print(attributes)
print(type)
print(max_length)
print(num_floating)
print(null)
print(pk)
else:
query +=",\n"
query +=");"
print(query)
# Returns datatype for query
def format_type(self, type, max_length, num_floating):
if type == "int":
return "int"
elif type == "decimal":
return "decimal({0},{1})".format(max_length, num_floating)
elif type == "dateTime":
return "dateTime"
elif type == "varchar":
return "varchar({0})".format(max_length)
elif type == "tinyint":
return "tinyint(1)"
# Given all relevant attributes, returns query to create table
# Supported types :
# Integer -> int
# Decimal -> decimal + ask user for length + ask user for number of decimal digits
# Date -> dateTime
# Character -> char + ask user for length
# Character -> varchar + ask user for length
# Boolean -> tinyint(1)
def handle_type_insert(self, type):
......@@ -206,12 +264,20 @@ class Database:
return "error", -1, -1
def handle_PK_query(self, pk):
pk_string = ""
for element in pk:
if element == pk[-1]:
pk_string += element
else:
pk_string += "{0} ,".format(element)
def handle_create_table(self, attributes, type, max_length, p_key, f_key,):
attributes = []
type = []
......@@ -338,6 +404,7 @@ if __name__ == "__main__":
while keepExecuting:
command = input("Please select a command : ")
command = command.lower() # No longer case sensitive
if command == "quit": # Done
......@@ -395,7 +462,7 @@ if __name__ == "__main__":
client.select_type()
elif command == "help": #TODO
elif command == "help": # Done
client.display_command_list()
......
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