-
Notifications
You must be signed in to change notification settings - Fork 1
/
cql_connection.go
161 lines (144 loc) · 3.92 KB
/
cql_connection.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
package main
import (
"bytes"
"fmt"
"io"
"reflect"
"sort"
"strings"
"github.com/ansel1/merry"
"github.com/gocql/gocql"
"github.com/olekukonko/tablewriter"
)
type CQLConnection struct {
session *gocql.Session
}
var CassandraErrorMap = map[int]string{
0x0000: "Server error (0x0000)",
0x000A: "Protocol error (0x000A)",
0x0100: "Bad credentials (0x0100)",
0x1000: "Unavailable (0x1000)",
0x1001: "Overloaded (0x1001)",
0x1002: "Bootstrapping (0x1002)",
0x1003: "Truncate error (0x1003)",
0x1100: "Write timeout (0x1100)",
0x1200: "Read timeout (0x1200)",
0x1300: "Read failure (0x1300)",
0x1400: "Function failure (0x1400)",
0x1500: "Write failure (0x1500)",
0x1600: "CDC write failure (0x1600)",
0x2000: "Syntax error (0x2000)",
0x2100: "Unauthorized (0x2100)",
0x2200: "Invalid (0x2200)",
0x2300: "Config error (0x2300)",
0x2400: "Already exists (0x2400)",
0x2500: "Unprepared (0x2500)",
}
// Result of execution of a CQL statement
type CQLResult struct {
status string
code string
message string
warnings []string
names []string
types []string
rows [][]string
}
func (result *CQLResult) String() string {
var offset = " "
buf := new(bytes.Buffer)
if result.status != "OK" {
fmt.Fprintf(buf, "%s%7s: %s\n", offset, "status", result.status)
fmt.Fprintf(buf, "%s%7s: %s\n", offset, "code", result.code)
fmt.Fprintf(buf, "%s%7s: %s\n", offset, "message", result.message)
return string(buf.Bytes())
}
if len(result.warnings) != 0 {
fmt.Fprintf(buf, "%s%8s: %+v\n", offset, "warnings", result.warnings)
}
if len(result.rows) != 0 {
fmt.Fprint(buf, offset)
table := tablewriter.NewWriter(buf)
table.SetHeader(result.names)
// Shift all rows by offset
table.SetNewLine("\n" + offset)
for _, v := range result.rows {
table.Append(v)
}
table.Render() // Send output
// Trim last offset
buf.Truncate(buf.Len() - len(offset))
} else {
fmt.Fprint(buf, " OK\n")
}
return string(buf.Bytes())
}
// Go maps are unordered.
// Pretty print them in string sorted order of key
func prettyPrint(iface interface{}) string {
v := reflect.Indirect(reflect.ValueOf(iface))
// Todo: unwrap other composite types such as slices, structs and arrays
if v.Type().Kind() != reflect.Map {
return fmt.Sprint(v.Interface())
}
var keys = v.MapKeys()
var strkeys = make([]string, len(keys))
var strmap = make(map[string]string)
for i, key := range keys {
strkeys[i] = prettyPrint(key)
strmap[strkeys[i]] = prettyPrint(v.MapIndex(key))
}
sort.Strings(strkeys)
buf := new(bytes.Buffer)
fmt.Fprintf(buf, "map[")
for i, key := range strkeys {
fmt.Fprintf(buf, "%s:%s", key, strmap[key])
if i+1 < len(strkeys) {
fmt.Fprintf(buf, " ")
}
}
fmt.Fprintf(buf, "]")
return string(buf.Bytes())
}
func (c *CQLConnection) Execute(cql string) (string, error) {
var result CQLResult
query := c.session.Query(cql)
iter := query.Iter()
row, err := iter.RowData()
if err == nil {
result.status = "OK"
result.warnings = iter.Warnings()
for _, column := range iter.Columns() {
result.names = append(result.names, column.Name)
result.types = append(result.types, column.TypeInfo.Type().String())
}
for {
if !iter.Scan(row.Values...) {
break
}
strrow := make([]string, len(row.Values))
for i, v := range row.Values {
strrow[i] = prettyPrint(v)
}
result.rows = append(result.rows, strrow)
}
} else {
switch e := err.(type) {
case gocql.RequestError:
result.status = "ERROR"
result.code = CassandraErrorMap[e.Code()]
result.message = fmt.Sprintf("%.80s", strings.Split(e.Message(), "\n")[0])
default:
if err == io.EOF {
return "", merry.New("Got EOF from server: check out vardir, it has most probably crashed.")
}
ylog.Printf("got gocql error of type %v, %+v", e, err)
// Transport error or internal driver error, propagate up
return "", merry.Wrap(err)
}
}
return result.String(), nil
}
func (c *CQLConnection) Close() {
c.session.Close()
}