diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md old mode 100644 new mode 100755 diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md old mode 100644 new mode 100755 diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml old mode 100644 new mode 100755 diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/README_BR.md b/README_BR.md old mode 100644 new mode 100755 diff --git a/commands/commands.go b/commands/commands.go old mode 100644 new mode 100755 index 93b0766..8674542 --- a/commands/commands.go +++ b/commands/commands.go @@ -89,6 +89,11 @@ func InsertInto(command string) { } func SelectFrom(command string) { + if strings.Contains(command, "where") { + SelectWhere(command) + return + } + if strings.Contains(command, "*") { SelectAllFrom(command) return @@ -141,11 +146,143 @@ func SelectFrom(command string) { } } } +} + +func SelectWhere(command string) { + if strings.Contains(command, "*") { + SelectAllFromWhere(command) + return + } + + arrayColumns := strings.Split(command, " ")[1] + arrayColumns = strings.Split(arrayColumns, "from")[0] + arrayColumns = strings.Replace(arrayColumns, " ", "", -1) + arrayColumns = strings.Split(arrayColumns, ",")[0] + + fmt.Println("Colunas: " + arrayColumns) + + 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) + + var columnsTable string + fmt.Fscanf(file, "%s\n", &columnsTable) + columnsTableArray := strings.Split(columnsTable, ";") + + for i := 0; i < len(columnsTableArray); i++ { + if columnsTableArray[i] == arrayColumns { + fmt.Println(columnsTableArray[i]) + } + } + + operadores := []string{"=", ">", "<", ">=", "<=", "<>"} + conditions := strings.Split(command, "where")[1] + conditions = strings.Replace(conditions, " ", "", -1) + conditions = strings.Replace(conditions, "(", "", -1) + conditions = strings.Replace(conditions, ")", "", -1) + conditions = strings.Replace(conditions, ";", "", -1) + conditions_array := strings.Split(conditions, "and") + + var operador string + + for i := 0; i < len(operadores); i++ { + if strings.Contains(conditions_array[0], operadores[i]) { + operador = operadores[i] + } + } + valor := strings.Split(conditions_array[0], operador)[1] + valor = strings.Replace(valor, " ", "", -1) + var line string + for { + _, err := fmt.Fscanf(file, "%s\n", &line) + if err != nil { + break + } + + if operador == "=" { + if strings.Contains(line, valor) { + lineArray := strings.Split(line, ";") + for i := 0; i < len(lineArray); i++ { + if columnsTableArray[i] == arrayColumns { + fmt.Println(lineArray[i]) + } + } + } + } + } } +func SelectAllFromWhere(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) + + operadores := []string{"=", ">", "<", ">=", "<=", "<>"} + conditions := strings.Split(command, "where")[1] + conditions = strings.Replace(conditions, " ", "", -1) + conditions = strings.Replace(conditions, "(", "", -1) + conditions = strings.Replace(conditions, ")", "", -1) + conditions = strings.Replace(conditions, ";", "", -1) + conditions_array := strings.Split(conditions, "and") + + var operador string + for i := 0; i < len(operadores); i++ { + if strings.Contains(conditions_array[0], operadores[i]) { + operador = operadores[i] + } + } + + valor := strings.Split(conditions_array[0], operador)[1] + valor = strings.Replace(valor, " ", "", -1) + var line string + for { + _, err := fmt.Fscanf(file, "%s\n", &line) + if err != nil { + break + } + + if operador == "=" { + if strings.Contains(line, valor) { + line = strings.Replace(line, ";", " | ", -1) + fmt.Println(line) + } + } + } +} func SelectAllFrom(command string) { diff --git a/data/config.txt b/data/config.txt new file mode 100644 index 0000000..cfa3066 --- /dev/null +++ b/data/config.txt @@ -0,0 +1,2 @@ +user:root +password:root diff --git a/data/users.csv b/data/users.csv new file mode 100644 index 0000000..4616538 --- /dev/null +++ b/data/users.csv @@ -0,0 +1,3 @@ +id;nome +1;luiz +20;joao diff --git a/database/database.go b/database/database.go old mode 100644 new mode 100755 index 5b0094a..e156ce3 --- a/database/database.go +++ b/database/database.go @@ -89,6 +89,22 @@ func (Database) LoadConfig() (string, string) { return user, password } +func CheckExisteDirData() bool { + _, err := os.Stat("data") + if os.IsNotExist(err) { + return false + } else { + return true + } +} + +func CreateDirData() { + err := os.Mkdir("data", 0755) + if err != nil { + fmt.Println("Erro ao criar diretório data") + } +} + func (Database) CheckConfig() bool { _, err := os.Stat("data/config.txt") if os.IsNotExist(err) { diff --git a/go.mod b/go.mod old mode 100644 new mode 100755 diff --git a/logo.png b/logo.png old mode 100644 new mode 100755 diff --git a/main.go b/main.go old mode 100644 new mode 100755 index 099f58f..8dbeadb --- a/main.go +++ b/main.go @@ -10,8 +10,16 @@ func main() { var db database.Database + dirData := database.CheckExisteDirData() + if !dirData { + fmt.Println("Diretório data não encontrado.") + fmt.Println("Criando diretório data...") + database.CreateDirData() + fmt.Println("Diretório data criado com sucesso.") + } + config := db.CheckConfig() - if config == true { + if config { fmt.Println("Configurações encontradas.") fmt.Println("Digite o usuário e a senha para carregar as configurações.") fmt.Print("Usuário: ") @@ -20,7 +28,7 @@ func main() { password := database.GetUserInput() valid := db.CheckUser(user, password) count_try := 0 - for valid == false { + for !valid { count_try++ if count_try > 2 { fmt.Println("Número de tentativas excedido.")