-
Notifications
You must be signed in to change notification settings - Fork 69
/
filter.go
61 lines (53 loc) · 1.35 KB
/
filter.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
package cmagic
import (
"strings"
"fmt"
)
type filter struct {
t T
rs []Relation
}
func (f *filter) generateWhere() (string, []interface{}) {
strs := []string{}
vals := []interface{}{}
for _, r := range f.rs {
s, v := r.cql()
strs = append(strs, s)
vals = append(vals, v...)
}
return "WHERE " + strings.Join(strs, " AND "), vals
}
func (f filter) Replace(i interface{}) error {
return nil
}
// UPDATE keyspace.Movies SET col1 = val1, col2 = val2
func updateStatement(kn, cfName string, fieldNames []string) string {
cols := []string{}
for _, v := range fieldNames {
cols = append(cols, v+" = ?")
}
return fmt.Sprintf("UPDATE %v.%v SET "+strings.Join(cols, ", "), kn, cfName)
}
func (f filter) Update(m map[string]interface{}) error {
fields, values := keyValues(m)
str, wvals := f.generateWhere()
stmt := updateStatement(f.t.keySpace.name, f.t.info.name, fields)
sess := f.t.keySpace.session
if f.t.keySpace.debugMode {
fmt.Println(stmt, wvals)
}
return sess.Query(stmt +" "+ str, append(values, wvals...)...).Exec()
}
func (f filter) Delete() error {
str, vals := f.generateWhere()
stmt := fmt.Sprintf("DELETE FROM %v.%v ", f.t.keySpace.name, f.t.info.name) + str
if f.t.keySpace.debugMode {
fmt.Println(stmt, vals)
}
return f.t.keySpace.session.Query(stmt, vals...).Exec()
}
func (f filter) Query() Query {
return &query{
f: f,
}
}