-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpatch.go
More file actions
240 lines (194 loc) · 6.05 KB
/
patch.go
File metadata and controls
240 lines (194 loc) · 6.05 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package patcher
import (
"database/sql"
"errors"
"reflect"
"slices"
"strings"
)
// SQLDialect represents the SQL dialect to use for parameter placeholders
type SQLDialect int
const (
// DialectMySQL uses ? for parameter placeholders (default)
DialectMySQL SQLDialect = iota
// DialectSQLite uses ? for parameter placeholders (same as MySQL)
DialectSQLite
// DialectPostgreSQL uses $1, $2, $3 for parameter placeholders
DialectPostgreSQL
)
var (
// ErrNoDatabaseConnection is returned when no database connection is set
ErrNoDatabaseConnection = errors.New("no database connection set")
// ErrNoTable is returned when no table is set
ErrNoTable = errors.New("no table set")
// ErrNoFields is returned when no fields are set
ErrNoFields = errors.New("no fields set")
// ErrNoArgs is returned when no arguments are set
ErrNoArgs = errors.New("no arguments set")
// ErrNoWhere is returned when no where clause is set
ErrNoWhere = errors.New("no where clause set")
)
type IgnoreFieldsFunc func(field *reflect.StructField) bool
type SQLPatch struct {
// fields is the fields to update in the SQL statement
fields []string
// args is the arguments to use in the SQL statement
args []any
// db is the database connection to use
db *sql.DB
// tagName is the tag name to look for in the struct. This is an override from the default tag "db"
tagName string
// table is the table name to use in the SQL statement
table string
// whereSql is the where clause to use in the SQL statement
whereSql *strings.Builder
// whereArgs is the arguments to use in the where clause
whereArgs []any
// joinSql is the join clause to use in the SQL statement
joinSql *strings.Builder
// joinArgs is the arguments to use in the join clause
joinArgs []any
// includeZeroValues determines whether zero values should be included in the patch
includeZeroValues bool
// includeNilValues determines whether nil values should be included in the patch
includeNilValues bool
// ignoreFields is a list of fields to ignore when patching
ignoreFields []string
// ignoreFieldsFunc is a function that determines whether a field should be ignored
//
// This func should return true is the field is to be ignored
ignoreFieldsFunc IgnoreFieldsFunc
// dialect is the SQL dialect to use for parameter placeholders
dialect SQLDialect
}
// newPatchDefaults creates a new SQLPatch with default options.
func newPatchDefaults(opts ...PatchOpt) *SQLPatch {
// Default options
p := &SQLPatch{
fields: make([]string, 0),
args: make([]any, 0),
db: nil,
tagName: DefaultDbTagName,
table: "",
whereSql: new(strings.Builder),
whereArgs: nil,
joinSql: new(strings.Builder),
joinArgs: nil,
includeZeroValues: false,
includeNilValues: false,
ignoreFields: nil,
ignoreFieldsFunc: nil,
}
for _, opt := range opts {
opt(p)
}
return p
}
// Fields returns the fields to update in the SQL statement
func (s *SQLPatch) Fields() []string {
if len(s.fields) == 0 {
// Default behaviour is to return nil if there are no fields
return nil
}
return s.fields
}
// Args returns the arguments to use in the SQL statement
func (s *SQLPatch) Args() []any {
if len(s.args) == 0 {
// Default behaviour is to return nil if there are no args
return nil
}
return s.args
}
// validatePerformPatch validates the SQLPatch for the PerformPatch method
func (s *SQLPatch) validatePerformPatch() error {
switch {
case s.db == nil:
return ErrNoDatabaseConnection
case s.table == "":
return ErrNoTable
case len(s.fields) == 0:
return ErrNoFields
case len(s.args) == 0:
return ErrNoArgs
case s.whereSql.String() == "":
return ErrNoWhere
default:
return nil
}
}
// validateSQLGen validates the SQLPatch for the SQLGen method
func (s *SQLPatch) validateSQLGen() error {
switch {
case s.table == "":
return ErrNoTable
case len(s.fields) == 0:
return ErrNoFields
case len(s.args) == 0:
return ErrNoArgs
case s.whereSql.String() == "":
return ErrNoWhere
default:
return nil
}
}
// shouldIncludeNil determines whether the field should be included in the patch
func (s *SQLPatch) shouldIncludeNil(tag string) bool {
if s.includeNilValues {
return true
}
return s.shouldOmitEmpty(tag)
}
// shouldIncludeZero determines whether zero values should be included in the patch
func (s *SQLPatch) shouldIncludeZero(tag string) bool {
if s.includeZeroValues {
return true
}
return s.shouldOmitEmpty(tag)
}
// shouldOmitEmpty determines whether the field should be omitted if it is empty
func (s *SQLPatch) shouldOmitEmpty(tag string) bool {
if tag != "" {
tags := strings.Split(tag, TagOptSeparator)
if slices.Contains(tags, TagOptOmitempty) {
return true
}
}
return false
}
func (s *SQLPatch) shouldSkipField(fType *reflect.StructField, fVal reflect.Value) bool {
if !fType.IsExported() || !IsValidType(fVal) || s.checkSkipField(fType) {
return true
}
patcherOptsTag := fType.Tag.Get(TagOptsName)
if fVal.Kind() == reflect.Ptr && (fVal.IsNil() && !s.shouldIncludeNil(patcherOptsTag)) {
return true
} else if fVal.Kind() != reflect.Ptr && (fVal.IsZero() && !s.shouldIncludeZero(patcherOptsTag)) {
return true
}
return false
}
func (s *SQLPatch) checkSkipField(field *reflect.StructField) bool {
// The ignore fields tag takes precedence over the ignore fields list
if s.checkSkipTag(field) {
return true
}
return s.ignoredFieldsCheck(field)
}
func (s *SQLPatch) checkSkipTag(field *reflect.StructField) bool {
val, ok := field.Tag.Lookup(TagOptsName)
if !ok {
return false
}
tags := strings.Split(val, TagOptSeparator)
return slices.Contains(tags, TagOptSkip)
}
func (s *SQLPatch) ignoredFieldsCheck(field *reflect.StructField) bool {
return s.checkIgnoredFields(field.Name) || s.checkIgnoreFunc(field)
}
func (s *SQLPatch) checkIgnoreFunc(field *reflect.StructField) bool {
return s.ignoreFieldsFunc != nil && s.ignoreFieldsFunc(field)
}
func (s *SQLPatch) checkIgnoredFields(field string) bool {
return len(s.ignoreFields) > 0 && slices.Contains(s.ignoreFields, field)
}