From efc68e078b1898c4204a73e9c578b13d1f3fe9ce Mon Sep 17 00:00:00 2001 From: sschonss Date: Mon, 4 Mar 2024 19:49:09 -0300 Subject: [PATCH] =?UTF-8?q?Adiciona=20fun=C3=A7=C3=A3o=20InsertInto=20ao?= =?UTF-8?q?=20executar=20comando=20INSERT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/commands.go | 37 +++++++++++++++++++++++++++++++++++++ database/database.go | 1 + 2 files changed, 38 insertions(+) diff --git a/commands/commands.go b/commands/commands.go index 787ce0d..305376e 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -53,4 +53,41 @@ func DropTable(command string) { } fmt.Println("Tabela deletada com sucesso") +} + +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 := + os.Stat("data/" + table + ".csv"); os.IsNotExist(err) { + fmt.Println("Tabela não existe") + return + } + + //escrever os values + values := strings.Split(command, "values")[1] + values = strings.Split(values, "(")[1] + values = strings.Split(values, ")")[0] + values = strings.Replace(values, ",", ";", -1) + values = strings.Replace(values, " ", "", -1) + values = values + "\n" + + file, err := os.OpenFile("data/" + table + ".csv", os.O_APPEND|os.O_WRONLY, 0644) + if err != nil { + fmt.Println("Erro ao abrir arquivo") + return + } + defer file.Close() + + _, err = file.WriteString(values) + if err != nil { + fmt.Println("Erro ao escrever no arquivo") + return + } + + fmt.Println("Registro inserido com sucesso") } \ No newline at end of file diff --git a/database/database.go b/database/database.go index 73e6b21..f052ee1 100644 --- a/database/database.go +++ b/database/database.go @@ -22,6 +22,7 @@ func (Database) ExecuteQuery(query string) string { fmt.Println("Executando comando SELECT") case "insert": fmt.Println("Executando comando INSERT") + commands.InsertInto(query) case "update": fmt.Println("Executando comando UPDATE") case "delete":