-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconditions.go
126 lines (105 loc) · 3.12 KB
/
conditions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package ecql
import (
"fmt"
)
type OrderType string
const (
AscOrder OrderType = "ASC"
DescOrder = "DESC"
)
type OrderBy struct {
Column string
OrderType
}
func Asc(col string) OrderBy {
return OrderBy{col, AscOrder}
}
func Desc(col string) OrderBy {
return OrderBy{col, DescOrder}
}
type PredicateType int
type Condition struct {
CQLFragment string
Values []interface{}
}
func And(lhs Condition, list ...Condition) Condition {
cqlfragment := lhs.CQLFragment
values := lhs.Values
for _, rhs := range list {
cqlfragment += " AND " + rhs.CQLFragment
values = append(values, rhs.Values...)
}
return Condition{CQLFragment: cqlfragment, Values: values}
}
func Eq(col string, v interface{}) Condition {
return Condition{CQLFragment: fmt.Sprintf("%s = ?", col),
Values: []interface{}{v}}
}
func Gt(col string, v interface{}) Condition {
return Condition{CQLFragment: fmt.Sprintf("%s > ?", col),
Values: []interface{}{v}}
}
func Ge(col string, v interface{}) Condition {
return Condition{CQLFragment: fmt.Sprintf("%s >= ?", col),
Values: []interface{}{v}}
}
func Lt(col string, v interface{}) Condition {
return Condition{CQLFragment: fmt.Sprintf("%s < ?", col),
Values: []interface{}{v}}
}
func Le(col string, v interface{}) Condition {
return Condition{CQLFragment: fmt.Sprintf("%s <= ?", col),
Values: []interface{}{v}}
}
func In(col string, v ...interface{}) Condition {
return Condition{CQLFragment: fmt.Sprintf("%s IN (%s)", col, qms(len(v))),
Values: v}
}
// EqInt takes is interested in the CQL indexes of the provided struct as a condition
// For convenience, that struct is assumed to follow the same rules as other mappings
func EqInt(i interface{}) Condition {
values, table := MapTable(i)
first := true
condition := True()
for _, column := range table.KeyColumns {
keyCondition := Eq(column, values[column])
if first {
condition = keyCondition
first = false
} else {
condition = And(condition, keyCondition)
}
}
return condition
}
func True() Condition {
return Condition{CQLFragment: "true"}
}
// Contains creates the condition 'col CONTAINS value' used to filter elements
// in a collection set, list, or map. Supported on CQL versions >= 3.2.0.
func Contains(col string, v interface{}) Condition {
return Condition{
CQLFragment: fmt.Sprintf("%s CONTAINS ?", col),
Values: []interface{}{v},
}
}
// Contains creates the condition 'col CONTAINS KEY value' used to filter elements
// by key in a map. Supported on CQL versions >= 3.2.0.
func ContainsKey(col string, v interface{}) Condition {
return Condition{
CQLFragment: fmt.Sprintf("%s CONTAINS KEY ?", col),
Values: []interface{}{v},
}
}
// Raw allows to set the CQLFrament and Values of a condition. It allows to add
// any not yet supported condition in a easy way.
// Raw("token(partition_key) > token(?)", v.ID)
// Raw("token(partition_key) > token('value')")
// Raw("time > maxTimeuuid('2013-01-01 00:05+0000')")
// Raw("time > maxTimeuuid(?) AND time < minTimeuuid(?)", maxTime, minTime)
func Raw(fragment string, v ...interface{}) Condition {
return Condition{
CQLFragment: fragment,
Values: v,
}
}