Skip to content

Commit 761091d

Browse files
committed
Refactor code
1 parent 0a3750a commit 761091d

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

writer.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,14 @@ func Init(modelType reflect.Type, db *sql.DB) (map[string]int, *Schema, map[stri
5353
schema := CreateSchema(modelType)
5454
fields := BuildFieldsBySchema(schema)
5555
jsonColumnMap := MakeJsonColumnMap(modelType)
56+
jm := GetWritableColumns(schema.Fields, jsonColumnMap)
5657
keys, arr := FindPrimaryKeys(modelType)
5758
if db == nil {
58-
return fieldsIndex, schema, jsonColumnMap, keys, arr, fields, nil, "", nil
59+
return fieldsIndex, schema, jm, keys, arr, fields, nil, "", nil
5960
}
6061
driver := GetDriver(db)
6162
buildParam := GetBuild(db)
62-
return fieldsIndex, schema, jsonColumnMap, keys, arr, fields, buildParam, driver, nil
63+
return fieldsIndex, schema, jm, keys, arr, fields, buildParam, driver, nil
6364
}
6465
type Writer struct {
6566
*Loader
@@ -113,7 +114,8 @@ func NewSqlWriterWithVersion(db *sql.DB, tableName string, modelType reflect.Typ
113114
driver := GetDriver(db)
114115
boolSupport := driver == DriverPostgres
115116
schema := CreateSchema(modelType)
116-
jsonColumnMap := MakeJsonColumnMap(modelType)
117+
jsonColumnMapT := MakeJsonColumnMap(modelType)
118+
jsonColumnMap := GetWritableColumns(schema.Fields, jsonColumnMapT)
117119
if len(versionField) > 0 {
118120
index := FindFieldIndex(modelType, versionField)
119121
if index >= 0 {
@@ -439,6 +441,20 @@ func JSONToColumns(model map[string]interface{}, m map[string]string) map[string
439441
}
440442
return r
441443
}
444+
func GetWritableColumns(fields map[string]*FieldDB, jsonColumnMap map[string]string) map[string]string {
445+
m := jsonColumnMap
446+
for k, v := range jsonColumnMap {
447+
for _, db := range fields {
448+
if db.Column == v {
449+
if db.Update == false && db.Key == false {
450+
delete(m, k)
451+
}
452+
}
453+
}
454+
}
455+
return m
456+
}
457+
442458
func Contains(s []string, str string) bool {
443459
for _, v := range s {
444460
if v == str {

0 commit comments

Comments
 (0)