-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.go
40 lines (32 loc) · 1004 Bytes
/
executor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package orm
import (
"context"
"database/sql"
)
var _ ExecutorIfc = (*sql.DB)(nil)
var _ ExecutorIfc = (*sql.Tx)(nil)
var _ TransactionIfc = (*sql.Tx)(nil)
type ExecutorIfc interface {
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}
type TransactionIfc interface {
Rollback() error
Commit() error
}
type DefaultExecutor struct {
*sql.DB
}
func NewDefaultExecutor(db *sql.DB) *DefaultExecutor {
return &DefaultExecutor{
DB: db,
}
}
func (e *DefaultExecutor) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
Log.Printf("exec: %s, args: %v", query, args)
return e.DB.ExecContext(ctx, query, args...)
}
func (e *DefaultExecutor) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
Log.Printf("query: %s, args: %v", query, args)
return e.DB.QueryContext(ctx, query, args...)
}