queryx: added Get, GetRelease, Select and SelectRelease helper functions

I have piggy backed with this change some docs update.

Fixes #41
This commit is contained in:
Michał Matczuk
2018-05-23 10:40:32 +02:00
parent 5739e64887
commit 6e4a05de42
4 changed files with 102 additions and 69 deletions

View File

@@ -191,9 +191,49 @@ func (q *Queryx) Exec() error {
return q.Query.Exec()
}
// ExecRelease performs exec and releases the query, a released query cannot be
// ExecRelease calls Exec and releases the query, a released query cannot be
// reused.
func (q *Queryx) ExecRelease() error {
defer q.Release()
return q.Exec()
}
// Get scans first row into a destination. If the destination type is a struct
// pointer, then Iter.StructScan will be used. If the destination is some
// other type, then the row must only have one column which can scan into that
// type.
//
// If no rows were selected, ErrNotFound is returned.
func (q *Queryx) Get(dest interface{}) error {
if q.err != nil {
return q.err
}
return Iter(q.Query).Get(dest)
}
// GetRelease calls Get and releases the query, a released query cannot be
// reused.
func (q *Queryx) GetRelease(dest interface{}) error {
defer q.Release()
return q.Get(dest)
}
// Select scans all rows into a destination, which must be a pointer to slice
// of any type. If the destination slice type is a struct, then Iter.StructScan
// will be used on each row. If the destination is some other type, then each
// row must only have one column which can scan into that type.
//
// If no rows were selected, ErrNotFound is NOT returned.
func (q *Queryx) Select(dest interface{}) error {
if q.err != nil {
return q.err
}
return Iter(q.Query).Select(dest)
}
// SelectRelease calls Select and releases the query, a released query cannot be
// reused.
func (q *Queryx) SelectRelease(dest interface{}) error {
defer q.Release()
return q.Select(dest)
}