This commit is contained in:
Michał Matczuk
2017-07-28 10:18:38 +02:00
parent 3b3c7087d2
commit 41bb8def2a
12 changed files with 247 additions and 132 deletions

View File

@@ -82,14 +82,18 @@ type Queryx struct {
Mapper *reflectx.Mapper
}
// Query creates a new Queryx from gocql.Query using a default mapper.
func Query(q *gocql.Query, names []string) Queryx {
return Queryx{
Query: q,
Names: names,
Mapper: DefaultMapper,
}
}
// BindStruct binds query named parameters using mapper.
func (q Queryx) BindStruct(arg interface{}) error {
m := q.Mapper
if m == nil {
m = DefaultMapper
}
arglist, err := bindStructArgs(q.Names, arg, m)
arglist, err := bindStructArgs(q.Names, arg, q.Mapper)
if err != nil {
return err
}
@@ -144,3 +148,13 @@ func bindMapArgs(names []string, arg map[string]interface{}) ([]interface{}, err
}
return arglist, nil
}
// QueryFunc creates Queryx from qb.Builder.ToCql() output.
type QueryFunc func(stmt string, names []string) Queryx
// SessionQuery creates QueryFunc that's session aware.
func SessionQuery(session *gocql.Session) QueryFunc {
return func(stmt string, names []string) Queryx {
return Query(session.Query(stmt), names)
}
}