-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql.go
240 lines (193 loc) · 6.45 KB
/
sql.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
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"
"fmt"
"reflect"
"slices"
"strings"
)
const (
DefaultDbTagName = "db"
)
var (
// ErrNoChanges is returned when no changes are detected between the old and new objects
ErrNoChanges = errors.New("no changes detected between the old and new objects")
)
func NewSQLPatch(resource any, opts ...PatchOpt) *SQLPatch {
sqlPatch := newPatchDefaults(opts...)
sqlPatch.patchGen(resource)
return sqlPatch
}
func (s *SQLPatch) patchGen(resource any) {
// If the resource is a pointer, we need to dereference it to get the value
if reflect.TypeOf(resource).Kind() == reflect.Ptr {
resource = reflect.ValueOf(resource).Elem().Interface()
}
// Ensure that the resource is a struct
if reflect.TypeOf(resource).Kind() != reflect.Struct {
// This is intentionally a panic as this is a programming error and should be fixed by the developer
panic("resource is not a struct")
}
rType := reflect.TypeOf(resource)
rVal := reflect.ValueOf(resource)
n := rType.NumField()
s.fields = make([]string, 0, n)
s.args = make([]any, 0, n)
for i := 0; i < n; i++ {
fType := rType.Field(i)
fVal := rVal.Field(i)
tag := fType.Tag.Get(s.tagName)
// Skip unexported fields
if !fType.IsExported() {
continue
}
patcherOptsTag := fType.Tag.Get(TagOptsName)
// Skip fields that are to be ignored
if s.checkSkipField(fType) {
continue
} else if fVal.Kind() == reflect.Ptr && (fVal.IsNil() && !s.shouldIncludeNil(patcherOptsTag)) {
continue
} else if fVal.Kind() != reflect.Ptr && (fVal.IsZero() && !s.shouldIncludeZero(patcherOptsTag)) {
continue
}
if patcherOptsTag != "" {
patcherOpts := strings.Split(patcherOptsTag, TagOptSeparator)
if slices.Contains(patcherOpts, TagOptSkip) {
continue
}
}
// If no tag is set, use the field name
if tag == "" {
tag = fType.Name
}
addField := func() {
s.fields = append(s.fields, tag+" = ?")
}
if fVal.Kind() == reflect.Ptr && fVal.IsNil() && s.shouldIncludeNil(patcherOptsTag) {
s.args = append(s.args, nil)
addField()
continue
} else if fVal.Kind() == reflect.Ptr && fVal.IsNil() {
continue
}
addField()
var val reflect.Value
if fVal.Kind() == reflect.Ptr {
val = fVal.Elem()
} else {
val = fVal
}
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
s.args = append(s.args, val.Int())
case reflect.String:
s.args = append(s.args, val.String())
case reflect.Bool:
boolArg := 0
if val.Bool() {
boolArg = 1
}
s.args = append(s.args, boolArg)
case reflect.Float32, reflect.Float64:
s.args = append(s.args, val.Float())
default:
// This is intentionally a panic as this is a programming error and should be fixed by the developer
panic(fmt.Sprintf("unsupported type: %s", val.Kind()))
}
}
}
func GenerateSQL(resource any, opts ...PatchOpt) (string, []any, error) {
sqlPatch := NewSQLPatch(resource, opts...)
return sqlPatch.GenerateSQL()
}
func (s *SQLPatch) GenerateSQL() (string, []any, error) {
if err := s.validateSQLGen(); err != nil {
return "", nil, fmt.Errorf("validate perform patch: %w", err)
}
sqlBuilder := new(strings.Builder)
sqlBuilder.WriteString("UPDATE ")
sqlBuilder.WriteString(s.table)
sqlBuilder.WriteString("\n")
if s.joinSql.String() != "" {
sqlBuilder.WriteString(s.joinSql.String())
}
sqlBuilder.WriteString("SET ")
sqlBuilder.WriteString(strings.Join(s.fields, ", "))
sqlBuilder.WriteString("\n")
sqlBuilder.WriteString("WHERE (1=1)\n")
sqlBuilder.WriteString("AND (\n")
// If the where clause starts with "AND" or "OR", we need to remove it
where := s.whereSql.String()
if strings.HasPrefix(where, string(WhereTypeAnd)) || strings.HasPrefix(where, string(WhereTypeOr)) {
where = strings.TrimPrefix(where, string(WhereTypeAnd))
where = strings.TrimPrefix(where, string(WhereTypeOr))
where = strings.TrimSpace(where)
}
sqlBuilder.WriteString(strings.TrimSpace(where) + "\n")
sqlBuilder.WriteString(")")
args := append(s.joinArgs, s.args...)
args = append(args, s.whereArgs...)
return sqlBuilder.String(), args, nil
}
func PerformPatch(resource any, opts ...PatchOpt) (sql.Result, error) {
sqlPatch := NewSQLPatch(resource, opts...)
return sqlPatch.PerformPatch()
}
func PerformDiffPatch[T any](old, newT *T, opts ...PatchOpt) (sql.Result, error) {
sqlPatch, err := NewDiffSQLPatch(old, newT, opts...)
if err != nil {
return nil, fmt.Errorf("new diff sql patch: %w", err)
}
return sqlPatch.PerformPatch()
}
func (s *SQLPatch) PerformPatch() (sql.Result, error) {
if err := s.validatePerformPatch(); err != nil {
return nil, fmt.Errorf("validate perform patch: %w", err)
}
sqlStr, args, err := s.GenerateSQL()
if err != nil {
return nil, fmt.Errorf("generate SQL: %w", err)
}
return s.db.Exec(sqlStr, args...)
}
func NewDiffSQLPatch[T any](old, newT *T, opts ...PatchOpt) (*SQLPatch, error) {
if !isPointerToStruct(old) || !isPointerToStruct(newT) {
return nil, ErrInvalidType
}
// Take a copy of the old object
oldCopy := reflect.New(reflect.TypeOf(old).Elem()).Interface()
// copy the old object into the copy
reflect.ValueOf(oldCopy).Elem().Set(reflect.ValueOf(old).Elem())
patch := newPatchDefaults(opts...)
if err := patch.loadDiff(old, newT); err != nil {
return nil, fmt.Errorf("load diff: %w", err)
}
// Are the old and new objects the same?
if reflect.DeepEqual(old, oldCopy) {
return nil, ErrNoChanges
}
oldElem := reflect.ValueOf(old).Elem()
oldCopyElem := reflect.ValueOf(oldCopy).Elem()
// For each field in the old object, compare it against the copy and if the fields are the same, set them to zero or nil.
for i := 0; i < reflect.ValueOf(old).Elem().NumField(); i++ {
oldField := oldElem.Field(i)
copyField := oldCopyElem.Field(i)
patcherOptsTag := oldElem.Type().Field(i).Tag.Get(TagOptsName)
if oldField.Kind() == reflect.Ptr && (oldField.IsNil() && copyField.IsNil() && !patch.shouldIncludeNil(patcherOptsTag)) {
continue
} else if oldField.Kind() != reflect.Ptr && (oldField.IsZero() && copyField.IsZero() && !patch.shouldIncludeZero(patcherOptsTag)) {
continue
}
if reflect.DeepEqual(oldField.Interface(), copyField.Interface()) {
// Field is the same, set it to zero or nil. Add it to be ignored in the patch
if patch.ignoreFields == nil {
patch.ignoreFields = make([]string, 0)
}
patch.ignoreFields = append(patch.ignoreFields, strings.ToLower(oldElem.Type().Field(i).Name))
continue
}
}
patch.patchGen(old)
return patch, nil
}