-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
95 lines (80 loc) · 1.67 KB
/
utils.go
File metadata and controls
95 lines (80 loc) · 1.67 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
package pg
import (
"database/sql/driver"
"reflect"
"strconv"
"strings"
"github.com/jackc/pgx/v5/pgtype"
)
func fieldName(fld reflect.StructField) string {
if col, ok := fld.Tag.Lookup("db"); ok && col != "primary" {
return strings.Split(col, ",")[0]
}
if col, ok := fld.Tag.Lookup("json"); ok {
return strings.Split(col, ",")[0]
}
return fld.Name
}
func skipField(i any) bool {
switch v := i.(type) {
case Nullable:
if v.IsNullable() {
return false
}
case IsZeroer:
if v.IsZero() {
return true
}
case pgtype.Text:
if !v.Valid {
return true
}
case pgtype.Array[pgtype.Text]:
if !v.Valid {
return true
}
case pgtype.Timestamptz:
if !v.Valid {
return true
}
case driver.Valuer:
if val, err := v.Value(); val == nil || err != nil {
return true
}
}
return false
}
func writeInt[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64](b ByteStringWriter, v T) {
b.Write(strconv.AppendInt([]byte{}, int64(v), 10))
}
func writeParam(b ByteStringWriter, args *[]any, value any) {
if col, ok := value.(Columnar); ok {
col.encodeColumnIdentifier(b)
} else {
*args = append(*args, value)
b.WriteByte('$')
writeInt(b, len(*args))
}
}
func writeIdentifier(b ByteStringWriter, identifiers ...string) {
if len(identifiers) == 0 {
return
}
first := true
for _, id := range identifiers {
if first {
first = false
} else {
b.WriteByte('.')
}
b.WriteByte('"')
b.WriteString(id)
b.WriteByte('"')
}
}
var stringReplacer = strings.NewReplacer("'", "", "\\", "")
func writeString(b ByteStringWriter, str string) {
b.WriteByte('\'')
stringReplacer.WriteString(b, str)
b.WriteByte('\'')
}