Repository: nuveo/prest (go)

Issue:
"invalid where clause" when using file with extension in the where 
- pREST version (or commit ref): 0a1c7bcdb3b2e2bd4dc82d1e881432b56b8bab25
- pREST endpoint: /database/public/tablename
- PostgreSQL version: 9.6.1.0
- Operating system: macOS Sierra
- Go version: 1.7
- Log gist:

## Description

When I search for filenames that are registered in a field of a table the pREST gets confused because of the dot and the extension in the filename and return "invalid where clause".

Example of how to reproduce:
```
curl -i -X GET http://localhost:3000/database/public/tablename?local_file=teste.txt
```

I believe the error is happening because of the way the new where operators are coded.
A dot can occur in any string so this seems to be a bug.


Pull request:
[WIP] fix bugs in where #95
fix #95 

Patch:
diff --git a/adapters/postgres/postgres.go b/adapters/postgres/postgres.go
index ef9a246..ca982d1 100644
--- a/adapters/postgres/postgres.go
+++ b/adapters/postgres/postgres.go
@@ -12,6 +12,8 @@ import (
 
 	"database/sql"
 
+	"regexp"
+
 	"github.com/nuveo/prest/adapters/postgres/connection"
 	"github.com/nuveo/prest/api"
 	"github.com/nuveo/prest/config"
@@ -24,6 +26,12 @@ const (
 	defaultPageSize = 10
 )
 
+var removeOperatorRegex *regexp.Regexp
+
+func init() {
+	removeOperatorRegex = regexp.MustCompile("\\$[a-z]+.")
+}
+
 // chkInvalidIdentifier return true if identifier is invalid
 func chkInvalidIdentifier(identifer string) bool {
 	if len(identifer) > 63 || unicode.IsDigit([]rune(identifer)[0]) {
@@ -44,35 +52,20 @@ func WhereByRequest(r *http.Request, initialPlaceholderID int) (whereSyntax stri
 	whereValues := []string{}
 	var value, op string
 
-	op, err = GetQueryOperator("$eq")
-	if err != nil {
-		// Never throw an error, amen
-		return
-	}
-
 	pid := initialPlaceholderID
 	for key, val := range r.URL.Query() {
 		if !strings.HasPrefix(key, "_") {
 
 			value = val[0]
 			if val[0] != "" {
-				opValues := strings.Split(val[0], ".")
-				correctLength := len(opValues) == 2
-				hasPreffix := strings.HasPrefix(opValues[0], "$")
-				if hasPreffix {
-					op = opValues[0]
-					if correctLength {
-						value = opValues[1]
-					} else {
-						value = ""
-					}
-					op, err = GetQueryOperator(op)
-					if err != nil {
-						return
-					}
+				op = removeOperatorRegex.FindString(val[0])
+				op = strings.Replace(op, ".", "", -1)
+				if op == "" {
+					op = "$eq"
 				}
-				if correctLength && !hasPreffix {
-					err = errors.New("invalid where clause")
+				value = removeOperatorRegex.ReplaceAllString(val[0], "")
+				op, err = GetQueryOperator(op)
+				if err != nil {
 					return
 				}
 			}
@@ -192,8 +185,7 @@ func JoinByRequest(r *http.Request) (values []string, err error) {
 // SelectFields query
 func SelectFields(fields []string) (sql string, err error) {
 	if len(fields) == 0 {
-
-		err = errors.New("you must select at least one field.")
+		err = errors.New("you must select at least one field")
 		return
 	}
 


Test patch:
diff --git a/adapters/postgres/postgres_test.go b/adapters/postgres/postgres_test.go
index d46e959..871560f 100644
--- a/adapters/postgres/postgres_test.go
+++ b/adapters/postgres/postgres_test.go
@@ -23,6 +23,7 @@ func TestWhereByRequest(t *testing.T) {
 		{"Where by request without paginate", "/databases?dbname=$eq.prest&test=$eq.cool", []string{"dbname = $", "test = $", " AND "}, []string{"prest", "cool"}, nil},
 		{"Where by request with spaced values", "/prest/public/test5?name=$eq.prest tester", []string{"name = $"}, []string{"prest tester"}, nil},
 		{"Where by request with jsonb field", "/prest/public/test_jsonb_bug?name=$eq.goku&data->>description:jsonb=$eq.testing", []string{"name = $", "data->>'description' = $", " AND "}, []string{"goku", "testing"}, nil},
+		{"Where by request with dot values", "/prest/public/test5?name=$eq.prest.txt tester", []string{"name = $"}, []string{"prest.txt tester"}, nil},
 	}
 
 	for _, tc := range testCases {
@@ -60,7 +61,6 @@ func TestInvalidWhereByRequest(t *testing.T) {
 		{"Where by request without jsonb key", "/prest/public/test_jsonb_bug?name=$eq.nuveo&data->>description:bla"},
 		{"Where by request with jsonb field invalid", "/prest/public/test_jsonb_bug?name=$eq.nuveo&data->>0description:jsonb=$eq.bla"},
 		{"Where by request with field invalid", "/prest/public/test?0name=$eq.prest"},
-		{"Where by request with invalid comparisons", "/prest/public/test?name=pq.prest"},
 	}
 
 	for _, tc := range testCases {