forked from gocassa/gocassa
/
generate.go
170 lines (157 loc) · 4.07 KB
/
generate.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
package gocassa
import (
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/gocql/gocql"
)
// CREATE TABLE users (
// user_name varchar PRIMARY KEY,
// password varchar,
// gender varchar,
// session_token varchar,
// state varchar,
// birth_year bigint
// );
//
// CREATE TABLE emp (
// empID int,
// deptID int,
// first_name varchar,
// last_name varchar,
// PRIMARY KEY (empID, deptID)
// );
//
func createTable(keySpace, cf string, partitionKeys, colKeys []string, fields []string, values []interface{}, order []ClusteringOrderColumn) (string, error) {
firstLine := fmt.Sprintf("CREATE TABLE %v.%v (", keySpace, cf)
fieldLines := []string{}
for i, _ := range fields {
typeStr, err := stringTypeOf(values[i])
if err != nil {
return "", err
}
l := " " + strings.ToLower(fields[i]) + " " + typeStr
fieldLines = append(fieldLines, l)
}
str := " PRIMARY KEY ((%v) %v)"
if len(colKeys) > 0 {
str = " PRIMARY KEY ((%v), %v)"
}
fieldLines = append(fieldLines, fmt.Sprintf(str, j(partitionKeys), j(colKeys)))
lines := []string{
firstLine,
strings.Join(fieldLines, ",\n"),
")",
}
if len(order) > 0 {
orderStrs := make([]string, len(order))
for i, o := range order {
dir := "ASC"
if o.Direction == DESC {
dir = "DESC"
}
orderStrs[i] = fmt.Sprintf("%v %v", o.Column, dir)
}
orderLine := fmt.Sprintf("WITH CLUSTERING ORDER BY (%v)", strings.Join(orderStrs, ", "))
lines = append(lines, orderLine)
}
lines = append(lines, ";")
stmt := strings.Join(lines, "\n")
return stmt, nil
}
func j(s []string) string {
s1 := []string{}
for _, v := range s {
s1 = append(s1, strings.ToLower(v))
}
return strings.Join(s1, ", ")
}
func createKeyspace(keyspaceName string) string {
return fmt.Sprintf("CREATE KEYSPACE \"%v\" WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'eu-west-1' : 3}", keyspaceName)
}
func cassaType(i interface{}) gocql.Type {
switch i.(type) {
case int, int32:
return gocql.TypeInt
case int64:
return gocql.TypeBigInt
case int8, int16, uint, uint8, uint16, uint32, uint64:
return gocql.TypeVarint
case string:
return gocql.TypeVarchar
case float32:
return gocql.TypeFloat
case float64:
return gocql.TypeDouble
case bool:
return gocql.TypeBoolean
case time.Time:
return gocql.TypeTimestamp
case gocql.UUID:
return gocql.TypeUUID
case []byte:
return gocql.TypeBlob
case Counter:
return gocql.TypeCounter
}
return gocql.TypeCustom
}
func stringTypeOf(i interface{}) (string, error) {
_, isByteSlice := i.([]byte)
if !isByteSlice {
// Check if we found a higher kinded type
switch reflect.ValueOf(i).Kind() {
case reflect.Slice:
elemVal := reflect.Indirect(reflect.New(reflect.TypeOf(i).Elem())).Interface()
ct := cassaType(elemVal)
if ct == gocql.TypeCustom {
return "", fmt.Errorf("Unsupported type %T", i)
}
return fmt.Sprintf("list<%v>", ct), nil
case reflect.Map:
keyVal := reflect.Indirect(reflect.New(reflect.TypeOf(i).Key())).Interface()
elemVal := reflect.Indirect(reflect.New(reflect.TypeOf(i).Elem())).Interface()
keyCt := cassaType(keyVal)
elemCt := cassaType(elemVal)
if keyCt == gocql.TypeCustom || elemCt == gocql.TypeCustom {
return "", fmt.Errorf("Unsupported map key or value type %T", i)
}
return fmt.Sprintf("map<%v, %v>", keyCt, elemCt), nil
}
}
ct := cassaType(i)
if ct == gocql.TypeCustom {
return "", fmt.Errorf("Unsupported type %T", i)
}
return cassaTypeToString(ct)
}
func cassaTypeToString(t gocql.Type) (string, error) {
switch t {
case gocql.TypeInt:
return "int", nil
case gocql.TypeBigInt:
return "bigint", nil
case gocql.TypeVarint:
return "varint", nil
case gocql.TypeVarchar:
return "varchar", nil
case gocql.TypeFloat:
return "float", nil
case gocql.TypeDouble:
return "double", nil
case gocql.TypeBoolean:
return "boolean", nil
case gocql.TypeTimestamp:
return "timestamp", nil
case gocql.TypeUUID:
return "uuid", nil
case gocql.TypeBlob:
return "blob", nil
case gocql.TypeCounter:
return "counter", nil
default:
return "", errors.New("unkown cassandra type")
}
}