-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
143 lines (133 loc) · 3.34 KB
/
client.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package CorePostgres
import (
"database/sql"
"errors"
"fmt"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"time"
)
type Client struct {
//DB 数据库对象
DB *sqlx.DB
//InstallDir 配置文件默认路径
InstallDir string
//最大连接数量
MaxConnect int
//连接超时时间秒
ConnectExpireSec int
}
// Init 初始化
// eg url: host=%s port=%d user=%s password=%s dbname=%s sslmode=disable
func (t *Client) Init(url string, installDir string, timeZone string) (err error) {
t.DB, err = sqlx.Connect("postgres", url)
if err != nil {
return
}
_, err = t.DB.Exec(fmt.Sprint("set time zone \"", timeZone, "\";"))
if err != nil {
err = errors.New("init exec sql, " + err.Error())
}
//设置超时时间
if t.ConnectExpireSec < 1 {
t.ConnectExpireSec = 10
}
t.DB.SetConnMaxLifetime(time.Duration(t.ConnectExpireSec) * time.Second)
if t.MaxConnect < 1 {
t.MaxConnect = 0
}
t.DB.SetMaxOpenConns(t.MaxConnect)
//设置安装包目录
if installDir != "" {
t.InstallDir = installDir
}
//反馈
return
}
// Get using this DB.
// Any placeholder parameters are replaced with supplied args.
// An error is returned if the result set is empty.
func (t *Client) Get(dest interface{}, query string, args ...interface{}) (err error) {
//捕捉异常
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprint(r))
return
}
}()
return t.DB.Get(dest, query, args...)
}
// Select using this DB.
// Any placeholder parameters are replaced with supplied args.
func (t *Client) Select(dest interface{}, query string, args ...interface{}) (err error) {
//捕捉异常
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprint(r))
return
}
}()
return t.DB.Select(dest, query, args...)
}
// MustBegin starts a transaction, and panics on error. Returns an *sqlx.Tx instead
// of an *sql.Tx.
func (t *Client) MustBegin() *sqlx.Tx {
tx, err := t.DB.Beginx()
if err != nil {
panic(err)
}
return tx
}
// Exec executes a query without returning any rows.
// The args are for any placeholder parameters in the query.
//
// Exec uses context.Background internally; to specify the context, use
// ExecContext.
func (t *Client) Exec(query string, args ...any) (s sql.Result, err error) {
//捕捉异常
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprint(r))
return
}
}()
return t.DB.Exec(query, args...)
}
// PrepareNamed returns an sqlx.NamedStmt
func (t *Client) PrepareNamed(query string) (s *sqlx.NamedStmt, err error) {
//捕捉异常
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprint(r))
return
}
}()
return t.DB.PrepareNamed(query)
}
// NamedExec using this DB.
// Any named placeholder parameters are replaced with fields from arg.
func (t *Client) NamedExec(query string, arg interface{}) (s sql.Result, err error) {
//捕捉异常
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprint(r))
return
}
}()
return t.DB.NamedExec(query, arg)
}
// Ping verifies a connection to the database is still alive,
// establishing a connection if necessary.
//
// Ping uses context.Background internally; to specify the context, use
// PingContext.
func (t *Client) Ping() (err error) {
//捕捉异常
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprint(r))
return
}
}()
return t.DB.Ping()
}