Skip to content

Commit

Permalink
Merge pull request #10 from sschonss/select-where
Browse files Browse the repository at this point in the history
Select where with =
  • Loading branch information
sschonss committed Mar 5, 2024
2 parents fad39e8 + 01a3e19 commit e535f72
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 2 deletions.
Empty file modified .github/ISSUE_TEMPLATE/bug_report.md
100644 → 100755
Empty file.
Empty file modified .github/ISSUE_TEMPLATE/feature_request.md
100644 → 100755
Empty file.
Empty file modified .github/workflows/go.yml
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified README_BR.md
100644 → 100755
Empty file.
137 changes: 137 additions & 0 deletions commands/commands.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {

Expand Down
2 changes: 2 additions & 0 deletions data/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
user:root
password:root
3 changes: 3 additions & 0 deletions data/users.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id;nome
1;luiz
20;joao
16 changes: 16 additions & 0 deletions database/database.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Empty file modified go.mod
100644 → 100755
Empty file.
Empty file modified logo.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 10 additions & 2 deletions main.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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: ")
Expand All @@ -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.")
Expand Down

0 comments on commit e535f72

Please sign in to comment.