Skip to content

Commit

Permalink
Introduce Unsafe method on Queryx
Browse files Browse the repository at this point in the history
It enables local control over `unsafe` mode for .Bind methods of `Queryx` and iterators
spawn by it.
  • Loading branch information
dkropachev committed Jun 14, 2024
1 parent 67b02d5 commit 50c2977
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
8 changes: 4 additions & 4 deletions iterx.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/scylladb/go-reflectx"
)

// DefaultUnsafe enables the behavior of forcing the iterator to ignore
// DefaultUnsafe enables the behavior of forcing queries and iterators to ignore
// missing fields for all queries. See Unsafe below for more information.
var DefaultUnsafe bool

Expand Down Expand Up @@ -206,9 +206,9 @@ func (iter *Iterx) scanAll(dest interface{}) bool {

// isScannable takes the reflect.Type and the actual dest value and returns
// whether or not it's Scannable. t is scannable if:
// * ptr to t implements gocql.Unmarshaler, gocql.UDTUnmarshaler or UDT
// * it is not a struct
// * it has no exported fields
// - ptr to t implements gocql.Unmarshaler, gocql.UDTUnmarshaler or UDT
// - it is not a struct
// - it has no exported fields
func (iter *Iterx) isScannable(t reflect.Type) bool {
ptr := reflect.PtrTo(t)
switch {
Expand Down
6 changes: 1 addition & 5 deletions iterx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,12 +480,8 @@ func TestIterxUnsafe(t *testing.T) {
})

t.Run("select default unsafe", func(t *testing.T) {
gocqlx.DefaultUnsafe = true
defer func() {
gocqlx.DefaultUnsafe = false
}()
var v []UnsafeTable
err := session.Query(stmt, nil).Iter().Select(&v)
err := session.Query(stmt, nil).Unsafe().Iter().Select(&v)
if err != nil {
t.Fatal("Select() failed:", err)
}
Expand Down
18 changes: 14 additions & 4 deletions queryx.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ type Queryx struct {
Names []string
Mapper *reflectx.Mapper

tr Transformer
err error
unsafe bool
tr Transformer
err error
}

// Query creates a new Queryx from gocql.Query using a default mapper.
Expand All @@ -107,6 +108,7 @@ func Query(q *gocql.Query, names []string) *Queryx {
Names: names,
Mapper: DefaultMapper,
tr: DefaultBindTransformer,
unsafe: DefaultUnsafe,
}
}

Expand Down Expand Up @@ -210,7 +212,7 @@ func (q *Queryx) bindMapArgs(arg map[string]interface{}) ([]interface{}, error)
// Bind sets query arguments of query. This can also be used to rebind new query arguments
// to an existing query instance.
func (q *Queryx) Bind(v ...interface{}) *Queryx {
q.Query.Bind(udtWrapSlice(q.Mapper, DefaultUnsafe, v)...)
q.Query.Bind(udtWrapSlice(q.Mapper, q.unsafe, v)...)
return q
}

Expand Down Expand Up @@ -343,6 +345,14 @@ func (q *Queryx) Iter() *Iterx {
return &Iterx{
Iter: q.Query.Iter(),
Mapper: q.Mapper,
unsafe: DefaultUnsafe,
unsafe: q.unsafe,
}
}

// Unsafe forces the query and iterators to ignore missing fields. By default when scanning
// a struct if result row has a column that cannot be mapped to any destination
// field an error is reported. With unsafe such columns are ignored.
func (q *Queryx) Unsafe() *Queryx {
q.unsafe = true
return q
}
5 changes: 4 additions & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func NewSession(session *gocql.Session) Session {
// the created session to gocqlx.Session.
//
// Example:
// session, err := gocqlx.WrapSession(cluster.CreateSession())
//
// session, err := gocqlx.WrapSession(cluster.CreateSession())
func WrapSession(session *gocql.Session, err error) (Session, error) {
return Session{
Session: session,
Expand All @@ -50,6 +51,7 @@ func (s Session) ContextQuery(ctx context.Context, stmt string, names []string)
Names: names,
Mapper: s.Mapper,
tr: DefaultBindTransformer,
unsafe: DefaultUnsafe,
}
}

Expand All @@ -64,6 +66,7 @@ func (s Session) Query(stmt string, names []string) *Queryx {
Names: names,
Mapper: s.Mapper,
tr: DefaultBindTransformer,
unsafe: DefaultUnsafe,
}
}

Expand Down

0 comments on commit 50c2977

Please sign in to comment.