This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
forked from gocassa/gocassa
-
Notifications
You must be signed in to change notification settings - Fork 13
/
connection.go
54 lines (47 loc) · 1.5 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
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 {
query := fmt.Sprintf("CREATE KEYSPACE %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1 };", name)
stmt := cqlStatement{query: query}
return c.q.Execute(stmt)
}
// DropKeySpace drops the keyspace having the given name.
func (c *connection) DropKeySpace(name string) error {
query := fmt.Sprintf("DROP KEYSPACE IF EXISTS %s", name)
stmt := cqlStatement{query: query}
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
}