-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsql.go
207 lines (168 loc) · 6.9 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
package patcher
import (
"database/sql"
"errors"
"fmt"
"reflect"
"strings"
)
const (
DefaultDbTagName = "db"
DBTagPrimaryKey = "pk"
)
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")
)
// NewSQLPatch creates a new SQLPatch instance with the given resource and options.
// It initializes the SQLPatch with default settings and generates the SQL patch
// for the provided resource by processing its fields and applying the necessary tags and options.
func NewSQLPatch(resource any, opts ...PatchOpt) *SQLPatch {
sqlPatch := newPatchDefaults(opts...)
sqlPatch.patchGen(resource)
return sqlPatch
}
// patchGen generates the SQL patch for the given resource.
// It processes the fields of the struct, applying the necessary tags and options,
// and prepares the SQL update statement components (fields and arguments).
func (s *SQLPatch) patchGen(resource any) {
resource = dereferenceIfPointer(resource)
ensureStruct(resource)
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 := range n {
fType := rType.Field(i)
fVal := rVal.Field(i)
tag := getTag(&fType, s.tagName)
optsTag := fType.Tag.Get(TagOptsName)
if s.shouldSkipField(&fType, fVal) {
continue
}
var arg any = nil
if fVal.Kind() == reflect.Ptr && fVal.IsNil() {
if !s.shouldIncludeNil(optsTag) {
continue
}
} else {
arg = getValue(fVal)
}
s.fields = append(s.fields, tag+" = ?")
s.args = append(s.args, arg)
}
}
// GenerateSQL generates the SQL update statement and its arguments for the given resource.
// It creates a new SQLPatch instance with the provided options, processes the resource's fields,
// and constructs the SQL update statement along with the necessary arguments.
func GenerateSQL(resource any, opts ...PatchOpt) (sqlStr string, args []any, err error) {
return NewSQLPatch(resource, opts...).GenerateSQL()
}
// GenerateSQL constructs the SQL update statement and its arguments.
// It validates the SQL generation process, builds the SQL update statement
// with the table name, join clauses, set clauses, and where clauses,
// and returns the final SQL string along with the arguments.
func (s *SQLPatch) GenerateSQL() (sqlStr string, args []any, err error) {
if err := s.validateSQLGen(); err != nil {
return "", nil, fmt.Errorf("validate SQL generation: %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(")")
sqlArgs := s.joinArgs
sqlArgs = append(sqlArgs, s.args...)
sqlArgs = append(sqlArgs, s.whereArgs...)
return sqlBuilder.String(), sqlArgs, nil
}
// PerformPatch executes the SQL update statement for the given resource.
// It creates a new SQLPatch instance with the provided options, generates the SQL update statement,
// and executes it using the database connection.
func PerformPatch(resource any, opts ...PatchOpt) (sql.Result, error) {
return NewSQLPatch(resource, opts...).PerformPatch()
}
// PerformDiffPatch executes the SQL update statement for the differences between the old and new resources.
// It creates a new SQLPatch instance by comparing the old and new resources, generates the SQL update statement,
// and executes it using the database connection.
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()
}
// PerformPatch executes the SQL update statement for the current SQLPatch instance.
// It validates the SQL generation process, constructs the SQL update statement and its arguments,
// and executes the statement using the database connection.
// It returns the result of the SQL execution or an error if the process fails.
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...)
}
// NewDiffSQLPatch creates a new SQLPatch instance by comparing the old and new resources.
// It initializes the SQLPatch with default settings, loads the differences between the old and new resources,
// and prepares the SQL update statement components (fields and arguments) for the differences.
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 := range reflect.ValueOf(old).Elem().NumField() {
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, oldElem.Type().Field(i).Name)
continue
}
}
patch.patchGen(old)
return patch, nil
}