github.com/safing/portbase@v0.19.5/database/query/condition.go (about) 1 package query 2 3 import ( 4 "fmt" 5 6 "github.com/safing/portbase/database/accessor" 7 ) 8 9 // Condition is an interface to provide a common api to all condition types. 10 type Condition interface { 11 complies(acc accessor.Accessor) bool 12 check() error 13 string() string 14 } 15 16 // Operators. 17 const ( 18 Equals uint8 = iota // int 19 GreaterThan // int 20 GreaterThanOrEqual // int 21 LessThan // int 22 LessThanOrEqual // int 23 FloatEquals // float 24 FloatGreaterThan // float 25 FloatGreaterThanOrEqual // float 26 FloatLessThan // float 27 FloatLessThanOrEqual // float 28 SameAs // string 29 Contains // string 30 StartsWith // string 31 EndsWith // string 32 In // stringSlice 33 Matches // regex 34 Is // bool: accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE 35 Exists // any 36 37 errorPresent uint8 = 255 38 ) 39 40 // Where returns a condition to add to a query. 41 func Where(key string, operator uint8, value interface{}) Condition { 42 switch operator { 43 case Equals, 44 GreaterThan, 45 GreaterThanOrEqual, 46 LessThan, 47 LessThanOrEqual: 48 return newIntCondition(key, operator, value) 49 case FloatEquals, 50 FloatGreaterThan, 51 FloatGreaterThanOrEqual, 52 FloatLessThan, 53 FloatLessThanOrEqual: 54 return newFloatCondition(key, operator, value) 55 case SameAs, 56 Contains, 57 StartsWith, 58 EndsWith: 59 return newStringCondition(key, operator, value) 60 case In: 61 return newStringSliceCondition(key, operator, value) 62 case Matches: 63 return newRegexCondition(key, operator, value) 64 case Is: 65 return newBoolCondition(key, operator, value) 66 case Exists: 67 return newExistsCondition(key, operator) 68 default: 69 return newErrorCondition(fmt.Errorf("no operator with ID %d", operator)) 70 } 71 }