-
Notifications
You must be signed in to change notification settings - Fork 69
/
connection.go
58 lines (50 loc) · 1.57 KB
/
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
package gocassa
import (
"fmt"
)
type connection struct {
q QueryExecutor
}
// Connect to a cluster.
// If you are happy with default the options use this, if you need anything fancier, use `NewConnection`
func Connect(nodeIps []string, username, password string) (Connection, error) {
qe, err := newGoCQLBackend(nodeIps, username, password)
if err != nil {
return nil, err
}
return &connection{
q: qe,
}, nil
}
// NewConnection creates a Connection with a custom query executor.
// Use `Connect` if you just want to talk to Cassandra with the default options.
// See `GoCQLSessionToQueryExecutor` if you want to use a gocql session with your own options as a `QueryExecutor`
func NewConnection(q QueryExecutor) Connection {
return &connection{
q: q,
}
}
// CreateKeySpace creates a keyspace with the given name. Only used to create test keyspaces.
func (c *connection) CreateKeySpace(name string) error {
stmt := fmt.Sprintf("CREATE KEYSPACE %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1 };", name)
return c.q.Execute(stmt)
}
// DropKeySpace drops the keyspace having the given name.
func (c *connection) DropKeySpace(name string) error {
stmt := fmt.Sprintf("DROP KEYSPACE IF EXISTS %s", name)
return c.q.Execute(stmt)
}
// KeySpace returns the keyspace having the given name.
func (c *connection) KeySpace(name string) KeySpace {
k := &k{
qe: c.q,
name: name,
}
k.tableFactory = k
return k
}
// Close closes the current session
// The connection should not be used again after calling Close()
func (c *connection) Close() {
c.q.Close()
}