-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.go
More file actions
56 lines (46 loc) · 1.37 KB
/
db.go
File metadata and controls
56 lines (46 loc) · 1.37 KB
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
package dago
import (
"github.com/gocql/gocql"
)
type CassandraDb struct {
session *gocql.Session
helper *CQLHelper
da *DataAccess
}
// Convenience method to initiate a session and return a CassandraDb
// with a default cluster configuration.
func Open(keyspace string, hosts ...string) (*CassandraDb, error) {
cluster := gocql.NewCluster(hosts...)
cluster.Keyspace = keyspace
session, err := cluster.CreateSession()
if err != nil {
return nil, err
}
return Wrap(session), nil
}
// Wraps an existing gocql session into a CassandraDb to gain access
// to our CQL helper and Data Access object
func Wrap(session *gocql.Session) *CassandraDb {
helper := NewCQLHelper(session)
da := NewDataAccess(helper)
store := &CassandraDb{session, helper, da}
return store
}
// Return a DataAccess reference. DataAccess is the highest level facility to
// manipulate data from/to Cassandra
func (self *CassandraDb) GetDA() *DataAccess {
return self.da
}
// Return a CQLHelper reference. The CQL helper is an intermediate level tool that
// smoothes out the wrinkles in generate CQL statement strings.
func (self *CassandraDb) GetHelper() *CQLHelper {
return self.helper
}
// Return the gocql Session reference
func (self *CassandraDb) GetSession() *gocql.Session {
return self.session
}
// Close the underlying connection.
func (self *CassandraDb) Close() {
self.session.Close()
}