Skip to content

Commit

Permalink
fixed check to more efficiently match options
Browse files Browse the repository at this point in the history
  • Loading branch information
1nv8rzim committed Oct 27, 2023
1 parent a5bcfd6 commit 4b5fd99
Showing 1 changed file with 24 additions and 34 deletions.
58 changes: 24 additions & 34 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"database/sql"
"fmt"
"regexp"
"strings"
)

type optionsStruct struct {
Expand Down Expand Up @@ -143,13 +141,14 @@ func Run(ctx context.Context, target string, command string, expectedOutput stri

// Check if row exists
if optionsStruct.RowExists {
query, err := conn.PrepareContext(ctx, "SELECT * FROM ? LIMIT 1")
queryStr := fmt.Sprintf("SELECT * FROM %s LIMIT 1", optionsStruct.Table)
query, err := conn.PrepareContext(ctx, queryStr)
if err != nil {
return false, err.Error()
}
defer query.Close()

rows, err := query.QueryContext(ctx, optionsStruct.Table)
rows, err := query.QueryContext(ctx)
if err != nil {
return false, err.Error()
}
Expand All @@ -161,68 +160,59 @@ func Run(ctx context.Context, target string, command string, expectedOutput stri

// Check if field matches regex
if optionsStruct.RegexMatch {
regexp, err := regexp.Compile(expectedOutput)
if err != nil {
return false, err.Error()
}

query, err := conn.PrepareContext(ctx, "SELECT ? FROM ? LIMIT 1")
queryStr := fmt.Sprintf("SELECT %s FROM %s WHERE %s REGEXP ? LIMIT 1", optionsStruct.Field, optionsStruct.Table, optionsStruct.Field)
query, err := conn.PrepareContext(ctx, queryStr)
if err != nil {
return false, err.Error()
}
defer query.Close()

rows, err := query.QueryContext(ctx, optionsStruct.Field, optionsStruct.Table)
rows, err := query.QueryContext(ctx, expectedOutput)
if err != nil {
return false, err.Error()
}

if !rows.Next() {
return false, fmt.Sprintf("table is empty: \"%s\"", optionsStruct.Table)
}

var field string
err = rows.Scan(&field)
if err != nil {
return false, err.Error()
}

if !regexp.MatchString(field) {
return false, fmt.Sprintf("field does not match regex: \"%s\"", expectedOutput)
return false, fmt.Sprintf("no results exist that match regex: \"%s\" for field: \"%s\" in table: \"%s\" in database: \"%s\"", expectedOutput, optionsStruct.Field, optionsStruct.Table, optionsStruct.Database)
}
}

// Check if field matches substring or contains substring
if optionsStruct.SubstringMatch || optionsStruct.Match {
query, err := conn.PrepareContext(ctx, "SELECT ? FROM ? LIMIT 1")
// Check if field matches substring
if optionsStruct.SubstringMatch {
queryStr := fmt.Sprintf("SELECT %s FROM %s WHERE %s LIKE ? LIMIT 1", optionsStruct.Field, optionsStruct.Table, optionsStruct.Field)
query, err := conn.PrepareContext(ctx, queryStr)
if err != nil {
return false, err.Error()
}
defer query.Close()

rows, err := query.QueryContext(ctx, optionsStruct.Field, optionsStruct.Table)
rows, err := query.QueryContext(ctx, expectedOutput)
if err != nil {
return false, err.Error()
}

if !rows.Next() {
return false, fmt.Sprintf("table is empty: \"%s\"", optionsStruct.Table)
return false, fmt.Sprintf("no results exist that match substring: \"%s\" for field: \"%s\" in table: \"%s\" in database: \"%s\"", expectedOutput, optionsStruct.Field, optionsStruct.Table, optionsStruct.Database)
}
}

var field string
err = rows.Scan(&field)
// Check if field matches exact string
if optionsStruct.Match {
queryStr := fmt.Sprintf("SELECT %s FROM %s WHERE %s = ? LIMIT 1", optionsStruct.Field, optionsStruct.Table, optionsStruct.Field)
query, err := conn.PrepareContext(ctx, queryStr)
if err != nil {
return false, err.Error()
}
defer query.Close()

if optionsStruct.Match && field != expectedOutput {
return false, fmt.Sprintf("field does not match string: \"%s\"", expectedOutput)
rows, err := query.QueryContext(ctx, optionsStruct.Field, optionsStruct.Table)
if err != nil {
return false, err.Error()
}

if optionsStruct.SubstringMatch && !strings.Contains(field, expectedOutput) {
return false, fmt.Sprintf("field does not contain substring: \"%s\"", expectedOutput)
if !rows.Next() {
return false, fmt.Sprintf("no results exist that match string: \"%s\" for field: \"%s\" in table: \"%s\" in database: \"%s\"", expectedOutput, optionsStruct.Field, optionsStruct.Table, optionsStruct.Database)
}

}

return true, ""
Expand Down

0 comments on commit 4b5fd99

Please sign in to comment.