Files
gocqlx/session.go
sylwiaszunejko 7072863b0c Replace Unsafe with Strict mechanism
Previously by default the presence of a missing field in
a udt would result in an error reported. The Unsafe
mechanism could be used to ignore these fields.

This PR changes the default behavior to ignoring missing
fields and only reporting an error if Strict mode is used.
This approach is in line with the gocql.
2024-06-25 15:29:10 +02:00

77 lines
2.1 KiB
Go

// 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,
tr: DefaultBindTransformer,
strict: DefaultStrict,
}
}
// 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,
tr: DefaultBindTransformer,
strict: DefaultStrict,
}
}
// ExecStmt creates query and executes the given statement.
func (s Session) ExecStmt(stmt string) error {
return s.Query(stmt, nil).ExecRelease()
}