Add Query & QueryNext funcs

pull/2/head
Vinícius Garcia 2020-09-24 14:17:19 -03:00
parent 3f888e0420
commit 6c725a69f5
1 changed files with 29 additions and 0 deletions

View File

@ -17,6 +17,8 @@ type ORMProvider interface {
Update(ctx context.Context, intems ...interface{}) error
}
type Iterator interface{}
// Client ...
type Client struct {
tableName string
@ -63,6 +65,33 @@ func (c Client) Find(
return it.Error
}
// Query build an iterator for querying several
// results from the database
func (c Client) Query(
ctx context.Context,
query string,
params ...interface{},
) (Iterator, error) {
it := c.db.Raw(query, params...)
return it, it.Error
}
// QueryNext parses the next row of a query
// and updates the item argument that must be
// passed by reference.
func (c Client) QueryNext(
ctx context.Context,
rawIt Iterator,
item interface{},
) error {
it, ok := rawIt.(*gorm.DB)
if !ok {
return fmt.Errorf("invalid iterator received on QueryNext()")
}
it.Scan(item)
return it.Error
}
// GetByID recovers a single entity from the database by the ID field.
func (c Client) GetByID(
ctx context.Context,