This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
forked from gocassa/gocassa
-
Notifications
You must be signed in to change notification settings - Fork 13
/
op.go
162 lines (141 loc) · 3.55 KB
/
op.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package gocassa
import (
"sort"
"context"
)
const (
readOpType uint8 = iota
singleReadOpType
deleteOpType
updateOpType
insertOpType
)
type singleOp struct {
options Options
f filter
opType uint8
result interface{}
m map[string]interface{} // map for updates, sets etc
qe QueryExecutor
}
func (o *singleOp) Options() Options {
return o.options
}
func (o *singleOp) WithOptions(opts Options) Op {
return &singleOp{
options: o.options.Merge(opts),
f: o.f,
opType: o.opType,
result: o.result,
m: o.m,
qe: o.qe}
}
func (o *singleOp) Add(additions ...Op) Op {
return multiOp{o}.Add(additions...)
}
func (o *singleOp) Preflight() error {
return nil
}
func newWriteOp(qe QueryExecutor, f filter, opType uint8, m map[string]interface{}) *singleOp {
return &singleOp{
qe: qe,
f: f,
opType: opType,
m: m}
}
func (o *singleOp) Run() error {
switch o.opType {
case readOpType, singleReadOpType:
stmt := o.generateSelect(o.options)
scanner := NewScanner(stmt, o.result)
return o.qe.QueryWithOptions(o.options, stmt, scanner)
case insertOpType:
stmt := o.generateInsert(o.options)
return o.qe.ExecuteWithOptions(o.options, stmt)
case updateOpType:
stmt := o.generateUpdate(o.options)
return o.qe.ExecuteWithOptions(o.options, stmt)
case deleteOpType:
stmt := o.generateDelete(o.options)
return o.qe.ExecuteWithOptions(o.options, stmt)
}
return nil
}
func (o *singleOp) RunWithContext(ctx context.Context) error {
return o.WithOptions(Options{Context: ctx}).Run()
}
func (o *singleOp) RunAtomically() error {
return o.Run()
}
func (o *singleOp) RunLoggedBatchWithContext(ctx context.Context) error {
return o.WithOptions(Options{Context: ctx}).Run()
}
func (o *singleOp) RunAtomicallyWithContext(ctx context.Context) error {
return o.RunLoggedBatchWithContext(ctx)
}
func (o *singleOp) GenerateStatement() Statement {
switch o.opType {
case readOpType, singleReadOpType:
return o.generateSelect(o.options)
case insertOpType:
return o.generateInsert(o.options)
case updateOpType:
return o.generateUpdate(o.options)
case deleteOpType:
return o.generateDelete(o.options)
}
return noOpStatement{}
}
func (o *singleOp) QueryExecutor() QueryExecutor {
return o.qe
}
func (o *singleOp) generateSelect(opt Options) SelectStatement {
mopt := o.f.t.options.Merge(opt)
return SelectStatement{
keyspace: o.f.t.keySpace.name,
table: o.f.t.Name(),
fields: o.f.t.generateFieldList(mopt.Select),
where: o.f.rs,
order: mopt.ClusteringOrder,
limit: mopt.Limit,
allowFiltering: mopt.AllowFiltering,
keys: o.f.t.info.keys,
}
}
func (o *singleOp) generateInsert(opt Options) InsertStatement {
mopt := o.f.t.options.Merge(opt)
return InsertStatement{
keyspace: o.f.t.keySpace.name,
table: o.f.t.Name(),
fieldMap: o.m,
ttl: mopt.TTL,
keys: o.f.t.info.keys,
}
}
func (o *singleOp) generateUpdate(opt Options) UpdateStatement {
mopt := o.f.t.options.Merge(opt)
return UpdateStatement{
keyspace: o.f.t.keySpace.name,
table: o.f.t.Name(),
fieldMap: o.m,
where: o.f.rs,
ttl: mopt.TTL,
keys: o.f.t.info.keys,
}
}
func (o *singleOp) generateDelete(opt Options) DeleteStatement {
return DeleteStatement{
keyspace: o.f.t.keySpace.name,
table: o.f.t.Name(),
where: o.f.rs,
keys: o.f.t.info.keys,
}
}
func sortedKeys(m map[string]interface{}) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}