This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection.go
108 lines (93 loc) · 2.38 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
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
package cql
import (
"context"
"database/sql/driver"
)
// Close a database connection
func (cqlConn *cqlConnStruct) Close() error {
if cqlConn.session != nil {
cqlConn.session.Close()
cqlConn.session = nil
}
return nil
}
// Ping a database connection
func (cqlConn *cqlConnStruct) Ping(ctx context.Context) error {
var err error
if cqlConn.session == nil {
cqlConn.session, err = cqlConn.clusterConfig.CreateSession()
if err != nil {
cqlConn.Close()
cqlConn.logger.Print("Ping CreateSession error: ", err)
return driver.ErrBadConn
}
cqlConn.pingQuery = cqlConn.session.Query("select cql_version from system.local")
}
iter := cqlConn.pingQuery.WithContext(ctx).Iter()
rowData, err := iter.RowData()
if err != nil {
iter.Close()
cqlConn.Close()
cqlConn.logger.Print("Ping RowData error: ", err)
return driver.ErrBadConn
}
if len(rowData.Values) != 1 {
iter.Close()
cqlConn.Close()
cqlConn.logger.Print("Ping len(Values) != 1")
return driver.ErrBadConn
}
if !iter.Scan(rowData.Values...) {
err = iter.Close()
if err != nil {
cqlConn.Close()
} else {
err = cqlConn.Close()
}
cqlConn.logger.Print("Ping Scan error: ", err)
return driver.ErrBadConn
}
err = iter.Close()
if err != nil {
cqlConn.Close()
cqlConn.logger.Print("Ping iter Close error: ", err)
return driver.ErrBadConn
}
data, ok := rowData.Values[0].(*string)
if !ok {
cqlConn.Close()
cqlConn.logger.Print("Ping Value not *string")
return driver.ErrBadConn
}
if len(*data) < 1 {
cqlConn.Close()
cqlConn.logger.Print("Ping len(data) < 1")
return driver.ErrBadConn
}
return nil
}
// Prepare a query, uses connection conntext
func (cqlConn *cqlConnStruct) Prepare(query string) (driver.Stmt, error) {
return cqlConn.PrepareContext(cqlConn.context, query)
}
// Prepare a query with context
func (cqlConn *cqlConnStruct) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
var err error
if cqlConn.session == nil {
err = cqlConn.Ping(ctx)
if err != nil {
return nil, err
}
}
return &CqlStmt{
CqlQuery: cqlConn.session.Query(query).WithContext(ctx),
}, nil
}
// Begin not supported
func (cqlConn *cqlConnStruct) Begin() (driver.Tx, error) {
return nil, ErrNotSupported
}
// BeginTx not supported
func (cqlConn *cqlConnStruct) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
return nil, ErrNotSupported
}