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
/
table.go
192 lines (172 loc) · 4.17 KB
/
table.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package gocassa
import (
"bytes"
"fmt"
"reflect"
"strconv"
"strings"
g "github.com/hailocab/gocassa/generate"
r "github.com/hailocab/gocassa/reflect"
)
type t struct {
keySpace *k
info *tableInfo
}
// Contains mostly analyzed information about the entity
type tableInfo struct {
keyspace, name string
marshalSource interface{}
fieldSource map[string]interface{}
keys Keys
fieldNames map[string]struct{} // This is here only to check containment
fields []string
fieldValues []interface{}
}
func newTableInfo(keyspace, name string, keys Keys, entity interface{}, fieldSource map[string]interface{}) *tableInfo {
cinf := &tableInfo{
keyspace: keyspace,
name: name,
marshalSource: entity,
keys: keys,
fieldSource: fieldSource,
}
fields := []string{}
values := []interface{}{}
for k, v := range fieldSource {
fields = append(fields, k)
values = append(values, v)
}
cinf.fieldNames = map[string]struct{}{}
for _, v := range fields {
cinf.fieldNames[v] = struct{}{}
}
cinf.fields = fields
cinf.fieldValues = values
return cinf
}
func (t *t) zero() interface{} {
return reflect.New(reflect.TypeOf(t.info.marshalSource)).Interface()
}
// Since we cant have Map -> [(k, v)] we settle for Map -> ([k], [v])
// #tuplelessLifeSucks
func keyValues(m map[string]interface{}) ([]string, []interface{}) {
keys := []string{}
values := []interface{}{}
for k, v := range m {
keys = append(keys, k)
values = append(values, v)
}
return keys, values
}
func toMap(i interface{}) (map[string]interface{}, bool) {
switch v := i.(type) {
//case M:
// return map[string]interface{}(v), true
case map[string]interface{}:
return v, true
}
return r.StructToMap(i)
}
func (t t) Where(rs ...Relation) Filter {
return filter{
t: t,
rs: rs,
}
}
func (t t) generateFieldNames() string {
xs := make([]string, len(t.info.fields))
for i, v := range t.info.fields {
xs[i] = strings.ToLower(v)
}
return strings.Join(xs, ", ")
}
// INSERT INTO Hollywood.NerdMovies (user_uuid, fan)
// VALUES ('cfd66ccc-d857-4e90-b1e5-df98a3d40cd6', 'johndoe')
//
// Gotcha: primkey must be first
func insert(keySpaceName, cfName string, fieldNames []string, opts Options) string {
placeHolders := make([]string, len(fieldNames))
for i := 0; i < len(fieldNames); i++ {
placeHolders[i] = "?"
}
lowerFieldNames := make([]string, len(fieldNames))
for i, v := range fieldNames {
lowerFieldNames[i] = strings.ToLower(v)
}
buf := new(bytes.Buffer)
buf.WriteString(fmt.Sprintf("INSERT INTO %s.%s (%s) VALUES (%s)",
keySpaceName,
cfName,
strings.Join(lowerFieldNames, ", "),
strings.Join(placeHolders, ", ")))
// Apply options
if opts.TTL != 0 {
buf.WriteString(" USING TTL ")
buf.WriteString(strconv.FormatFloat(opts.TTL.Seconds(), 'f', 0, 64))
}
return buf.String()
}
func (t t) SetWithOptions(i interface{}, opts Options) WriteOp {
m, ok := toMap(i)
if !ok {
panic("SetWithOptions: Incompatible type")
}
fields, values := keyValues(m)
stmt := insert(t.keySpace.name, t.info.name, fields, opts)
if t.keySpace.debugMode {
fmt.Println(stmt, values)
}
return newWriteOp(t.keySpace.qe, stmt, values)
}
func (t t) Set(i interface{}) WriteOp {
return t.SetWithOptions(i, Options{})
}
func (t t) Create() error {
if stmt, err := t.CreateStatement(); err != nil {
return err
} else {
return t.keySpace.qe.Execute(stmt)
}
}
// Drop table if exists and create it again
func (t t) Recreate() error {
if ex, err := t.keySpace.Exists(t.info.name); ex && err == nil {
if err := t.keySpace.DropTable(t.info.name); err != nil {
return err
}
} else if err != nil {
return err
}
return t.Create()
}
func (t t) CreateStatement() (string, error) {
return g.CreateTable(t.keySpace.name,
t.info.name,
t.info.keys.PartitionKeys,
t.info.keys.ClusteringColumns,
t.info.fields,
t.info.fieldValues)
}
//const (
// asc = iota
// desc
//)
//
//type Ordering struct {
// fieldName string
// order int
//}
//
//func ASC(fieldName string) Ordering {
// return Ordering{
// fieldName: fieldName,
// order: asc,
// }
//}
//
//func DESC(fieldName string) Ordering {
// return Ordering{
// fieldName: fieldName,
// order: asc,
// }
//}