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
|
/**
* CQL Minus - cqlminus
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Paolo Lulli <kevwe.com>
* @copyright Paolo Lulli 2026
*/
package db
import (
"cqlminus/internal/config"
"fmt"
"time"
"github.com/gocql/gocql"
)
// Config holds the connection settings loaded from the JSON config file.
func Connect(cfg *config.Config) (*gocql.Session, error) {
cluster := gocql.NewCluster(cfg.Endpoint)
cluster.Keyspace = cfg.Keyspace
cluster.Consistency = gocql.LocalQuorum
cluster.Timeout = 10 * time.Second
if cfg.Username != "" {
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: cfg.Username,
Password: cfg.Password,
}
}
if cfg.TlsCertificate != "" {
cluster.SslOpts = buildSslOptions(cfg)
}
session, err := cluster.CreateSession()
if err != nil {
return nil, fmt.Errorf("connecting to cassandra: %w", err)
}
return session, nil
}
// QueryResult holds column names and row data returned from a CQL query.
type QueryResult struct {
Columns []string
Rows [][]string
}
func RunQuery(session *gocql.Session, query string) (*QueryResult, error) {
iter := session.Query(query).Iter()
columns := iter.Columns()
colNames := make([]string, len(columns))
for i, c := range columns {
colNames[i] = c.Name
}
result := &QueryResult{Columns: colNames}
row := make(map[string]interface{})
for iter.MapScan(row) {
strRow := make([]string, len(colNames))
for i, name := range colNames {
strRow[i] = formatValue(row[name])
}
result.Rows = append(result.Rows, strRow)
row = make(map[string]interface{})
}
if err := iter.Close(); err != nil {
return nil, fmt.Errorf("query error: %w", err)
}
return result, nil
}
|