Introduce Unsafe method on Queryx
It enables local control over `unsafe` mode for .Bind methods of `Queryx` and iterators spawn by it.
This commit is contained in:
committed by
Sylwia Szunejko
parent
a222c1f067
commit
c6f942afc7
2
iterx.go
2
iterx.go
@@ -13,7 +13,7 @@ import (
|
|||||||
"github.com/scylladb/go-reflectx"
|
"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.
|
// missing fields for all queries. See Unsafe below for more information.
|
||||||
var DefaultUnsafe bool
|
var DefaultUnsafe bool
|
||||||
|
|
||||||
|
|||||||
@@ -484,12 +484,8 @@ func TestIterxUnsafe(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("select default unsafe", func(t *testing.T) {
|
t.Run("select default unsafe", func(t *testing.T) {
|
||||||
gocqlx.DefaultUnsafe = true
|
|
||||||
defer func() {
|
|
||||||
gocqlx.DefaultUnsafe = false
|
|
||||||
}()
|
|
||||||
var v []UnsafeTable
|
var v []UnsafeTable
|
||||||
err := session.Query(stmt, nil).Iter().Select(&v)
|
err := session.Query(stmt, nil).Unsafe().Iter().Select(&v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Select() failed:", err)
|
t.Fatal("Select() failed:", err)
|
||||||
}
|
}
|
||||||
|
|||||||
16
queryx.go
16
queryx.go
@@ -94,7 +94,8 @@ type Queryx struct {
|
|||||||
tr Transformer
|
tr Transformer
|
||||||
Mapper *reflectx.Mapper
|
Mapper *reflectx.Mapper
|
||||||
*gocql.Query
|
*gocql.Query
|
||||||
Names []string
|
Names []string
|
||||||
|
unsafe bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query creates a new Queryx from gocql.Query using a default mapper.
|
// Query creates a new Queryx from gocql.Query using a default mapper.
|
||||||
@@ -106,6 +107,7 @@ func Query(q *gocql.Query, names []string) *Queryx {
|
|||||||
Names: names,
|
Names: names,
|
||||||
Mapper: DefaultMapper,
|
Mapper: DefaultMapper,
|
||||||
tr: DefaultBindTransformer,
|
tr: DefaultBindTransformer,
|
||||||
|
unsafe: DefaultUnsafe,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,7 +211,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
|
// Bind sets query arguments of query. This can also be used to rebind new query arguments
|
||||||
// to an existing query instance.
|
// to an existing query instance.
|
||||||
func (q *Queryx) Bind(v ...interface{}) *Queryx {
|
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
|
return q
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -342,6 +344,14 @@ func (q *Queryx) Iter() *Iterx {
|
|||||||
return &Iterx{
|
return &Iterx{
|
||||||
Iter: q.Query.Iter(),
|
Iter: q.Query.Iter(),
|
||||||
Mapper: q.Mapper,
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ func (s Session) ContextQuery(ctx context.Context, stmt string, names []string)
|
|||||||
Names: names,
|
Names: names,
|
||||||
Mapper: s.Mapper,
|
Mapper: s.Mapper,
|
||||||
tr: DefaultBindTransformer,
|
tr: DefaultBindTransformer,
|
||||||
|
unsafe: DefaultUnsafe,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +66,7 @@ func (s Session) Query(stmt string, names []string) *Queryx {
|
|||||||
Names: names,
|
Names: names,
|
||||||
Mapper: s.Mapper,
|
Mapper: s.Mapper,
|
||||||
tr: DefaultBindTransformer,
|
tr: DefaultBindTransformer,
|
||||||
|
unsafe: DefaultUnsafe,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user