Compare commits

..

No commits in common. "a08fd5f98b726dee011d3520b80da72e2ebd5744" and "1edf47346fc209776631552a67a144eae9e0be07" have entirely different histories.

3 changed files with 36 additions and 61 deletions

72
main.py
View File

@ -2,65 +2,37 @@ import os
from table import Table
from sql import SQL
import mysql.connector
from mysql.connector import Error
def main():
print("Bem-vindo!")
print("Deseja utilizar um banco de dados (BD) ou um arquivo .csv (CSV)?")
escolha = input()
print("Bem vindo ao SGBD")
print("Digite a pasta onde está os arquivos CSV:")
#csv_folder = input()
csv_folder = "./source"
if escolha == "CSV":
print("Digite a pasta onde estão os arquivos CSV:")
#csv_folder = input()
csv_folder = "./source"
tables = {}
tables = {}
# get all csv files in the folder
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
# Seleciona todos o csv presentes na pasta
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:")
print(tables.keys())
print("Tabelas carregadas:")
print(tables.keys())
while True:
print("Digite a query [0 para sair]:")
query = input()
while True:
print("Digite a query [0 para sair]:")
query = input()
if query == "0":
break
if query == "0":
break
sql = SQL(tables, query)
sql.execute()
sql = SQL(tables, query)
sql.execute()
print()
elif escolha == "BD":
try:
connection = mysql.connector.connect(host='localhost',
database='employees',
user='employeesdb',
password='Employees1!')
if connection.is_connected():
print("Conectado ao employees-db")
cursor = connection.cursor()
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
else:
print("Fora do intervalo!")
print()
if __name__ == "__main__":
main()

23
sql.py
View File

@ -1,32 +1,35 @@
from table import Table
class SQL:
def __init__(self, tables: dict, query: str) -> None:
self.tables = tables
self.query = query.replace(";", "")
# ISCOLHE * DE tabela
def __str__(self) -> str:
pass
# SELECIONAR * DE tabela
def execute(self):
query_parts = self.query.split(" ")
if query_parts[0] == "ISCOLHE":
if query_parts[0] == "SELECIONAR":
self.select(query_parts)
else:
pass
# ISCOLHE * DE tabela AJUNTAR tabela2 EM coluna;
# ISCOLHE * DE tabela DONDE coluna = valor;
# SELECIONAR * DE tabela JUNTAR tabela2 coluna ONDE coluna = valor
def select(self, query_parts: list) -> None:
where_filter = None
join_stmt = None
if "AJUNTAR" in query_parts:
index = query_parts.index("AJUNTAR")
if "JUNTAR" in query_parts:
index = query_parts.index("JUNTAR")
join_table = query_parts[index + 1]
join_operator = query_parts[index + 2] # EM -> estetico
join_column = query_parts[index + 3]
join_column = query_parts[index + 2]
join_stmt = [self.tables[join_table], join_column]
if "DONDE" in query_parts:
index = query_parts.index("DONDE")
if "ONDE" in query_parts:
index = query_parts.index("ONDE")
where_filter_column = query_parts[index + 1]
where_filter_operator = query_parts[index + 2]
where_filter_value = query_parts[index + 3]

View File

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