-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpmx.go
203 lines (169 loc) · 3.73 KB
/
pmx.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
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
package pmx
import (
"bytes"
"context"
"errors"
"fmt"
"reflect"
"slices"
"strings"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
var (
ErrInvalidRef = errors.New("invalid ref")
ErrNoRows = pgx.ErrNoRows
)
type Executor interface {
Exec(context.Context, string, ...any) (pgconn.CommandTag, error)
Query(context.Context, string, ...any) (pgx.Rows, error)
}
func Insert(ctx context.Context, e Executor, entity any) (pgconn.CommandTag, error) {
t := reflect.TypeOf(entity)
v := reflect.ValueOf(entity)
if t.Kind() != reflect.Ptr {
return pgconn.CommandTag{}, ErrInvalidRef
}
t = t.Elem()
v = v.Elem()
if t.Kind() != reflect.Struct {
return pgconn.CommandTag{}, ErrInvalidRef
}
buf := bytes.NewBufferString(fmt.Sprintf(
"insert into %s ",
t.Field(0).Tag.Get("table"),
))
columns := []string{}
values := []string{}
args := []any{}
for i := 0; i < t.NumField(); i++ {
tag := t.Field(i).Tag
column := tag.Get("db")
if len(column) == 0 {
continue
}
if !v.Field(i).CanInterface() {
continue
}
columns = append(columns, column)
if tag.Get("default") == "true" {
values = append(values, "default")
continue
}
args = append(args, v.Field(i).Interface())
values = append(values, fmt.Sprintf("$%d", len(args)))
}
buf.WriteString(fmt.Sprintf(
"(%s) values (%s)",
strings.Join(columns, ", "),
strings.Join(values, ", "),
))
if slices.Contains(values, "default") {
buf.WriteString(" returning *")
rows, err := e.Query(ctx, buf.String(), args...)
if err != nil {
return pgconn.CommandTag{}, err
}
defer rows.Close()
err = scan(rows, entity)
if err != nil {
return pgconn.CommandTag{}, err
}
rows.Close()
return rows.CommandTag(), nil
}
return e.Exec(ctx, buf.String(), args...)
}
func Select(ctx context.Context, e Executor, dest any, sql string, args ...any) error {
rows, err := e.Query(ctx, sql, args...)
if err != nil {
return err
}
defer rows.Close()
return scan(rows, dest)
}
func UniqueViolation(err error) bool {
var pgErr *pgconn.PgError
ok := errors.As(err, &pgErr)
return ok && pgErr.Code == pgerrcode.UniqueViolation
}
func scan(rows pgx.Rows, dest any) error {
t := reflect.TypeOf(dest)
if t.Kind() != reflect.Ptr {
return ErrInvalidRef
}
t = t.Elem()
v := reflect.ValueOf(dest)
switch t.Kind() {
case reflect.Slice:
return scanSlice(rows, t, v)
case reflect.Struct:
return scanStruct(rows, t, v)
default:
return ErrInvalidRef
}
}
func scanSlice(rows pgx.Rows, t reflect.Type, v reflect.Value) error {
t = t.Elem()
if t.Kind() != reflect.Ptr {
return ErrInvalidRef
}
t = t.Elem()
if t.Kind() != reflect.Struct {
return ErrInvalidRef
}
for rows.Next() {
ptr, err := scanFields(rows, t)
if err != nil {
return err
}
sv := v.Elem()
sv.Set(reflect.Append(sv, ptr))
}
err := rows.Err()
if err != nil {
return err
}
return nil
}
func scanStruct(rows pgx.Rows, t reflect.Type, v reflect.Value) error {
if !rows.Next() {
err := rows.Err()
if err != nil {
return err
}
return pgx.ErrNoRows
}
ptr, err := scanFields(rows, t)
if err != nil {
return err
}
v.Elem().Set(ptr.Elem())
return nil
}
func scanFields(rows pgx.Rows, t reflect.Type) (reflect.Value, error) {
fields := []any{}
ptr := reflect.New(t)
v := ptr.Elem()
for _, fd := range rows.FieldDescriptions() {
var field any
for i := 0; i < t.NumField(); i++ {
if t.Field(i).Tag.Get("db") != fd.Name {
continue
}
field = v.Field(i).Addr().Interface()
}
fields = append(fields, field)
}
for i := range fields {
if len(rows.RawValues()[i]) == 0 {
fields[i] = new(any)
}
}
err := rows.Scan(fields...)
if err != nil {
return reflect.ValueOf(nil), err
}
return ptr, nil
}