-
Notifications
You must be signed in to change notification settings - Fork 4
/
gorp_api.go
86 lines (72 loc) · 2.87 KB
/
gorp_api.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
package gorb
import (
"database/sql"
"fmt"
"github.com/go-gorp/gorp"
)
// Get https://godoc.org/gopkg.in/gorp.v2#DbMap.Get
func (b *Balancer) Get(i interface{}, keys ...interface{}) (interface{}, error) {
return b.Replica().Get(i, keys...)
}
// Select https://godoc.org/gopkg.in/gorp.v2#DbMap.Select
func (b *Balancer) Select(i interface{}, query string, args ...interface{}) ([]interface{}, error) {
return b.Replica().Select(i, query, args...)
}
// SelectFloat https://godoc.org/gopkg.in/gorp.v2#DbMap.SelectFloat
func (b *Balancer) SelectFloat(query string, args ...interface{}) (float64, error) {
return b.Replica().SelectFloat(query, args...)
}
// SelectInt https://godoc.org/gopkg.in/gorp.v2#DbMap.SelectInt
func (b *Balancer) SelectInt(query string, args ...interface{}) (int64, error) {
return b.Replica().SelectInt(query, args...)
}
// SelectNullFloat https://godoc.org/gopkg.in/gorp.v2#DbMap.SelectNullFloat
func (b *Balancer) SelectNullFloat(query string, args ...interface{}) (sql.NullFloat64, error) {
return b.Replica().SelectNullFloat(query, args...)
}
// SelectNullInt https://godoc.org/gopkg.in/gorp.v2#DbMap.SelectNullInt
func (b *Balancer) SelectNullInt(query string, args ...interface{}) (sql.NullInt64, error) {
return b.Replica().SelectNullInt(query, args...)
}
// SelectNullStr https://godoc.org/gopkg.in/gorp.v2#DbMap.SelectNullStr
func (b *Balancer) SelectNullStr(query string, args ...interface{}) (sql.NullString, error) {
return b.Replica().SelectNullStr(query, args...)
}
// SelectOne https://godoc.org/gopkg.in/gorp.v2#DbMap.SelectOne
func (b *Balancer) SelectOne(holder interface{}, query string, args ...interface{}) error {
return b.Replica().SelectOne(holder, query, args...)
}
// SelectStr https://godoc.org/gopkg.in/gorp.v2#DbMap.SelectStr
func (b *Balancer) SelectStr(query string, args ...interface{}) (string, error) {
return b.Replica().SelectStr(query, args...)
}
// Prepare creates a prepared statement for later queries or executions on each physical database.
// Multiple queries or executions may be run concurrently from the returned statement.
// This is equivalent to running: Prepare() using database/sql
//
// https://godoc.org/gopkg.in/gorp.v2#DbMap.Prepare
func (b *Balancer) Prepare(query string) (Stmt, error) {
dbs := b.GetAllDbs()
stmts := make([]*sql.Stmt, len(dbs))
for i := range stmts {
s, err := dbs[i].Prepare(query)
if err != nil {
return nil, err
}
stmts[i] = s
}
return &stmt{bl: b, stmts: stmts}, nil
}
// TraceOn https://godoc.org/gopkg.in/gorp.v2#DbMap.TraceOn
func (b *Balancer) TraceOn(prefix string, logger gorp.GorpLogger) {
for _, s := range b.replicas {
s.TraceOn(fmt.Sprintf("%s <slave>", prefix), logger)
}
b.DbMap.TraceOn(fmt.Sprintf("%s <master>", prefix), logger)
}
// TraceOff https://godoc.org/gopkg.in/gorp.v2#DbMap.TraceOff
func (b *Balancer) TraceOff() {
for _, db := range b.GetAllDbs() {
db.TraceOff()
}
}