-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgdb.go
More file actions
212 lines (189 loc) · 5.22 KB
/
Copy pathpgdb.go
File metadata and controls
212 lines (189 loc) · 5.22 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package gosql
import (
"context"
"fmt"
"reflect"
"strings"
"sync"
"github.com/jackc/pgx/v4/pgxpool"
)
type PGConn struct {
*pgxpool.Pool
conf string
Ctx context.Context
sc *Sqlconfig
mu *sync.RWMutex
debug bool
}
func (d *PGConn) InsertInterfaceWithID(dest interface{}, cmd string, args ...interface{}) Result {
// $key 和 $value 固定位置固定值
// db.InsertInterfaceWithID(&value, "insert into test($key) values($value)")
res := Result{
LastInsertIds: make([]int64, 0),
}
typ := reflect.TypeOf(dest)
value := reflect.ValueOf(dest)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
value = value.Elem()
}
if typ.Kind() == reflect.Struct {
return d.insertInterface(dest, cmd, args...)
}
if typ.Kind() == reflect.Slice {
// 如果是切片, 那么每个值都做一次处理
length := value.Len()
if length == 1 {
return d.insertInterface(dest, cmd, args...)
}
for i := 0; i < length; i++ {
result := d.insertInterface(value.Index(i).Interface(), cmd, args...)
res.Sql += ";" + result.Sql
if result.Err != nil {
return result
}
res.LastInsertIds = append(res.LastInsertIds, result.LastInsertId)
}
} else {
res.Err = ErrNotSupport
}
return res
}
func (d *PGConn) insertInterface(dest interface{}, cmd string, args ...interface{}) Result {
// 插入到args之前 dest 是struct或切片的指针
newcmd, newargs, err := insertInterfaceSql(dest, cmd, args...)
if err != nil {
return Result{Err: err}
}
return d.Insert(newcmd, newargs...)
}
func (d *PGConn) insertWithoutInterface(dest interface{}, cmd string, args ...interface{}) Result {
// 插入到args之前 dest 是struct或切片的指针
newcmd, newargs, err := insertInterfaceSql(dest, cmd, args...)
if err != nil {
return Result{Err: err}
}
return d.Insert(newcmd, newargs...)
}
func (d *PGConn) Insert(cmd string, args ...interface{}) Result {
res := Result{
Sql: ToSql(cmd, args...),
}
for i := 1; i <= len(args); i++ {
cmd = strings.Replace(cmd, "?", fmt.Sprintf("$%d", i), 1)
}
result := d.QueryRow(d.Ctx, cmd, args...)
err := result.Scan(&res.LastInsertId)
if err != nil {
res.Err = err
return res
}
return res
}
func (d *PGConn) InsertWithoutId(cmd string, args ...interface{}) Result {
res := Result{
Sql: ToSql(cmd, args...),
}
for i := 1; i <= len(args); i++ {
cmd = strings.Replace(cmd, "?", fmt.Sprintf("$%d", i), 1)
}
result, err := d.Exec(d.Ctx, cmd, args...)
res.RowsAffected = result.RowsAffected()
res.Err = err
return res
}
func (d *PGConn) UpdateInterface(dest interface{}, cmd string, args ...interface{}) Result {
newcmd, newargs, err := updateInterfaceSql(dest, cmd, args...)
if err != nil {
return Result{Err: err}
}
return d.Update(newcmd, newargs...)
}
func (d *PGConn) Update(cmd string, args ...interface{}) Result {
res := Result{}
if d.debug {
res.Sql = ToPGSql(cmd, args...)
}
for i := 1; i <= len(args); i++ {
cmd = strings.Replace(cmd, "?", fmt.Sprintf("$%d", i), 1)
}
tags, err := d.Exec(d.Ctx, cmd, args...)
if err != nil {
res.Err = err
return res
}
res.RowsAffected, res.Err = tags.RowsAffected(), err
return res
}
func (d *PGConn) InsertInterfaceWithoutID(dest interface{}, cmd string, args ...interface{}) Result {
// $key 和 $value 固定位置固定值
// ID 自增的必须设置 default
// db.InsertInterfaceWithoutID(&value, "insert into test($key) values($value)")
res := Result{}
typ := reflect.TypeOf(dest)
value := reflect.ValueOf(dest)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
value = value.Elem()
}
if typ.Kind() == reflect.Struct {
return d.insertWithoutInterface(dest, cmd, args...)
}
if typ.Kind() == reflect.Slice {
// 如果是切片, 那么每个值都做一次处理
length := value.Len()
if length == 1 {
return d.insertWithoutInterface(dest, cmd, args...)
}
arguments := make([]interface{}, 0)
for i := 0; i < length; i++ {
newcmd, newargs, err := insertInterfaceSql(value.Index(i).Interface(), cmd, args...)
if err != nil {
res.Err = err
return res
}
cmd = newcmd
arguments = append(arguments, newargs...)
}
return d.InsertMany(cmd, arguments...)
} else {
res.Err = ErrNotSupport
}
return res
}
func (d *PGConn) InsertMany(cmd string, args ...interface{}) Result {
// sql: insert into test(id, name) values(?,?) args: interface{}... 1,'t1', 2, 't2', 3, 't3'
// 每次返回的是第一次插入的id
if args == nil {
return d.InsertWithoutId(cmd)
}
newcmd, err := formatSql(cmd, args...)
if err != nil {
return Result{Err: err}
}
return d.InsertWithoutId(newcmd, args...)
}
func (d *PGConn) Select(dest interface{}, cmd string, args ...interface{}) Result {
// db.Select(&value, "select * from test")
// 传入切片的地址, 根据tag 的 db 自动补充,
// 最求性能建议还是使用 GetRows or GetOne
for i := 1; i <= len(args); i++ {
cmd = strings.Replace(cmd, "?", fmt.Sprintf("$%d", i), 1)
}
res := Result{}
if d.debug {
res.Sql = ToPGSql(cmd, args...)
}
rows, err := d.Query(d.Ctx, cmd, args...)
if err != nil {
res.Err = err
return res
}
defer rows.Close()
// 需要设置的值
res.Err = pgfill(dest, rows)
return res
}
func (d *PGConn) Delete(cmd string, args ...interface{}) Result {
return d.Update(cmd, args...)
}