forked from scylladb/gocqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
71 lines (62 loc) · 2.03 KB
/
session.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
// Copyright (C) 2017 ScyllaDB
// Use of this source code is governed by a ALv2-style
// license that can be found in the LICENSE file.
package gocqlx
import (
"context"
"github.com/gocql/gocql"
"github.com/scylladb/go-reflectx"
)
// Session wraps gocql.Session and provides a modified Query function that
// returns Queryx instance.
// The original Session instance can be accessed as Session.
// The default mapper uses `db` tag and automatically converts struct field
// names to snake case. If needed package reflectx provides constructors
// for other types of mappers.
type Session struct {
*gocql.Session
Mapper *reflectx.Mapper
}
// NewSession wraps existing gocql.session.
func NewSession(session *gocql.Session) Session {
return Session{
Session: session,
Mapper: DefaultMapper,
}
}
// WrapSession should be called on CreateSession() gocql function to convert
// the created session to gocqlx.Session.
//
// Example:
// session, err := gocqlx.WrapSession(cluster.CreateSession())
func WrapSession(session *gocql.Session, err error) (Session, error) {
return Session{
Session: session,
Mapper: DefaultMapper,
}, err
}
// ContextQuery is a helper function that allows to pass context when creating
// a query, see the "Query" function .
func (s Session) ContextQuery(ctx context.Context, stmt string, names []string) *Queryx {
return &Queryx{
Query: s.Session.Query(stmt).WithContext(ctx),
Names: names,
Mapper: s.Mapper,
}
}
// Query creates a new Queryx using the session mapper.
// The stmt and names parameters are typically result of a query builder
// (package qb) ToCql() function or come from table model (package table).
// The names parameter is a list of query parameters' names and it's used for
// binding.
func (s Session) Query(stmt string, names []string) *Queryx {
return &Queryx{
Query: s.Session.Query(stmt),
Names: names,
Mapper: s.Mapper,
}
}
// ExecStmt creates query and executes the given statement.
func (s Session) ExecStmt(stmt string) error {
return s.Query(stmt, nil).ExecRelease()
}