diff --git a/sqlt.go b/sqlt.go index 5b61b30..f39eb28 100644 --- a/sqlt.go +++ b/sqlt.go @@ -23,6 +23,14 @@ type Rows interface { type ExtractFunc func(Rows) error +func (e ExtractFunc) Extract(rs Rows) error { + return e(rs) +} + +type RowsExtractor interface { + Extract(Rows) error +} + type Dbop struct { Maker *sqlx.DB @@ -41,7 +49,7 @@ func New(db *sqlx.DB, maker Maker) *Dbop { } } -func (c *Dbop) TQuery(ctx context.Context, id string, param interface{}, h ExtractFunc) error { +func (c *Dbop) TQuery(ctx context.Context, id string, param interface{}, h RowsExtractor) error { return query(ctx, c, id, param, h) } @@ -50,7 +58,7 @@ func (c *Dbop) TExec(ctx context.Context, id string, param interface{}) (r sql.R return } -func (c *Dbop) TExecRtn(ctx context.Context, id string, param interface{}, h ExtractFunc) error { +func (c *Dbop) TExecRtn(ctx context.Context, id string, param interface{}, h RowsExtractor) error { return query(ctx, c, id, param, h) } @@ -71,7 +79,7 @@ type Txop struct { *sqlx.Tx } -func (t *Txop) TQuery(ctx context.Context, id string, param interface{}, h ExtractFunc) error { +func (t *Txop) TQuery(ctx context.Context, id string, param interface{}, h RowsExtractor) error { return query(ctx, t, id, param, h) } @@ -80,7 +88,7 @@ func (t *Txop) TExec(ctx context.Context, id string, param interface{}) (r sql.R return } -func (t *Txop) TExecRtn(ctx context.Context, id string, param interface{}, h ExtractFunc) error { +func (t *Txop) TExecRtn(ctx context.Context, id string, param interface{}, h RowsExtractor) error { return query(ctx, t, id, param, h) } diff --git a/util.go b/util.go index 60efc57..c3b07ea 100644 --- a/util.go +++ b/util.go @@ -29,7 +29,7 @@ type sqltExecer interface { Maker } -func query(ctx context.Context, ext sqltExecer, id string, data interface{}, h ExtractFunc) error { +func query(ctx context.Context, ext sqltExecer, id string, data interface{}, h RowsExtractor) error { param := dummy(data) sql := MustSql(ext, id, param) @@ -43,7 +43,7 @@ func query(ctx context.Context, ext sqltExecer, id string, data interface{}, h E return err } defer rows.Close() - return h(rows) + return h.Extract(rows) } func exec(ctx context.Context, ext sqltExecer, id string, data interface{}) (r sql.Result, e error) { @@ -60,17 +60,17 @@ func exec(ctx context.Context, ext sqltExecer, id string, data interface{}) (r s } type TExecer interface { - TQuery(context.Context, string, interface{}, ExtractFunc) error + TQuery(context.Context, string, interface{}, RowsExtractor) error TExec(context.Context, string, interface{}) (sql.Result, error) - TExecRtn(context.Context, string, interface{}, ExtractFunc) error + TExecRtn(context.Context, string, interface{}, RowsExtractor) error } -func Query(execer TExecer, ctx context.Context, id string, param interface{}, h ExtractFunc) (err error) { +func Query(execer TExecer, ctx context.Context, id string, param interface{}, h RowsExtractor) (err error) { err = execer.TQuery(ctx, id, param, h) return } -func MustQuery(execer TExecer, ctx context.Context, id string, param interface{}, h ExtractFunc) { +func MustQuery(execer TExecer, ctx context.Context, id string, param interface{}, h RowsExtractor) { if err := Query(execer, ctx, id, param, h); err != nil { panic(err) } @@ -89,12 +89,12 @@ func MustExec(execer TExecer, ctx context.Context, id string, param interface{}) return } -func ExecRtn(execer TExecer, ctx context.Context, id string, param interface{}, h ExtractFunc) (err error) { +func ExecRtn(execer TExecer, ctx context.Context, id string, param interface{}, h RowsExtractor) (err error) { err = execer.TExecRtn(ctx, id, param, h) return } -func MustExecRtn(execer TExecer, ctx context.Context, id string, param interface{}, h ExtractFunc) { +func MustExecRtn(execer TExecer, ctx context.Context, id string, param interface{}, h RowsExtractor) { if err := ExecRtn(execer, ctx, id, param, h); err != nil { panic(err) }