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.
This commit is contained in:
committed by
Sylwia Szunejko
parent
6a60650668
commit
7072863b0c
20
queryx.go
20
queryx.go
@@ -95,7 +95,7 @@ type Queryx struct {
|
||||
Mapper *reflectx.Mapper
|
||||
*gocql.Query
|
||||
Names []string
|
||||
unsafe bool
|
||||
strict bool
|
||||
}
|
||||
|
||||
// Query creates a new Queryx from gocql.Query using a default mapper.
|
||||
@@ -107,7 +107,7 @@ func Query(q *gocql.Query, names []string) *Queryx {
|
||||
Names: names,
|
||||
Mapper: DefaultMapper,
|
||||
tr: DefaultBindTransformer,
|
||||
unsafe: DefaultUnsafe,
|
||||
strict: DefaultStrict,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,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
|
||||
// to an existing query instance.
|
||||
func (q *Queryx) Bind(v ...interface{}) *Queryx {
|
||||
q.Query.Bind(udtWrapSlice(q.Mapper, q.unsafe, v)...)
|
||||
q.Query.Bind(udtWrapSlice(q.Mapper, q.strict, v)...)
|
||||
return q
|
||||
}
|
||||
|
||||
@@ -219,7 +219,7 @@ func (q *Queryx) Bind(v ...interface{}) *Queryx {
|
||||
// row into the values pointed at by dest and discards the rest. If no rows
|
||||
// were selected, ErrNotFound is returned.
|
||||
func (q *Queryx) Scan(v ...interface{}) error {
|
||||
return q.Query.Scan(udtWrapSlice(q.Mapper, q.unsafe, v)...)
|
||||
return q.Query.Scan(udtWrapSlice(q.Mapper, q.strict, v)...)
|
||||
}
|
||||
|
||||
// Err returns any binding errors.
|
||||
@@ -351,14 +351,14 @@ func (q *Queryx) Iter() *Iterx {
|
||||
return &Iterx{
|
||||
Iter: q.Query.Iter(),
|
||||
Mapper: q.Mapper,
|
||||
unsafe: q.unsafe,
|
||||
strict: q.strict,
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
// Strict forces the query and iterators to report an error if there are missing fields.
|
||||
// By default when scanning a struct if result row has a column that cannot be mapped to
|
||||
// any destination it is ignored. With strict error is reported.
|
||||
func (q *Queryx) Strict() *Queryx {
|
||||
q.strict = true
|
||||
return q
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user