Attempt to make the wherer interface type a little bit more flexible to accomodate many different field types.

Tests for wherer, WhereAnd and WhereOr
This commit is contained in:
Mosen
2016-07-21 22:55:58 +10:00
parent 66d5036346
commit 4c10a0302f
3 changed files with 112 additions and 23 deletions

View File

@@ -103,29 +103,6 @@ func (store pgStore) GetCertificatesByDeviceUDID(udid string) ([]Certificate, er
return certificates, nil
}
// UUID is a filter that can be added as a parameter to narrow down the list of returned results
type UUID struct {
UUID string
}
func (p UUID) where() string {
return fmt.Sprintf("certificate_uuid = '%s'", p.UUID)
}
// Filter by a device uuid
type DeviceUUID struct {
UUID string
}
func (p DeviceUUID) where() string {
return fmt.Sprintf("device_uuid = '%s'", p.UUID)
}
// whereer is for building args passed into a method which finds resources
type whereer interface {
where() string
}
// add WHERE clause from params
func addWhereFilters(stmt string, separator string, params ...interface{}) string {
var where []string

64
certificates/statement.go Normal file
View File

@@ -0,0 +1,64 @@
package certificates
import (
"fmt"
"strconv"
"strings"
)
// Where struct wraps a where clause.
// Basic usage is Where{"field","value"} for WHERE field = value
// The Operator field can be used if you need < > = != LIKE / NOT LIKE operators
type Where struct {
Field string
Value interface{}
Operator string
}
// Stringer produces the WHERE condition
func (w Where) String() string {
var operator string = w.Operator
if w.Operator == "" {
operator = "="
}
var quotedValue string
switch w.Value.(type) {
case string:
quotedValue = fmt.Sprintf("'%s'", w.Value)
case nil:
operator = "IS"
quotedValue = "NULL"
case bool:
if w.Value.(bool) == true {
quotedValue = "true"
} else {
quotedValue = "false"
}
case []string: // IN("strings...")
operator = "IN"
inValues := w.Value.([]string)
quotedValue = "('" + strings.Join(inValues, "','") + "')"
case int:
quotedValue = strconv.Itoa(w.Value.(int))
}
return fmt.Sprintf("%s %s %s", w.Field, operator, quotedValue)
}
// whereer is for building args passed into a method which finds resources
type whereer interface {
where() string
}
type WhereAnd []Where
func (wa WhereAnd) String() {
return strings.Join(wa, " AND ")
}
type WhereOr []Where
func (wo WhereOr) String() {
return strings.Join(wo, " OR ")
}

View File

@@ -0,0 +1,48 @@
package certificates
import (
"testing"
)
type testpair struct {
when Where
then string
}
var tests = []testpair{
{Where{"field", "value", "="}, "field = 'value'"},
{Where{"field", 1, "="}, "field = 1"},
{Where{"field", false, "="}, "field = false"},
{Where{"field", []string{"foo", "bar"}, "IN"}, "field IN ('foo','bar')"},
{Where{"field", "%foo%", "LIKE"}, "field LIKE '%foo%'"},
{Where{"field", "bar", "!="}, "field != 'bar'"},
{Where{"field", nil, "IS"}, "field IS NULL"},
}
func TestWhere(t *testing.T) {
for _, test := range tests {
v := test.when.String()
if v != test.then {
t.Error(
"Expected", test.then,
"got", v,
)
}
}
}
var waTests = []testpair{
{WhereAnd{Where{"field", "value", "="}, Where{"field", "bar", "="}}, "field = 'value' AND field = 'bar'"},
}
func TestWhereAnd_String(t *testing.T) {
for _, test := range waTests {
v := test.when.String()
if v != test.then {
t.Error(
"Expected", test.then,
"got", v,
)
}
}
}