Commit 2e921685 authored by Danielvdh03's avatar Danielvdh03
Browse files

Added commands plus removed unnessary code

parent 31cd57b7
......@@ -72,6 +72,7 @@ class Database:
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
self.state_id = self.setup_stateid_table()
# Returns column names and types for a given table (view_column)
def get_column_and_type(self, table_name: str):
......@@ -102,6 +103,20 @@ class Database:
# print(fields)
return fields
def setup_stateid_table(self):
query = "select id, name from state;"
results = self.execute_query(query)
state_id = {}
for row in results:
state_id[row[0]] = row[1]
return state_id
# Creates a Table based off user input
def create_table(self):
......@@ -340,110 +355,12 @@ class Database:
return query
# Get a number of results that meet criteria based off filtering
def select_count(self):
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):
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:
query = "select * from {0}".format(table_name)
limit = input("Limit amount of search results (Y/N) : ")
if limit == "Y":
query += self.limit_results()
result = self.execute_query(query)
print(result)
def limit_results(self):
num = input("Maximum results to be returned : ")
return "LIMIT {0}".format(num)
def delete_records(self):
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):
table_name = input("Name of table to delete : ")
empty_list = []
if table_name in self.table_list:
query = "drop table {0}".format(table_name)
result = self.execute_query(query)
print(result)
self.table_list.remove(table_name)
if result == empty_list:
print("Operation Successful!")
else:
print("Table name does not exist")
# Removes all records in the Table but keeps schema
def clear_table(self):
table_name = input("Name of table to delete all records from:")
empty_list = []
if table_name in self.table_list:
cursor.execute("Truncate table {0}".format(table_name))
result = cursor.fetchall()
if result == empty_list:
print("Operation Successful!")
else:
print("Table name does not exist")
# Given a valid table name, prints out all atributes and associated types
def show_columns(self):
......@@ -476,13 +393,6 @@ 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
......@@ -500,9 +410,24 @@ class Database:
def get_votes_state(self):
state = input("Enter state that you wish to filter by : ")
state = input("Enter state that you wish to filter by or All : ")
state = state.strip()
if state == "All":
query = "select name, total_votes from state order by total_votes desc ;"
results = self.execute_query(query)
counter = 1
for element in results:
if element[0] == "United States":
print("Total Votes in the United States : {0}".format(element[1]))
else:
print("{0}. State : {1}, Total Votes : {2}".format(counter, element[0],element[1]))
counter += 1
return
query = "select total_votes from state where name =\"{0}\"".format(state)
results = self.execute_query(query)
......@@ -511,7 +436,48 @@ class Database:
print("Number of votes from {0} is {1}".format(state,output))
def find_user(self):
pass
state = input("Enter the state to search in : ")
state = state.strip()
county = input("Enter the county in {0} to search in or (All):".format(state))
county = county.strip()
min_follower_count = input("Enter minimum follower count to query by : ")
min_follower_count = min_follower_count.strip()
max_follower_count = input("Emter maximum follower count to query by (leave blank for infinite) : ")
max_follower_count = max_follower_count.strip()
limit = input("All results or limit results (All/Limit) : ")
limit = limit.strip()
limit = limit.lower()
limit_string = ""
if limit == "limit":
limit_string += self.limit_results()
# TODO implement limit_string to query
if max_follower_count == "":
query = "select display_name, follower_count from user where follower_count > {0} AND" \
" county_id = (select id from county where name = \"{1}\" and state_id = (select id from state where name = \"{2}\" ));"\
.format( min_follower_count, county, state)
else:
query = "select display_name, follower_count from user where follower_count between {0} and {1} AND" \
" county_id = (select id from county where name = \"{2}\" and state_id = (select id from state where name = \"{3}\" ));".format(min_follower_count, max_follower_count,
county, state)
print(query)
results = self.execute_query(query)
if len(results) == 0:
print("No results returned")
else:
for result in results:
print("Username : {0}, Follower Count : {1}".format(result[0], result[1]))
return
def results_county(self):
......@@ -537,10 +503,112 @@ class Database:
print("{0}. Candidate Name : {1} Party Affiliation : {2} Vote Count : {3}".format(counter, result[0], result[1], result[2]))
counter +=1
def sanitize_categories(self, input: str):
input = input.lower()
if input =="voting age":
return "voting_age"
elif input =="income per capita":
return "income_per_cap"
elif input == "child poverty":
return "child_poverty"
elif input == "work from home":
return "work_from_home"
elif input == "private work":
return "private_work"
elif input == "public work":
return "public_work"
elif input == "self employed":
return "self_employed"
elif input == "family work":
return "family_work"
else:
return input
def group_county_by(self):
state = input("Select state to filter results by or (All) :")
state = state.strip()
print("Statistics list is : \nCases, Deaths, Population, Men, Women, Hispanic, White, Black, Native, \n"
"Asian, Pacific, Voting Age, Income, Income per Capita, Poverty, Child Poverty, Professional, \n"
"Service, Office, Construction, Production, Drive, Carpool, Transit, Unemployed \n"
"Walk, Work From Home, Employed, Private Work, Public Work, Self Employed, Family Work, \n")
old_choice = input("Select statistic to query by : ")
old_choice = old_choice.strip()
choice = self.sanitize_categories(old_choice)
limit = input("All results or limit results (All/Limit) : ")
limit = limit.strip()
limit = limit.lower()
limit_string = ""
if limit == "limit":
limit_string += self.limit_results()
order = input("Ascending or Descending Order (Asc/Desc) : ")
order = order.strip()
order = order.lower()
order_string = ""
if order == "asc":
order_string = "order by {0} asc ".format(choice)
else:
order_string = "order by {0} desc ".format(choice)
query = ""
if state == "All":
query+= "select name, {0}, state_id from county group by id ".format(choice)
else:
query+= "select name, {0}, state_id from county where state_id = (select id from state where name = \"{1}\") group by id ".format(choice, state)
query += order_string
query += limit_string
query += ";"
print(query)
results = self.execute_query(query)
if len(results)== 0:
print("No results returned")
counter = 1
for result in results:
state_name = self.state_id[result[2]]
print("{0}. County : {1}, State : {2}, {3} : {4}".format(counter, result[0], state_name, old_choice, result[1]))
counter += 1
#TODO Match Candidate_name to hashtag
def get_tweet_candidate(self):
candidate_name = input("Enter the candidate name : ")
candidate_name = candidate_name.strip()
state = input("Enter the name of the state : ")
state = state.strip()
region = input("Enter the county you wish to query by : ")
region = region.strip()
likes = input("Input the minimum amount of likes to query by")
likes = likes.strip()
query = "select display_name, tweet, likes from user inner join tweet on tweet.user_id=user.id where tweet > {0} and hashtag = \"{1}\"" \
" and county_id = (select id from county where name = \"{2}\" and state_id = (select id from state where name = \"{3}\")) ;" .format(likes, candidate_name, region, state )
results = self.execute_query(query)
if len(results) == 0:
print("No results were returned")
return
print("Results for the county {0} in the state of {1}".format(region,state))
for result in results:
print("Username : {0}, Tweet : {1}, Likes : {2} ".format(result[0], result[1], result[2]))
if __name__ == "__main__":
......@@ -580,32 +648,10 @@ if __name__ == "__main__":
break
elif command == "create": # Done
client.create_table()
elif command == "insert": # Done
client.add_records()
elif command == "clear": # Done
client.clear_table()
elif command == "delete": # Done
client.delete_table()
elif command == "remove": #TODO
client.delete_records()
elif command == "view_column": # Done
client.show_columns()
elif command == "count": # TODO
client.select_count()
elif command == "search_all": # TODO
......@@ -627,6 +673,15 @@ if __name__ == "__main__":
client.results_county()
elif command == "group_county_by":
client.group_county_by()
elif command == "get_tweet_candidate":
client.get_tweet_candidate()
elif command == "count_tweet_region":
pass
else:
print("Invalid command, please enter a valid command!")
......
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