From bd15630f87d9aaea88c40dde02a4a1f233b35dfb Mon Sep 17 00:00:00 2001 From: sschonss Date: Mon, 4 Mar 2024 20:03:12 -0300 Subject: [PATCH] =?UTF-8?q?Adiciona=20fun=C3=A7=C3=A3o=20SelectFrom=20ao?= =?UTF-8?q?=20comando=20SELECT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/commands.go | 42 +++++++++++++++++++++++++++++++++++++----- database/database.go | 1 + 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/commands/commands.go b/commands/commands.go index 305376e..ff691e1 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -57,9 +57,6 @@ func DropTable(command string) { func InsertInto(command string) { - //create table table (column1, column2, column3) - //ex: insert into table (column1, column2, column3) values (value1, value2, value3) - table := strings.Split(command, " ")[2] if _, err := @@ -68,7 +65,6 @@ func InsertInto(command string) { return } - //escrever os values values := strings.Split(command, "values")[1] values = strings.Split(values, "(")[1] values = strings.Split(values, ")")[0] @@ -90,4 +86,40 @@ func InsertInto(command string) { } fmt.Println("Registro inserido com sucesso") -} \ No newline at end of file +} + +func SelectFrom(command string) { + + table := strings.Split(command, " ")[3] + + if _, err := os.Stat("data/" + table + ".csv"); os.IsNotExist(err) { + fmt.Println("Tabela não existe") + return + } + + file, err := os.Open("data/" + table + ".csv") + if err != nil { + fmt.Println("Erro ao abrir arquivo") + return + } + + defer file.Close() + + fmt.Println("Tabela: " + table) + fmt.Println("") + var columns string + fmt.Fscanf(file, "%s\n", &columns) + columns = strings.Replace(columns, ";", " | ", -1) + fmt.Println(columns) + + var line string + for { + _, err := fmt.Fscanf(file, "%s\n", &line) + if err != nil { + break + } + line = strings.Replace(line, ";", " | ", -1) + fmt.Println(line) + } +} + diff --git a/database/database.go b/database/database.go index f052ee1..baaed29 100644 --- a/database/database.go +++ b/database/database.go @@ -20,6 +20,7 @@ func (Database) ExecuteQuery(query string) string { fmt.Print("\033[H\033[2J") case "select": fmt.Println("Executando comando SELECT") + commands.SelectFrom(query) case "insert": fmt.Println("Executando comando INSERT") commands.InsertInto(query)