forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcassandra.go
240 lines (184 loc) · 4.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
228
229
230
231
232
233
234
235
236
237
238
239
240
package gorm
import (
"fmt"
"github.com/gocql/gocql"
"reflect"
"strings"
"time"
)
type cassandra struct {
Cluster *gocql.ClusterConfig
Session *Session
dsn dsn
}
type dsn struct {
keyspace string
hosts []string
}
type Session struct {
*gocql.Session
}
type Iter struct {
*gocql.Iter
}
func (i *Iter) Columns() ([]string, error) {
columns := make([]string, 0)
for _, column := range i.Iter.Columns() {
columns = append(columns, column.Name)
}
return columns, nil
}
func (i *Iter) Scan(dest ...interface{}) error {
result := i.Iter.Scan(dest...)
if !result {
return i.Err()
}
return nil
}
func (i *Iter) LastInsertId() (int64, error) {
return 0, nil
}
func (i *Iter) RowsAffected() (int64, error) {
return int64(len(i.Rows())), nil
}
func (s *Session) Close() error {
s.Session.Close()
if !s.Session.Closed() {
return fmt.Errorf("Can't close the session")
}
return nil
}
func parseDSN(source string) (dsn, error) {
result := dsn{}
for _, part := range strings.Split(source, " ") {
parts := strings.Split(part, "=")
if parts[0] == "keyspace" {
result.keyspace = parts[1]
}
if parts[0] == "hosts" {
result.hosts = strings.Split(parts[1], ",")
}
}
return result, nil
}
func NewCassandraDialect(source string) (Dialect, error) {
dsn, err := parseDSN(source)
if err != nil {
return nil, err
}
cass := &cassandra{
dsn: dsn,
}
return cass, nil
}
func (c cassandra) clone() Dialect {
return &cassandra{
dsn: c.dsn,
Cluster: c.Cluster,
Session: c.Session,
}
}
func (c cassandra) DB() Database {
return c.Session
}
func (c cassandra) Exec(query string, vars ...interface{}) (Result, error) {
qs := c.Session.Query(query, vars...)
iter := &Iter{qs.Iter()}
return iter, iter.Err()
}
func (c cassandra) Query(query string, vars ...interface{}) (Rows, error) {
iter := &Iter{c.Session.Query(query, vars...).Iter()}
return iter, iter.Err()
}
func (c cassandra) QueryRow(query string, vars ...interface{}) Row {
iter, _ := c.Query(query, vars...)
return iter
}
func (c *cassandra) Connect() error {
cluster := gocql.NewCluster(c.dsn.hosts...)
cluster.Keyspace = c.dsn.keyspace
c.Cluster = cluster
session, err := cluster.CreateSession()
if err != nil {
return err
}
c.Session = &Session{session}
return nil
}
func (cassandra) RollbackTransaction() error {
return TransactionNotSupported
}
func (cassandra) BeginTransaction() error {
return TransactionNotSupported
}
func (cassandra) CommitTransaction() error {
return TransactionNotSupported
}
func (c cassandra) CloseDB() error {
return c.Session.Close()
}
func (cassandra) ReturningStr(tableName, key string) string {
return ""
}
func (cassandra) SelectFromDummyTable() string {
return ""
}
func (cassandra) Quote(key string) string {
return fmt.Sprintf("%s", key)
}
func (cassandra) SupportLastInsertId() bool {
return false
}
func (c cassandra) databaseName(scope *Scope) string {
return c.Cluster.Keyspace
}
func (cassandra) HasTop() bool {
return false
}
func (cassandra) BinVar(i int) string {
return "$$" // ?
}
func (cassandra) SqlTag(value reflect.Value, size int, autoIncrease bool) string {
switch value.Kind() {
case reflect.Bool:
return "boolean"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
return "int"
case reflect.Int64, reflect.Uint64:
return "bigint"
case reflect.Float32, reflect.Float64:
return "float"
case reflect.String:
return "varchar"
case reflect.Struct:
if _, ok := value.Interface().(time.Time); ok {
return "timestamp"
}
if _, ok := value.Interface().(gocql.UUID); ok {
return "uuid"
}
default:
if _, ok := value.Interface().([]byte); ok {
return "blob"
}
}
panic(fmt.Sprintf("invalid sql type %s (%s) for cassandra", value.Type().Name(), value.Kind().String()))
}
func (cassandra) HasTable(scope *Scope, tableName string) bool {
var count int
scope.NewDB().Raw("SELECT count(*) FROM INFORMATION_SCHEMA.tables WHERE table_name = ? AND table_type = 'BASE TABLE'", tableName).Row().Scan(&count)
return count > 0
}
func (cassandra) HasColumn(scope *Scope, tableName string, columnName string) bool {
var count int
scope.NewDB().Raw("SELECT count(*) FROM INFORMATION_SCHEMA.columns WHERE table_name = ? AND column_name = ?", tableName, columnName).Row().Scan(&count)
return count > 0
}
func (cassandra) RemoveIndex(scope *Scope, indexName string) {
scope.NewDB().Exec(fmt.Sprintf("DROP INDEX %v", indexName))
}
func (cassandra) HasIndex(scope *Scope, tableName string, indexName string) bool {
var count int
scope.NewDB().Raw("SELECT count(*) FROM pg_indexes WHERE tablename = ? AND indexname = ?", tableName, indexName).Row().Scan(&count)
return count > 0
}