-
Notifications
You must be signed in to change notification settings - Fork 0
/
cql.go
184 lines (165 loc) · 3.93 KB
/
cql.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
package cql
import (
"fmt"
"strings"
"github.com/gocql/gocql"
)
var operators = map[string]string{
"$eq": "=",
"$gte": ">=",
"$gt": ">",
"$lt": "<",
"$lte": "<=",
"$in": "IN",
}
// CError is error from CQL
type CError struct {
Msg string
Code int
}
func (e *CError) Error() string {
if e == nil {
return ""
}
return fmt.Sprintf("CQL ERROR: %s", e.Msg)
}
// Views is for materialized views
type Views struct {
Name string
PartitionKey []string
ClusterKey []string
Select []string
}
// Table for cassandra table
//
// Example
// userTable := &cql.Table{
// Conn: session,
// TableName: "USERS",
// Model: &PartnerAPIAuth{},
// MaterializedView: []cql.Views{
// cql.Views{
// Name: "user_view1",
// Select: []string{"phone"},
// },
// },
// }
type Table struct {
Conn *gocql.Session
TableName string
Model interface{}
PartitionKey []string
ClusterKey []string
MaterializedView []Views
}
// QOpt is the options for Find
type QOpt struct {
AllowFiltering bool
Consistency string
Limit int
View string
ViewID int
BindTo interface{}
}
// InsertIfNotExistsResult is response for insert if not exists true
type InsertIfNotExistsResult struct {
Applied bool
Result map[string]interface{}
}
// Q is Query type a shortcut for map[string]interface{}
type Q map[string]interface{}
// Find is used to perform select queries
// result, err := userTable.Find(cql.Q{
// "where": cql.Q{
// "phone": "9895774319",
// },
// }, cql.QOpt{
// AllowFiltering: true,
// ViewID: 1,
//})
func (t *Table) Find(query Q, options QOpt) ([]map[string]interface{}, error) {
selectedCol := t.getSelectedColumns(query, options)
tableName := t.TableName
if options.View != "" {
for _, view := range t.MaterializedView {
if options.View == view.Name {
tableName = options.View
}
}
} else if len(t.MaterializedView) >= options.ViewID && options.ViewID > 0 {
tableName = t.MaterializedView[options.ViewID-1].Name
}
stmt := fmt.Sprintf(`SELECT %s FROM "%s"`, selectedCol, tableName)
values := make([]interface{}, 0)
whereCondition := parseQuery(query["where"].(Q), &values)
if len(values) > 0 {
stmt += fmt.Sprintf(" WHERE %s", whereCondition)
}
if options.Limit > 0 {
stmt += fmt.Sprintf(" LIMIT %d", options.Limit)
}
if options.AllowFiltering {
stmt += " ALLOW FILTERING"
}
iter := t.Conn.Query(stmt, values...).Iter()
result := make([]map[string]interface{}, 0)
for {
// New map each iteration
row := make(map[string]interface{})
if !iter.MapScan(row) {
break
}
result = append(result, row)
}
if err := iter.Close(); err != nil {
return nil, &CError{err.Error(), UnknownError}
}
return result, nil
}
// FindOne is used to perform get one result
// result, err := userTable.Find(cql.Q{
// "where": cql.Q{
// "phone": "9895774319",
// },
// }, cql.QOpt{
// AllowFiltering: true,
// ViewID: 1,
// })
func (t *Table) FindOne(query Q, options QOpt) (map[string]interface{}, *CError) {
options.Limit = 1
result, err := t.Find(query, options)
if err != nil {
return nil, &CError{err.Error(), UnknownError}
} else if len(result) == 0 {
return nil, &CError{"No Matching Row", NoMatchingRow}
}
if len(result) > 0 && options.BindTo != nil {
BindStruct(options.BindTo, result[0])
}
return result[0], nil
}
// Insert is used to to insert row if not exists
func (t *Table) Insert(input map[string]interface{}) (bool, error) {
stmt := fmt.Sprintf(`INSERT INTO "%s"`, t.TableName)
columns := ""
values := []interface{}{}
for key, value := range input {
values = append(values, value)
columns += `"` + key + `"` + ","
}
columns = strings.Trim(columns, ",")
stmt += " (" + columns + ") "
stmt += "VALUES("
for range values {
stmt += "?,"
}
stmt = strings.Trim(stmt, ",")
stmt += ")"
err := t.Conn.Query(
stmt,
values...).Exec()
if err != nil {
return false, err
}
return true, nil
}