2023-11-24 01:25:20 +00:00
|
|
|
class SQL:
|
|
|
|
def __init__(self, tables: dict, query: str) -> None:
|
|
|
|
self.tables = tables
|
|
|
|
self.query = query.replace(";", "")
|
|
|
|
|
2023-11-29 23:24:17 +00:00
|
|
|
# ISCOLHE * DE tabela
|
2023-11-24 01:25:20 +00:00
|
|
|
def execute(self):
|
|
|
|
query_parts = self.query.split(" ")
|
|
|
|
|
2023-11-29 23:24:17 +00:00
|
|
|
if query_parts[0] == "ISCOLHE":
|
2023-11-24 01:25:20 +00:00
|
|
|
self.select(query_parts)
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
2023-11-30 23:15:30 +00:00
|
|
|
# ISCOLHE * DE tabela AJUNTAR tabela2 EM coluna;
|
|
|
|
# ISCOLHE * DE tabela DONDE coluna = valor;
|
2023-11-24 01:25:20 +00:00
|
|
|
def select(self, query_parts: list) -> None:
|
|
|
|
where_filter = None
|
|
|
|
join_stmt = None
|
|
|
|
|
2023-11-29 23:24:17 +00:00
|
|
|
if "AJUNTAR" in query_parts:
|
|
|
|
index = query_parts.index("AJUNTAR")
|
2023-11-24 01:25:20 +00:00
|
|
|
join_table = query_parts[index + 1]
|
2023-11-30 23:15:30 +00:00
|
|
|
join_operator = query_parts[index + 2] # EM -> estetico
|
|
|
|
join_column = query_parts[index + 3]
|
2023-11-24 01:25:20 +00:00
|
|
|
join_stmt = [self.tables[join_table], join_column]
|
|
|
|
|
2023-11-29 23:24:17 +00:00
|
|
|
if "DONDE" in query_parts:
|
|
|
|
index = query_parts.index("DONDE")
|
2023-11-24 01:38:46 +00:00
|
|
|
where_filter_column = query_parts[index + 1]
|
2023-11-25 01:25:48 +00:00
|
|
|
where_filter_operator = query_parts[index + 2]
|
|
|
|
where_filter_value = query_parts[index + 3]
|
|
|
|
where_filter = [where_filter_column, where_filter_operator, where_filter_value]
|
2023-11-24 01:38:46 +00:00
|
|
|
|
|
|
|
# parse TABLE
|
2023-11-24 01:25:20 +00:00
|
|
|
table = self.tables[query_parts[3]]
|
2023-11-24 01:38:46 +00:00
|
|
|
|
2023-11-25 15:06:07 +00:00
|
|
|
# join columns
|
|
|
|
tables_columns = table.columns
|
|
|
|
tables_rows = table.rows
|
|
|
|
|
|
|
|
if join_stmt is not None:
|
|
|
|
join_table = join_stmt[0]
|
|
|
|
join_column = join_stmt[1]
|
|
|
|
|
|
|
|
for i in range(len(tables_rows)):
|
|
|
|
for j in range(len(join_table.rows)):
|
|
|
|
row_column_index = table.columns.index(join_column)
|
|
|
|
join_row_column_index = join_table.columns.index(join_column)
|
|
|
|
|
|
|
|
if tables_rows[i][row_column_index] == join_table.rows[j][join_row_column_index]:
|
|
|
|
tables_rows[i] += join_table.rows[j]
|
|
|
|
|
|
|
|
tables_columns = table.columns + join_table.columns
|
|
|
|
|
2023-11-24 01:38:46 +00:00
|
|
|
# parse SELECT COLUMNS
|
|
|
|
column_list = query_parts[1].split(",")
|
|
|
|
for i in range(len(column_list)):
|
|
|
|
column_list[i] = column_list[i].strip()
|
|
|
|
|
|
|
|
if column_list[0] == '*':
|
2023-11-25 15:06:07 +00:00
|
|
|
column_list = tables_columns
|
2023-11-24 01:38:46 +00:00
|
|
|
|
|
|
|
# print the columns
|
|
|
|
print(', '.join(column_list))
|
|
|
|
print('-' * 20)
|
|
|
|
|
|
|
|
# print the rows
|
|
|
|
for row in tables_rows:
|
|
|
|
if where_filter is not None:
|
|
|
|
column_index = tables_columns.index(where_filter[0])
|
|
|
|
row_value = row[column_index]
|
|
|
|
|
|
|
|
if where_filter[1] == '=':
|
|
|
|
if row_value != where_filter[2]:
|
|
|
|
continue
|
|
|
|
elif where_filter[1] == '>':
|
|
|
|
if row_value <= where_filter[2]:
|
|
|
|
continue
|
|
|
|
elif where_filter[1] == '<':
|
|
|
|
if row_value >= where_filter[2]:
|
|
|
|
continue
|
|
|
|
|
|
|
|
row_values = []
|
|
|
|
for column in column_list:
|
|
|
|
column_index = tables_columns.index(column)
|
|
|
|
row_values.append(row[column_index])
|
|
|
|
|
|
|
|
print(', '.join(row_values))
|