-
Notifications
You must be signed in to change notification settings - Fork 22
/
db.go
83 lines (71 loc) · 1.86 KB
/
db.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
package gosql
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"time"
)
//Debug env
var Debug = false
//ErrNoRows sql ErrNoRows
var ErrNoRows = sql.ErrNoRows
//Result sql Result
type Result sql.Result
//Executor is a Executor
type Executor interface {
PrepareContext(context.Context, string) (*sql.Stmt, error)
Prepare(string) (*sql.Stmt, error)
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
Exec(string, ...interface{}) (sql.Result, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
Query(string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
QueryRow(string, ...interface{}) *sql.Row
}
//DB ..
type DB interface {
SetMaxIdleConns(int)
SetMaxOpenConns(int)
SetConnMaxLifetime(time.Duration)
Stats() sql.DBStats
PingContext(context.Context) error
Ping() error
Close() error
BeginTx(context.Context, *sql.TxOptions) (*sql.Tx, error)
Begin() (*sql.Tx, error)
Driver() driver.Driver
Conn(context.Context) (*sql.Conn, error)
}
//Tx ..
type Tx interface {
StmtContext(context.Context, *sql.Stmt) *sql.Stmt
Stmt(*sql.Stmt) *sql.Stmt
Commit() error
Rollback() error
}
//Cluster ..
type Cluster interface {
// Executor(*Session, bool) (*Session, error)
Begin() (*Session, error)
Fetch(interface{}, ...Option) error
FetchAll(interface{}, ...Option) error
Update(interface{}, ...Option) (Result, error)
Insert(interface{}, ...Option) (Result, error)
Replace(interface{}, ...Option) (Result, error)
Delete(interface{}, ...Option) (Result, error)
}
func debugPrint(format string, vals ...interface{}) {
if Debug {
fmt.Printf(format+"\r\n", vals...)
}
}
// type Error mysql.MySQLError
//Error ..
type Error struct {
Number uint16
Message string
}
func (e *Error) Error() string {
return fmt.Sprintf("Error %d: %s", e.Number, e.Message)
}