Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Make Relation comparison types public
Browse files Browse the repository at this point in the history
This is leading up to a set of changes that will make the query building
a bit more transparent rather than providing just a CQL query string.
Part of the visibity is exposing how a relation is composed.
  • Loading branch information
suhailpatel committed Nov 15, 2020
1 parent 996f28e commit c7a2d93
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 44 deletions.
2 changes: 1 addition & 1 deletion interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ type Op interface {
WithOptions(Options) Op
// Options lets you read the `Options` for this `Op`
Options() Options
// Preflight performs any pre-execution validation that confirms the op considers itself "valid".
// Preflight performs any pre-execution validation that confirms the cmpType considers itself "valid".
// NOTE: Run() and RunLoggedBatch() should call this method before execution, and abort if any errors are returned.
Preflight() error
// GenerateStatement generates the statement to perform the operation
Expand Down
2 changes: 1 addition & 1 deletion mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func (f *MockFilter) keysFromRelations(keyNames []string) ([]key, error) {
return nil, fmt.Errorf("Missing mandatory PRIMARY KEY part `%s`", keyName)
}

if relation.op != equality && !(lastKey && relation.op == in) {
if relation.cmpType != CmpEquality && !(lastKey && relation.cmpType == CmpIn) {
return nil, fmt.Errorf("Invalid use of PK `%s`", keyName)
}

Expand Down
2 changes: 1 addition & 1 deletion op.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func generateWhere(rs []Relation) (string, []interface{}) {
}
s, v := r.cql()
buf.WriteString(s)
if r.op == in {
if r.cmpType == CmpIn {
vals = append(vals, v)
continue
}
Expand Down
82 changes: 42 additions & 40 deletions relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,37 @@ import (
)

const (
equality = iota
in
greaterThan
greaterThanOrEquals
lesserThan
lesserThanOrEquals
// These comparison types represent the comparison types supported
// when generating a relation between a key and it's terms
CmpEquality = iota // direct equality (foo = bar)
CmpIn // membership (foo IN (bar, bing, baz))
CmpGreaterThan // larger than (foo > 1)
CmpGreaterThanOrEquals // larger than or equal (foo >= 1)
CmpLesserThan // less than (foo < 1)
CmpLesserThanOrEquals // less than or equal (foo <= 1)
)

type Relation struct {
op int
key string
terms []interface{}
cmpType int
key string
terms []interface{}
}

func (r Relation) cql() (string, []interface{}) {
ret := ""
key := strings.ToLower(r.key)
switch r.op {
case equality:
switch r.cmpType {
case CmpEquality:
ret = key + " = ?"
case in:
case CmpIn:
return key + " IN ?", r.terms
case greaterThan:
case CmpGreaterThan:
ret = key + " > ?"
case greaterThanOrEquals:
case CmpGreaterThanOrEquals:
ret = key + " >= ?"
case lesserThan:
case CmpLesserThan:
ret = key + " < ?"
case lesserThanOrEquals:
case CmpLesserThanOrEquals:
ret = key + " <= ?"
}
return ret, r.terms
Expand Down Expand Up @@ -82,22 +84,22 @@ func (r Relation) accept(i interface{}) bool {
var result bool
var err error

if r.op == equality || r.op == in {
if r.cmpType == CmpEquality || r.cmpType == CmpIn {
return anyEquals(i, r.terms)
}

a, b := convertToPrimitive(i), convertToPrimitive(r.terms[0])

switch r.op {
case greaterThan:
switch r.cmpType {
case CmpGreaterThan:
result, err = builtinGreaterThan(a, b)
case greaterThanOrEquals:
case CmpGreaterThanOrEquals:
result, err = builtinGreaterThan(a, b)
result = result || a == b
case lesserThanOrEquals:
case CmpLesserThanOrEquals:
result, err = builtinLessThan(a, b)
result = result || a == b
case lesserThan:
case CmpLesserThan:
result, err = builtinLessThan(a, b)
}

Expand All @@ -110,48 +112,48 @@ func toI(i interface{}) []interface{} {

func Eq(key string, term interface{}) Relation {
return Relation{
op: equality,
key: key,
terms: toI(term),
cmpType: CmpEquality,
key: key,
terms: toI(term),
}
}

func In(key string, terms ...interface{}) Relation {
return Relation{
op: in,
key: key,
terms: terms,
cmpType: CmpIn,
key: key,
terms: terms,
}
}

func GT(key string, term interface{}) Relation {
return Relation{
op: greaterThan,
key: key,
terms: toI(term),
cmpType: CmpGreaterThan,
key: key,
terms: toI(term),
}
}

func GTE(key string, term interface{}) Relation {
return Relation{
op: greaterThanOrEquals,
key: key,
terms: toI(term),
cmpType: CmpGreaterThanOrEquals,
key: key,
terms: toI(term),
}
}

func LT(key string, term interface{}) Relation {
return Relation{
op: lesserThan,
key: key,
terms: toI(term),
cmpType: CmpLesserThan,
key: key,
terms: toI(term),
}
}

func LTE(key string, term interface{}) Relation {
return Relation{
op: lesserThanOrEquals,
key: key,
terms: toI(term),
cmpType: CmpLesserThanOrEquals,
key: key,
terms: toI(term),
}
}
2 changes: 1 addition & 1 deletion scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func wrapPtrValue(ptr reflect.Value, target reflect.Type) reflect.Value {
}

// ignoreFieldType struct is for fields we want to ignore, we specify a custom
// unmarshal type which literally is a no-op and does nothing with this data.
// unmarshal type which literally is a no-cmpType and does nothing with this data.
// In the future, maybe we can be smarter of only extracting fields which we
// are able to unmarshal into our target struct and get rid of this
type ignoreFieldType struct{}
Expand Down

0 comments on commit c7a2d93

Please sign in to comment.