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)