Commit 6d87d8af authored by Danielvdh03's avatar Danielvdh03
Browse files

Added more commands, remove archived code

parent a85f0b19
......@@ -74,20 +74,6 @@ class Database:
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):
cursor.execute("Show columns from {0}" .format(table_name))
results = cursor.fetchall()
fields = [] # field name
type = [] # type
for result in results:
fields.append(result[0])
type.append(result[1])
# print(fields)
return fields, type
def get_column(self, table_name: str):
......@@ -172,188 +158,6 @@ class Database:
# 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, 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)
if attribute == attributes[-1]:
if len(pk) != 0:
query += ",\n"
pk_string = self.handle_pk_string(pk)
query += "PRIMARY KEY ({0})".format(pk_string)
else:
query += ",\n"
query += ");"
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):
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 -> varchar + ask user for length
# Boolean -> tinyint(1)
# Based off the attribute this function drives what to ask the user
def handle_type_insert(self, type: str):
if type == "integer":
return "int", 0, 0
elif type == "decimal":
num_digits = input("Enter number of digits for this decimal number : ")
num_decimal = input("Enter number of digits to the right of decimal point : ")
return "decimal", num_digits, num_decimal
elif type == "date":
return "dateTime", 0, 0
elif type == "character":
char_length = input ("Enter maximum chracter limit on entries : ")
return "varchar", char_length, 0
elif type == "boolean":
return "tinyint", 1, 0
else:
return "error", -1, -1
# Formats the Primary Key clause in the Create Table command
def handle_pk_string(self, pk: list):
pk_string = ""
for element in pk:
if element == pk[-1]:
pk_string += element
else:
pk_string += "{0} ,".format(element)
return pk_string
#Adds rows to a table
def add_records(self):
table_name = input("Enter the table you wish to insert data into : ")
if table_name not in self.table_list:
print("Table {0} does not exist".format(table_name))
return
else:
all = self.get_column(table_name)
print("Attributes are {0}".format(all))
choice = input("Inserting into all columns? (All/Partial) : ")
choice = choice.strip() # Remove whitespace
choice = choice.lower() # Case insensitive
columns = ""
column_names = ""
attributes = []
if choice == "all": # Do nothing
attributes = all
column_names += "\n" # No columns to include
elif choice == "partial": # Specify columns to insert into
names = input("Please enter names of all columns to insert into (seperated by commas) : \n")
columns += names
old_list = names.split(",") # Convert to list
attributes = trim(old_list)
for element in attributes:
if element == attributes[0]:
column_names+="({0},".format(element)
elif element == attributes[-1]:
column_names += "{0})".format(element)
else:
column_names += "{0},".format(element)
else: # Error
print("Error : Invalid input")
return
query = self.generate_insert_query(table_name, column_names, attributes)
results = self.execute_query(query)
print(results)
# From passed in parameters generates final query
def generate_insert_query(self, table_name: str, column_names: str, attributes: list):
query = "insert into {0}".format(table_name)
query += column_names
query += "values ("
for element in attributes:
value = input("Enter value to be entered into {0} : ".format(element))
if element == attributes[-1]:
query += "\'{0}\');".format(value)
else:
query += "\'{0}\',".format(value)
return query
def limit_results(self):
num = input("Maximum results to be returned : ")
......@@ -433,6 +237,29 @@ class Database:
output = trim_string(results[0])
print("Number of votes from {0} is {1}".format(state,output))
return
def update_state_count(self):
state = input("Enter the state vote count that you wish to update : ")
query = "select total_votes from state where name =\"{0}\"".format(state)
results = self.execute_query(query)
output = trim_string(results[0])
for result in results:
print("Number of votes from {0} is currently : {1}".format(state, result[0]))
value = input("Enter new value that you wish to update the state count to")
query = "update state \n"
query += "set total_votes = {0} \n".format(value)
query += "where name = \"{0}\"".format(state)
results = self.execute_query(query)
def find_user(self):
......@@ -446,16 +273,6 @@ class Database:
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}\" ));"\
......@@ -473,9 +290,11 @@ class Database:
print("No results returned")
else:
counter = 1
for result in results:
print("Username : {0}, Follower Count : {1}".format(result[0], result[1]))
print("{2}. Username : {0}, Follower Count : {1}".format(result[0], result[1], counter))
counter += 1
return
def results_county(self):
......@@ -490,8 +309,9 @@ class Database:
"where name = \"{1}\"));".format(county,state)
results = self.execute_query(query)
print(query)
if len(results) == 0:
print ("No results to show")
print("No results to show")
return
counter = 1
......@@ -583,11 +403,14 @@ class Database:
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 = input("Enter the candidate name (Joe Biden/ Donald Trump) : ")
candidate_name = candidate_name.strip()
candidate_name = candidate_name.replace(" ","")
state = input("Enter the name of the state : ")
state = state.strip()
region = input("Enter the county you wish to query by : ")
......@@ -595,9 +418,11 @@ class Database:
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 )
print(query)
results = self.execute_query(query)
if len(results) == 0:
......@@ -609,6 +434,81 @@ class Database:
print("Username : {0}, Tweet : {1}, Likes : {2} ".format(result[0], result[1], result[2]))
def count_tweet_region(self):
candidate_name = input("Enter the candidate name (Joe Biden/ Donald Trump) : ")
candidate_name = candidate_name.strip()
candidate_name = candidate_name.replace(" ", "")
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 count(tweet) from tweet inner join user 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 )
print(query)
results = self.execute_query(query)
if len(results) == 0:
print("No tweets in county {0} above {1} likes".format(region, likes))
return
print("Number of tweets in county {0} above {1} likes is {2}".format(region, likes, results[0]))
def update_poll_count(self):
county = input("Enter the county that you wish to update the poll result for : ")
county = county.strip()
state = input("Enter the state that {0} is in : ".format(county))
state = state.strip()
query = "select party, votes from county_candidate_statistics where " \
"county_id = (select id from county where name=\"{0}\" and state_id = ( select id from state " \
"where name = \"{1}\"));".format(county, state)
results = self.execute_query(query)
if len(results) == 0:
print("No results to show")
return
counter = 1
print("Poll Rankings in {0} are as follows : ".format(county))
for result in results:
print("Party Affiliation : {0} Vote Count : {1}".format(result[0], result[1]))
counter += 1
party = input("Enter the party that you wish to modify : ")
party = party.strip()
votes = input("Input the number of votes that you wish to update it to : ")
votes = votes.strip()
query = "Update county_candidate_statistics \n"
query += "Set votes = {0}\n".format(votes)
query += "Where party = \"{0}\" and county_id = (select id from county where name = \"{1}\"" \
" and state_id = (select id from state where name = \"{2}\"));".format(party, county, state)
print(query)
results = self.execute_query(query)
print(results)
def insert_tweet(self):
username = input("Enter the username of this tweet : ")
username = username.strip()
tweet = input("Enter the contents of the tweet : ")
tweet = tweet.strip()
if __name__ == "__main__":
dotenv.load_dotenv(dotenv.find_dotenv())
......@@ -647,15 +547,6 @@ if __name__ == "__main__":
break
elif command == "view_column": # Done
client.show_columns()
elif command == "search_all": # TODO
client.select_type()
elif command == "help": # Done
client.display_command_list()
......@@ -680,7 +571,20 @@ if __name__ == "__main__":
client.get_tweet_candidate()
elif command == "count_tweet_region":
pass
client.count_tweet_region()
elif command == "update_poll_count":
client.update_poll_count()
elif command == "update_state_count":
client.update_state_count()
elif command == "insert_tweet":
client.insert_tweet()
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