-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpatch_opts.go
115 lines (102 loc) · 2.8 KB
/
patch_opts.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
package patcher
import (
"database/sql"
"strings"
)
const (
TagOptsName = "patcher"
TagOptSeparator = ","
TagOptSkip = "-"
TagOptOmitempty = "omitempty"
)
type PatchOpt func(*SQLPatch)
// WithTagName sets the tag name to look for in the struct. This is an override from the default tag "db"
func WithTagName(tagName string) PatchOpt {
return func(s *SQLPatch) {
s.tagName = tagName
}
}
// WithTable sets the table name to use in the SQL statement
func WithTable(table string) PatchOpt {
return func(s *SQLPatch) {
s.table = table
}
}
// WithWhere sets the where clause to use in the SQL statement
func WithWhere(where Wherer) PatchOpt {
return func(s *SQLPatch) {
if s.whereSql == nil {
s.whereSql = new(strings.Builder)
}
fwSQL, fwArgs := where.Where()
if fwArgs == nil {
fwArgs = make([]any, 0)
}
wtStr := WhereTypeAnd // default to AND
wt, ok := where.(WhereTyper)
if ok && wt.WhereType().IsValid() {
wtStr = wt.WhereType()
}
s.whereSql.WriteString(string(wtStr) + " ")
s.whereSql.WriteString(strings.TrimSpace(fwSQL))
s.whereSql.WriteString("\n")
s.whereArgs = append(s.whereArgs, fwArgs...)
}
}
// WithJoin sets the join clause to use in the SQL statement
func WithJoin(join Joiner) PatchOpt {
return func(s *SQLPatch) {
if s.joinSql == nil {
s.joinSql = new(strings.Builder)
}
fjSQL, fjArgs := join.Join()
if fjArgs == nil {
fjArgs = make([]any, 0)
}
s.joinSql.WriteString(strings.TrimSpace(fjSQL))
s.joinSql.WriteString("\n")
s.joinArgs = append(s.joinArgs, fjArgs...)
}
}
// WithDB sets the database connection to use
func WithDB(db *sql.DB) PatchOpt {
return func(s *SQLPatch) {
s.db = db
}
}
// WithIncludeZeroValues sets whether zero values should be included in the patch.
//
// This is useful when you want to set a field to zero.
func WithIncludeZeroValues() PatchOpt {
return func(s *SQLPatch) {
s.includeZeroValues = true
}
}
// WithIncludeNilValues sets whether nil values should be included in the patch.
//
// This is useful when you want to set a field to nil.
func WithIncludeNilValues() PatchOpt {
return func(s *SQLPatch) {
s.includeNilValues = true
}
}
// WithIgnoredFields sets the fields to ignore when patching.
//
// This should be the actual field name, not the JSON tag name or the db tag name.
//
// Note. When we parse the slice of strings, we convert them to lowercase to ensure that the comparison is
// case-insensitive.
func WithIgnoredFields(fields ...string) PatchOpt {
return func(s *SQLPatch) {
for i := range fields {
fields[i] = strings.ToLower(fields[i])
}
s.ignoreFields = fields
}
}
// WithIgnoredFieldsFunc sets a function that determines whether a field should be ignored when patching.
func WithIgnoredFieldsFunc(f IgnoreFieldsFunc) PatchOpt {
return func(s *SQLPatch) {
s.ignoreFieldsFunc = f
}
}