From 8333844f7112192ebd203992a67adf01b51ee8a0 Mon Sep 17 00:00:00 2001 From: ZhangShenao <15201440436@163.com> Date: Thu, 31 Mar 2022 20:57:20 +0800 Subject: [PATCH 1/5] fix variable shadowing (#5212) Co-authored-by: Shenao Zhang --- gorm.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gorm.go b/gorm.go index aca7cb5e..6a6bb032 100644 --- a/gorm.go +++ b/gorm.go @@ -124,8 +124,8 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) { for _, opt := range opts { if opt != nil { - if err := opt.Apply(config); err != nil { - return nil, err + if applyErr := opt.Apply(config); applyErr != nil { + return nil, applyErr } defer func(opt Option) { if errr := opt.AfterInitialize(db); errr != nil { From cd0315334b0fe555500d6f1870c566093d7daa33 Mon Sep 17 00:00:00 2001 From: Goxiaoy Date: Fri, 1 Apr 2022 08:33:39 +0800 Subject: [PATCH 2/5] fix: context missing in association (#5214) --- association.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/association.go b/association.go index dc731ff8..35e10ddd 100644 --- a/association.go +++ b/association.go @@ -502,7 +502,7 @@ func (association *Association) buildCondition() *DB { if association.Relationship.JoinTable != nil { if !tx.Statement.Unscoped && len(association.Relationship.JoinTable.QueryClauses) > 0 { - joinStmt := Statement{DB: tx, Schema: association.Relationship.JoinTable, Table: association.Relationship.JoinTable.Table, Clauses: map[string]clause.Clause{}} + joinStmt := Statement{DB: tx, Context: tx.Statement.Context, Schema: association.Relationship.JoinTable, Table: association.Relationship.JoinTable.Table, Clauses: map[string]clause.Clause{}} for _, queryClause := range association.Relationship.JoinTable.QueryClauses { joinStmt.AddClause(queryClause) } From f7b52bb649ba803ec149a06fec9e9da7b311d36e Mon Sep 17 00:00:00 2001 From: ZhangShenao <15201440436@163.com> Date: Fri, 1 Apr 2022 08:35:16 +0800 Subject: [PATCH 3/5] unify db receiver name (#5215) Co-authored-by: Shenao Zhang --- finisher_api.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/finisher_api.go b/finisher_api.go index b4d29b71..aa8e2b5a 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -207,7 +207,7 @@ func (db *DB) FindInBatches(dest interface{}, batchSize int, fc func(tx *DB, bat return tx } -func (tx *DB) assignInterfacesToValue(values ...interface{}) { +func (db *DB) assignInterfacesToValue(values ...interface{}) { for _, value := range values { switch v := value.(type) { case []clause.Expression: @@ -215,40 +215,40 @@ func (tx *DB) assignInterfacesToValue(values ...interface{}) { if eq, ok := expr.(clause.Eq); ok { switch column := eq.Column.(type) { case string: - if field := tx.Statement.Schema.LookUpField(column); field != nil { - tx.AddError(field.Set(tx.Statement.Context, tx.Statement.ReflectValue, eq.Value)) + if field := db.Statement.Schema.LookUpField(column); field != nil { + db.AddError(field.Set(db.Statement.Context, db.Statement.ReflectValue, eq.Value)) } case clause.Column: - if field := tx.Statement.Schema.LookUpField(column.Name); field != nil { - tx.AddError(field.Set(tx.Statement.Context, tx.Statement.ReflectValue, eq.Value)) + if field := db.Statement.Schema.LookUpField(column.Name); field != nil { + db.AddError(field.Set(db.Statement.Context, db.Statement.ReflectValue, eq.Value)) } } } else if andCond, ok := expr.(clause.AndConditions); ok { - tx.assignInterfacesToValue(andCond.Exprs) + db.assignInterfacesToValue(andCond.Exprs) } } case clause.Expression, map[string]string, map[interface{}]interface{}, map[string]interface{}: - if exprs := tx.Statement.BuildCondition(value); len(exprs) > 0 { - tx.assignInterfacesToValue(exprs) + if exprs := db.Statement.BuildCondition(value); len(exprs) > 0 { + db.assignInterfacesToValue(exprs) } default: - if s, err := schema.Parse(value, tx.cacheStore, tx.NamingStrategy); err == nil { + if s, err := schema.Parse(value, db.cacheStore, db.NamingStrategy); err == nil { reflectValue := reflect.Indirect(reflect.ValueOf(value)) switch reflectValue.Kind() { case reflect.Struct: for _, f := range s.Fields { if f.Readable { - if v, isZero := f.ValueOf(tx.Statement.Context, reflectValue); !isZero { - if field := tx.Statement.Schema.LookUpField(f.Name); field != nil { - tx.AddError(field.Set(tx.Statement.Context, tx.Statement.ReflectValue, v)) + if v, isZero := f.ValueOf(db.Statement.Context, reflectValue); !isZero { + if field := db.Statement.Schema.LookUpField(f.Name); field != nil { + db.AddError(field.Set(db.Statement.Context, db.Statement.ReflectValue, v)) } } } } } } else if len(values) > 0 { - if exprs := tx.Statement.BuildCondition(values[0], values[1:]...); len(exprs) > 0 { - tx.assignInterfacesToValue(exprs) + if exprs := db.Statement.BuildCondition(values[0], values[1:]...); len(exprs) > 0 { + db.assignInterfacesToValue(exprs) } return } From 9144969c83829d2f14049a6e4882f785a90b6cf9 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 2 Apr 2022 17:17:47 +0800 Subject: [PATCH 4/5] Allow to use tag to disable auto create/update time --- schema/field.go | 4 ++-- tests/associations_test.go | 1 - tests/go.mod | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/schema/field.go b/schema/field.go index 3b5cc5c5..77521ad3 100644 --- a/schema/field.go +++ b/schema/field.go @@ -275,7 +275,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { field.DataType = DataType(dataTyper.GormDataType()) } - if v, ok := field.TagSettings["AUTOCREATETIME"]; ok || (field.Name == "CreatedAt" && (field.DataType == Time || field.DataType == Int || field.DataType == Uint)) { + if v, ok := field.TagSettings["AUTOCREATETIME"]; (ok && utils.CheckTruth(v)) || (!ok && field.Name == "CreatedAt" && (field.DataType == Time || field.DataType == Int || field.DataType == Uint)) { if field.DataType == Time { field.AutoCreateTime = UnixTime } else if strings.ToUpper(v) == "NANO" { @@ -287,7 +287,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { } } - if v, ok := field.TagSettings["AUTOUPDATETIME"]; ok || (field.Name == "UpdatedAt" && (field.DataType == Time || field.DataType == Int || field.DataType == Uint)) { + if v, ok := field.TagSettings["AUTOUPDATETIME"]; (ok && utils.CheckTruth(v)) || (!ok && field.Name == "UpdatedAt" && (field.DataType == Time || field.DataType == Int || field.DataType == Uint)) { if field.DataType == Time { field.AutoUpdateTime = UnixTime } else if strings.ToUpper(v) == "NANO" { diff --git a/tests/associations_test.go b/tests/associations_test.go index bc3dac55..e729e979 100644 --- a/tests/associations_test.go +++ b/tests/associations_test.go @@ -263,7 +263,6 @@ func TestSaveHasManyCircularReference(t *testing.T) { } func TestAssociationError(t *testing.T) { - DB = DB.Debug() user := *GetUser("TestAssociationError", Config{Pets: 2, Company: true, Account: true, Languages: 2}) DB.Create(&user) diff --git a/tests/go.mod b/tests/go.mod index b85ebdad..fc6600b7 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -9,7 +9,7 @@ require ( github.com/jinzhu/now v1.1.5 github.com/lib/pq v1.10.4 github.com/mattn/go-sqlite3 v1.14.12 // indirect - golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 // indirect + golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 // indirect gorm.io/driver/mysql v1.3.2 gorm.io/driver/postgres v1.3.1 gorm.io/driver/sqlite v1.3.1 From 38a24606da3cd1e312644ef5f8d71e4d0d35554a Mon Sep 17 00:00:00 2001 From: huangcheng1 Date: Sat, 2 Apr 2022 17:27:53 +0800 Subject: [PATCH 5/5] fix: tables lost when joins exists in from clause, close #5218 commit 7f6a603afa26820e187489b5203f93adc513687c Author: Jinzhu Date: Sat Apr 2 17:26:48 2022 +0800 Refactor #5218 commit 95d00e6ff2668233f3eca98aa4917291e3d869bd Author: huangcheng1 Date: Fri Apr 1 16:30:27 2022 +0800 fix: tables lost when joins exists in from clause --- callbacks/query.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/callbacks/query.go b/callbacks/query.go index 6eda52ef..fb2bb37a 100644 --- a/callbacks/query.go +++ b/callbacks/query.go @@ -96,12 +96,12 @@ func BuildQuerySQL(db *gorm.DB) { } // inline joins - joins := []clause.Join{} - if fromClause, ok := db.Statement.Clauses["FROM"].Expression.(clause.From); ok { - joins = fromClause.Joins + fromClause := clause.From{} + if v, ok := db.Statement.Clauses["FROM"].Expression.(clause.From); ok { + fromClause = v } - if len(db.Statement.Joins) != 0 || len(joins) != 0 { + if len(db.Statement.Joins) != 0 || len(fromClause.Joins) != 0 { if len(db.Statement.Selects) == 0 && len(db.Statement.Omits) == 0 && db.Statement.Schema != nil { clauseSelect.Columns = make([]clause.Column, len(db.Statement.Schema.DBNames)) for idx, dbName := range db.Statement.Schema.DBNames { @@ -111,7 +111,7 @@ func BuildQuerySQL(db *gorm.DB) { for _, join := range db.Statement.Joins { if db.Statement.Schema == nil { - joins = append(joins, clause.Join{ + fromClause.Joins = append(fromClause.Joins, clause.Join{ Expression: clause.NamedExpr{SQL: join.Name, Vars: join.Conds}, }) } else if relation, ok := db.Statement.Schema.Relationships.Relations[join.Name]; ok { @@ -176,19 +176,19 @@ func BuildQuerySQL(db *gorm.DB) { } } - joins = append(joins, clause.Join{ + fromClause.Joins = append(fromClause.Joins, clause.Join{ Type: clause.LeftJoin, Table: clause.Table{Name: relation.FieldSchema.Table, Alias: tableAliasName}, ON: clause.Where{Exprs: exprs}, }) } else { - joins = append(joins, clause.Join{ + fromClause.Joins = append(fromClause.Joins, clause.Join{ Expression: clause.NamedExpr{SQL: join.Name, Vars: join.Conds}, }) } } - db.Statement.AddClause(clause.From{Joins: joins}) + db.Statement.AddClause(fromClause) db.Statement.Joins = nil } else { db.Statement.AddClauseIfNotExists(clause.From{})