/
cassandra.go
227 lines (205 loc) · 5.72 KB
/
cassandra.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package data
import (
"encoding/json"
"errors"
"fmt"
"skilldirectory/util"
"github.com/Sirupsen/logrus"
"github.com/gocql/gocql"
)
// TimestampFormat is a CSQL-compatible timestamp format
const TimestampFormat = "2006-01-02 15:04:05-0700"
type CassandraConnector struct {
*gocql.Session
*logrus.Logger
path string
port string
keyspace string
username string
password string
}
type CassandraQueryOptions struct {
Filters []Filter
}
type Filter struct {
key string
value string
id bool
}
func (f Filter) query() string {
queryString := fmt.Sprintf("%s", f.key)
queryString += " = "
if !f.id {
queryString += "'"
}
queryString += f.value
if !f.id {
queryString += "'"
}
return queryString
}
func NewCassandraConnector(path, port, keyspace, username,
password string) *CassandraConnector {
logger := util.LogInit()
logger.Printf("New Connector Path: %s, Port: %s, Keyspace: %s, Username: %s",
path, port, keyspace, username)
logger.Debug("Using Password: " + password)
cluster := gocql.NewCluster(path)
cluster.Keyspace = keyspace
cluster.Consistency = gocql.Quorum
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: username,
Password: password,
}
session, err := cluster.CreateSession()
if err != nil {
logger.Panic(err)
}
cassConn := CassandraConnector{
path: path,
port: port,
keyspace: keyspace,
username: username,
password: password,
}
cassConn.Session = session
cassConn.Logger = logger
return &cassConn
}
/*
NewCassandraQueryOptions creates a new options object.
key: query field name
value: query value
id: True if field is a UUID Cassandra key
*/
func NewCassandraQueryOptions(key, value string, id bool) CassandraQueryOptions {
filter := Filter{key, value, id}
return CassandraQueryOptions{
Filters: []Filter{filter},
}
}
/*
AddFilter adds a filter to an CassandraQueryOptions object
key: query field name
value: query value
id: True if field is a UUID Cassandra key
*/
func (o *CassandraQueryOptions) AddFilter(key, value string, id bool) {
filter := Filter{key, value, id}
o.Filters = append(o.Filters, filter)
}
func (c CassandraConnector) Save(table, key string, object interface{}) error {
b, err := json.Marshal(object)
if err != nil {
return err
}
query := "INSERT INTO " + table + " JSON '" + util.SanitizeInput(string(b)) + "'"
c.Debugf("Running this CQL Query: \n\t%q\n", query)
return c.Query(query).Exec()
}
func (c CassandraConnector) Read(table, id string, opts CassandraQueryOptions, object interface{}) error {
query := makeReadQueryStr(table, id, opts, c)
c.Debugf("Running this CQL Query: \n\t%q\n", query)
byteQ := []byte{}
err := c.Query(query).Consistency(gocql.One).Scan(&byteQ)
if err != nil {
return err
}
return json.Unmarshal(byteQ, &object)
}
func (c CassandraConnector) Delete(table, id string, opts CassandraQueryOptions) error {
query := makeDeleteQueryStr(table, id, opts, c)
if query == "" {
return errors.New("Attempting to delete with no id")
}
c.Debugf("Running the following DELETE query:\n\t%q\n", query)
return c.Query(query).Exec()
}
func makeReadQueryStr(table string, id string, opts CassandraQueryOptions, c CassandraConnector) string {
query := "SELECT JSON * FROM " + table + makeQueryConditionStr(table, id, opts, c)
c.Infof("Reading:%s,%s,%v", table, id, opts)
return query
}
func makeDeleteQueryStr(table string, id string, opts CassandraQueryOptions, c CassandraConnector) string {
query := "DELETE FROM " + table + makeQueryConditionStr(table, id, opts, c)
c.Infof("Deleting:%s,%s,%v", table, id, opts)
return query
}
func makeQueryConditionStr(table string, id string, opts CassandraQueryOptions, c CassandraConnector) string {
query := " WHERE "
firstField := true
if id != "" {
firstField = false
query += "id = " + id
}
for _, filter := range opts.Filters {
if filter.key == "id" {
continue
}
if filter.value == "" {
value, err := c.readCol(table, id, filter.key)
if err != nil {
c.Warnf("Read column failed:%s,%s,%s", table, id, filter.key)
continue
}
filter.value = value
}
if !firstField {
query += " AND "
}
query += filter.query()
firstField = false
}
query += ";"
return query
}
func (c CassandraConnector) readCol(table, id, col string) (string, error) {
// Get value for this column
colQuery := "SELECT " + col + " FROM " + table + " WHERE id = " + id
m := make(map[string]interface{})
err := c.Query(colQuery).Consistency(gocql.One).MapScan(m)
if err != nil {
return "", err
}
// value can be of different types. Based on type, append it onto the
// base query string.
var colVal string
switch v := m[col].(type) {
case int:
colVal = string(v)
case string:
colVal = "'" + v + "'"
case gocql.UUID:
colVal = v.String()
default:
return "", fmt.Errorf("Unrecognized value type, %T, for column, %q", m[col], col)
}
return colVal, nil
}
func (c CassandraConnector) ReadAll(table string, readType ReadAllInterface) ([]interface{}, error) {
return c.FilteredReadAll(table, CassandraQueryOptions{}, readType)
}
func (c CassandraConnector) FilteredReadAll(table string, opts CassandraQueryOptions, readType ReadAllInterface) ([]interface{}, error) {
query := "SELECT JSON * FROM " + table
if opts.Filters != nil {
query += " WHERE "
}
for _, filter := range opts.Filters {
query += filter.query()
}
query += ";"
queryBytes := []byte{}
queryObject := readType.GetType()
queryObjectArray := []interface{}{}
var err error
c.Debugf("Performing query: %s", query)
iter := c.Query(query).Iter()
for iter.Scan(&queryBytes) {
err = json.Unmarshal(queryBytes, &queryObject)
if err != nil {
return nil, fmt.Errorf("Read all unmarshal err: %s", err)
}
queryObjectArray = append(queryObjectArray, queryObject)
}
return queryObjectArray, nil
}