Skip to content

Commit

Permalink
Merge pull request #4 from sschonss/select-all
Browse files Browse the repository at this point in the history
Adiciona função SelectFrom ao comando SELECT
  • Loading branch information
sschonss committed Mar 4, 2024
2 parents 2b3bcd9 + bd15630 commit 88a6132
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
42 changes: 37 additions & 5 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 :=
Expand All @@ -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]
Expand All @@ -90,4 +86,40 @@ func InsertInto(command string) {
}

fmt.Println("Registro inserido com sucesso")
}
}

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)
}
}

1 change: 1 addition & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 88a6132

Please sign in to comment.