From 6c725a69f53d519629344cff7a4b244545e4454c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Garcia?= Date: Thu, 24 Sep 2020 14:17:19 -0300 Subject: [PATCH] Add Query & QueryNext funcs --- kiss_orm.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/kiss_orm.go b/kiss_orm.go index a1a11e8..61ea0d8 100644 --- a/kiss_orm.go +++ b/kiss_orm.go @@ -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,