This commit is contained in:
Gabriel Amaral 2023-11-29 20:24:17 -03:00
parent 1edf47346f
commit ddf8412747
3 changed files with 40 additions and 30 deletions

54
main.py
View File

@ -2,37 +2,47 @@ import os
from table import Table from table import Table
from sql import SQL from sql import SQL
import mysql.connector
def main(): def main():
print("Bem vindo ao SGBD") print("Bem-vindo!")
print("Digite a pasta onde está os arquivos CSV:") print("Deseja utilizar um banco de dados (BD) ou um arquivo .csv (CSV)?")
#csv_folder = input() escolha = input()
csv_folder = "./source"
tables = {} if escolha == "CSV":
print("Digite a pasta onde estão os arquivos CSV:")
#csv_folder = input()
csv_folder = "./source"
# get all csv files in the folder tables = {}
for file in os.listdir(csv_folder):
if file.endswith(".csv"):
table_name = file.split(".")[0]
table = Table(table_name)
table.load_from_csv(f"{csv_folder}/{file}")
tables[table_name] = table
print("Tabelas carregadas:") # Seleciona todos o csv presentes na pasta
print(tables.keys()) for file in os.listdir(csv_folder):
if file.endswith(".csv"):
table_name = file.split(".")[0]
table = Table(table_name)
table.load_from_csv(f"{csv_folder}/{file}")
tables[table_name] = table
while True: print("Tabelas carregadas:")
print("Digite a query [0 para sair]:") print(tables.keys())
query = input()
if query == "0": while True:
break print("Digite a query [0 para sair]:")
query = input()
sql = SQL(tables, query) if query == "0":
sql.execute() break
print() sql = SQL(tables, query)
sql.execute()
print()
elif escolha == "BD":
print("BD")
else:
print("Fora do intervalo!")
if __name__ == "__main__": if __name__ == "__main__":
main() main()

14
sql.py
View File

@ -8,28 +8,28 @@ class SQL:
def __str__(self) -> str: def __str__(self) -> str:
pass pass
# SELECIONAR * DE tabela # ISCOLHE * DE tabela
def execute(self): def execute(self):
query_parts = self.query.split(" ") query_parts = self.query.split(" ")
if query_parts[0] == "SELECIONAR": if query_parts[0] == "ISCOLHE":
self.select(query_parts) self.select(query_parts)
else: else:
pass pass
# SELECIONAR * DE tabela JUNTAR tabela2 coluna ONDE coluna = valor # ISCOLHE * DE tabela AJUNTAR tabela2 coluna DONDE coluna = valor
def select(self, query_parts: list) -> None: def select(self, query_parts: list) -> None:
where_filter = None where_filter = None
join_stmt = None join_stmt = None
if "JUNTAR" in query_parts: if "AJUNTAR" in query_parts:
index = query_parts.index("JUNTAR") index = query_parts.index("AJUNTAR")
join_table = query_parts[index + 1] join_table = query_parts[index + 1]
join_column = query_parts[index + 2] join_column = query_parts[index + 2]
join_stmt = [self.tables[join_table], join_column] join_stmt = [self.tables[join_table], join_column]
if "ONDE" in query_parts: if "DONDE" in query_parts:
index = query_parts.index("ONDE") index = query_parts.index("DONDE")
where_filter_column = query_parts[index + 1] where_filter_column = query_parts[index + 1]
where_filter_operator = query_parts[index + 2] where_filter_operator = query_parts[index + 2]
where_filter_value = query_parts[index + 3] where_filter_value = query_parts[index + 3]

View File

@ -14,7 +14,7 @@ class Table:
with open(csv_file, 'r') as f: with open(csv_file, 'r') as f:
reader = f.read().splitlines() reader = f.read().splitlines()
# First line is the column names # Primeira linha contêm o nome das colunas
self.columns = reader[0].split(',') self.columns = reader[0].split(',')
reader.pop(0) reader.pop(0)