Skip to content

Commit

Permalink
Adiciona função InsertInto ao executar comando INSERT
Browse files Browse the repository at this point in the history
  • Loading branch information
sschonss committed Mar 4, 2024
1 parent 55fe8a7 commit efc68e0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
1 change: 1 addition & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit efc68e0

Please sign in to comment.