Skip to content

Commit

Permalink
change interface
Browse files Browse the repository at this point in the history
  • Loading branch information
it512 committed Jan 21, 2019
1 parent ed0b3cd commit 0263da3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
16 changes: 12 additions & 4 deletions sqlt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down
16 changes: 8 additions & 8 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 0263da3

Please sign in to comment.