/
squeal_main.py
149 lines (130 loc) · 6.91 KB
/
squeal_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
By ZhiChao Luo
#1000431856
As Assighment 3: Squeal
CSC 108
"""
"""
Process SQuEaL queries from the keyboard and print the results.
"""
import db_io
import squeal
def main():
""" () -> NoneType
Ask for queries from the keyboard; stop when empty line is received. For
each query, process it and use db_io.print_csv to print the results.
"""
# Write your main function body here.
# An infinite while loop continues until a blank line is entered
while True:
#ask user for input
choose_query = input('')
#if an empty line is entered, break the loop
if choose_query == '':
break
index_of_from = choose_query.index('from')
#Saperate the titles given by the user from the user input
list_of_titles = choose_query[7:index_of_from-1].split(',')
list_of_where_names = []
#If the optional query where is used, saperates the title which is used in the where query
if 'where' in choose_query :
index_of_where = choose_query.index('where')
#Saperate the table names given by the user from the user input
list_of_tables_names = choose_query[index_of_from+5:index_of_where-1\
].split(',')
#Saperate the title names from where query
#given by the user from the user input
if '=' in choose_query:
list_of_where_names = choose_query[index_of_where+6:].split('=')
elif '>' in choose_query:
list_of_where_names = choose_query[index_of_where+6:].split('>')
else:
#Saperate the table names given by the user from the user input
list_of_tables_names = choose_query[index_of_from+5:].split(',')
#Creating a list for storing the tables which the user is required to use
list_of_tables = []
#Put the correct tables with the names user given into the list_of_tables list.
for items in list_of_tables_names:
list_of_tables.append(db_io.read_table(items+'.csv'))
#if the title names given is a '*', then use all the titles in the tables
if list_of_titles[0] == '*' :
list_of_titles = []
for items in list_of_tables :
for key in items.keys():
list_of_titles.append(key)
#product_table represents the table or cartesian product
#of two tables from the user's input
product_table = {}
if len (list_of_tables) == 1:
product_table = list_of_tables[0]
else:
product_table = squeal.cartesian_product(list_of_tables[0],\
list_of_tables[1])
#Check if the second value in the where query is a value or a column name
value_or_column_name = 'value'
for key in product_table.keys():
if 'where' in choose_query and key == list_of_where_names[1]:
value_or_column_name = 'column'
#procesed_table represents the processed table after operation
#from the user's where query, which is optional
processed_table = {}
#If where query is entered, added the missing title, (which is the
#title after the = operation in the user input) into the product
if 'where' in choose_query and value_or_column_name == 'column':
processed_table[list_of_where_names[1]] = \
product_table[list_of_where_names[1]]
#Eliminate all the titles which the user did not request in the query
for key in product_table.keys() :
if key in list_of_titles:
processed_table[key] = product_table[key]
#Get keys from the processed_table for further use
processed_table_keys = []
#A variable use to save the length of a row
row_length = 0
#If where query is used by the user, then operate the where query
if 'where' in choose_query :
#Loop each row in the table and check for duplicate in the given titles
for row_count in range(len(processed_table[list_of_where_names[0]])) :
row_length = len(processed_table[list_of_where_names[0]])
#If the information in the given titles from the where query
#are the same, then mark the row as "DELETEDROW"
if '=' in choose_query and value_or_column_name == 'column':
if processed_table[list_of_where_names[0]][row_count] != \
processed_table[list_of_where_names[1]][row_count] :
for key in list(processed_table.keys()) :
processed_table[key][row_count] = "DELETEDROW"
#are greater than the other, then mark the row as "DELETEDROW"
elif '>' in choose_query and value_or_column_name == 'column':
if processed_table[list_of_where_names[0]][row_count] <= \
processed_table[list_of_where_names[1]][row_count] :
for key in list(processed_table.keys()) :
processed_table[key][row_count] = "DELETEDROW"
if '=' in choose_query and value_or_column_name == 'value':
if processed_table[list_of_where_names[0]][row_count] != \
list_of_where_names[1] :
for key in list(processed_table.keys()) :
processed_table[key][row_count] = "DELETEDROW"
elif '>' in choose_query and value_or_column_name == 'value':
if processed_table[list_of_where_names[0]][row_count] <= \
list_of_where_names[1]:
for key in list(processed_table.keys()) :
processed_table[key][row_count] = "DELETEDROW"
#Delete the title after = operator given in the where query
if value_or_column_name == 'column' and list_of_where_names[0] \
!= list_of_where_names[1] :
del processed_table[list_of_where_names[1]]
position_changes = 0
#Check all the keys in the processed_table
for key in list(processed_table.keys()) :
position_changes = 0
for row_count in range(row_length) :
#Delete all the rows with the letter 'DELETEDROW'
if processed_table[key][row_count-position_changes] \
== "DELETEDROW" :
del processed_table[key][row_count-position_changes]
position_changes = position_changes + 1
#Print the table
db_io.print_csv(processed_table)
#db_io.print_csv(squeal.cartesian_product(data['movies'],data['oscars']))
if __name__ == '__main__':
main()