Skip to content

Commit

Permalink
Merge pull request #3 from sschonss/drop-table
Browse files Browse the repository at this point in the history
Drop table
  • Loading branch information
sschonss authored Mar 4, 2024
2 parents c51b941 + efc68e0 commit 2b3bcd9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
55 changes: 55 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,58 @@ func CreateTable(command string) {

fmt.Println("Tabela criada com sucesso")
}

func DropTable(command string) {

table := strings.Split(command, " ")[2]

if _, err := os.Stat("data/" + table + ".csv"); os.IsNotExist(err) {
fmt.Println("Tabela não existe")
return
}

err := os.Remove("data/" + table + ".csv")
if err != nil {
fmt.Println("Erro ao deletar tabela")
return
}

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")
}
2 changes: 2 additions & 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 All @@ -31,6 +32,7 @@ func (Database) ExecuteQuery(query string) string {
commands.CreateTable(query)
case "drop":
fmt.Println("Executando comando DROP")
commands.DropTable(query)
case "alter":
fmt.Println("Executando comando ALTER")
default:
Expand Down

0 comments on commit 2b3bcd9

Please sign in to comment.