-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
119 lines (100 loc) · 2.68 KB
/
database.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package tyr
import (
"context"
"database/sql"
"fmt"
"strconv"
"github.com/opentracing/opentracing-go"
"github.com/go-sql-driver/mysql"
"github.com/lib/pq"
)
type Store interface {
Notify(ctx context.Context, event Event)
Subscriber(ctx context.Context, t EventType, fn EventFunc)
WithTransaction(ctx context.Context, fn func(ctx context.Context, tx *sql.Tx) error) error
}
type Driver interface {
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Exec(query string, args ...interface{}) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
QueryRow(query string, args ...interface{}) *sql.Row
}
const (
POSTGRES string = "postgres"
MYSQL string = "mysql"
)
type Sql struct {
*sql.DB
Event *EventHandler
}
func (s *Sql) WithTransaction(ctx context.Context, fn func(context.Context, *sql.Tx) error) error {
span, ctxSpan := opentracing.StartSpanFromContext(ctx, "tyr.WithTransaction")
defer span.Finish()
tx, err := s.DB.BeginTx(ctxSpan, nil)
if err != nil {
return err
}
if err := fn(ctxSpan, tx); err != nil {
if errRoll := tx.Rollback(); errRoll != nil {
return fmt.Errorf("tx err: %v, rb err: %v", err, errRoll)
}
return err
}
return tx.Commit()
}
func (s *Sql) Subscriber(ctx context.Context, t EventType, fn EventFunc) {
span, ctxSpan := opentracing.StartSpanFromContext(ctx, "tyr.Subscriber")
defer span.Finish()
s.Event.Handle(ctxSpan, t, fn)
}
func (s *Sql) Notify(ctx context.Context, event Event) {
span, ctxSpan := opentracing.StartSpanFromContext(ctx, "tyr.Notify")
defer span.Finish()
s.Event.Event = event
s.Event.Dispatcher(ctxSpan)
}
func (s *Sql) SetEvent(handler *EventHandler) {
s.Event = handler
}
type SqlConnParams struct {
Driver, Dsn string
}
func New(args SqlConnParams) (*Sql, error) {
db, err := sql.Open(args.Driver, args.Dsn)
if err != nil {
panic(fmt.Errorf("cannot access your db master connection").Error())
}
return &Sql{DB: db}, nil
}
type Error struct {
Code string
Message string
}
func (e *Error) Error() string {
return fmt.Sprintf("Error %s: %s", e.Code, e.Message)
}
func CatchErr(err error) *Error {
var e *Error
// specific database error
// postgre
if pqErr, ok := err.(*pq.Error); ok {
e = &Error{
Code: pqErr.Code.Name(),
Message: pqErr.Message,
}
}
// mysql
if myErr, ok := err.(*mysql.MySQLError); ok {
e = &Error{
Code: strconv.Itoa(int(myErr.Number)),
Message: myErr.Message,
}
}
// default
if er, ok := err.(*Error); ok {
e = er
}
return e
}