From 5ad6f621e6f59672f5b5061df85b243436fde048 Mon Sep 17 00:00:00 2001 From: Sai Date: Thu, 13 Dec 2018 22:04:51 +0900 Subject: [PATCH 1/9] logMode codes more readable (#2216) --- main.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 17c75ed3..c1197bc9 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,7 @@ type DB struct { // single db db SQLCommon blockGlobalUpdate bool - logMode int + logMode logModeValue logger logger search *search values sync.Map @@ -31,6 +31,14 @@ type DB struct { singularTable bool } +type logModeValue int + +const ( + defaultLogMode logModeValue = iota + noLogMode + detailedLogMode +) + // Open initialize a new db connection, need to import driver first, e.g: // // import _ "github.com/go-sql-driver/mysql" @@ -141,9 +149,9 @@ func (s *DB) SetLogger(log logger) { // LogMode set log mode, `true` for detailed logs, `false` for no log, default, will only print error logs func (s *DB) LogMode(enable bool) *DB { if enable { - s.logMode = 2 + s.logMode = detailedLogMode } else { - s.logMode = 1 + s.logMode = noLogMode } return s } @@ -716,7 +724,7 @@ func (s *DB) SetJoinTableHandler(source interface{}, column string, handler Join func (s *DB) AddError(err error) error { if err != nil { if err != ErrRecordNotFound { - if s.logMode == 0 { + if s.logMode == defaultLogMode { go s.print(fileWithLineNum(), err) } else { s.log(err) @@ -780,13 +788,13 @@ func (s *DB) print(v ...interface{}) { } func (s *DB) log(v ...interface{}) { - if s != nil && s.logMode == 2 { + if s != nil && s.logMode == detailedLogMode { s.print(append([]interface{}{"log", fileWithLineNum()}, v...)...) } } func (s *DB) slog(sql string, t time.Time, vars ...interface{}) { - if s.logMode == 2 { + if s.logMode == detailedLogMode { s.print("sql", fileWithLineNum(), NowFunc().Sub(t), sql, vars, s.RowsAffected) } } From 447d578628011308498d9316838f59f93834967c Mon Sep 17 00:00:00 2001 From: Zed Date: Wed, 2 Jan 2019 21:23:43 +0800 Subject: [PATCH 2/9] amended comments in error.go for clarity and grammar; for more polish when using IDEs (e.g. VSCODE) that show comments as help text (#2182) --- errors.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/errors.go b/errors.go index 27c9a92d..d5ef8d57 100644 --- a/errors.go +++ b/errors.go @@ -6,11 +6,11 @@ import ( ) var ( - // ErrRecordNotFound record not found error, happens when only haven't find any matched data when looking up with a struct, finding a slice won't return this error + // ErrRecordNotFound returns a "record not found error". Occurs only when attempting to query the database with a struct; querying with a slice won't return this error ErrRecordNotFound = errors.New("record not found") - // ErrInvalidSQL invalid SQL error, happens when you passed invalid SQL + // ErrInvalidSQL occurs when you attempt a query with invalid SQL ErrInvalidSQL = errors.New("invalid SQL") - // ErrInvalidTransaction invalid transaction when you are trying to `Commit` or `Rollback` + // ErrInvalidTransaction occurs when you are trying to `Commit` or `Rollback` ErrInvalidTransaction = errors.New("no valid transaction") // ErrCantStartTransaction can't start transaction when you are trying to start one with `Begin` ErrCantStartTransaction = errors.New("can't start transaction") @@ -21,7 +21,7 @@ var ( // Errors contains all happened errors type Errors []error -// IsRecordNotFoundError returns current error has record not found error or not +// IsRecordNotFoundError returns true if error contains a RecordNotFound error func IsRecordNotFoundError(err error) bool { if errs, ok := err.(Errors); ok { for _, err := range errs { @@ -33,12 +33,12 @@ func IsRecordNotFoundError(err error) bool { return err == ErrRecordNotFound } -// GetErrors gets all happened errors +// GetErrors gets all errors that have occurred and returns a slice of errors (Error type) func (errs Errors) GetErrors() []error { return errs } -// Add adds an error +// Add adds an error to a given slice of errors func (errs Errors) Add(newErrors ...error) Errors { for _, err := range newErrors { if err == nil { @@ -62,7 +62,7 @@ func (errs Errors) Add(newErrors ...error) Errors { return errs } -// Error format happened errors +// Error takes a slice of all errors that have occurred and returns it as a formatted string func (errs Errors) Error() string { var errors = []string{} for _, e := range errs { From ac6c89ec0cb95e921ddf43759f1f1f367d9e587c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B9=8F?= Date: Wed, 2 Jan 2019 21:25:37 +0800 Subject: [PATCH 3/9] =?UTF-8?q?search=E4=B8=8D=E9=9C=80=E8=A6=81=E5=86=8Dc?= =?UTF-8?q?lone=EF=BC=8CdbClone=E5=86=85=E7=9A=84search=E5=B7=B2=E7=BB=8F?= =?UTF-8?q?=E6=98=AF=E4=B8=80=E4=B8=AA=E5=85=A8=E6=96=B0=E7=9A=84=E4=BA=86?= =?UTF-8?q?=20(#2179)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index c1197bc9..34a6ddc8 100644 --- a/main.go +++ b/main.go @@ -178,7 +178,7 @@ func (s *DB) SingularTable(enable bool) { func (s *DB) NewScope(value interface{}) *Scope { dbClone := s.clone() dbClone.Value = value - return &Scope{db: dbClone, Search: dbClone.search.clone(), Value: value} + return &Scope{db: dbClone, Search: dbClone.search, Value: value} } // QueryExpr returns the query as expr object From e2cfd6be3b09b548be8c4d349490bf563cb1ee13 Mon Sep 17 00:00:00 2001 From: David Zhang Date: Wed, 2 Jan 2019 21:27:17 +0800 Subject: [PATCH 4/9] LintFix: Make receiver name of structField consistent (#2164) * Make receiver name of structField consistent * Change s to sf --- model_struct.go | 66 ++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/model_struct.go b/model_struct.go index 8c27e209..08e741fe 100644 --- a/model_struct.go +++ b/model_struct.go @@ -21,12 +21,12 @@ var modelStructsMap sync.Map // ModelStruct model definition type ModelStruct struct { - PrimaryFields []*StructField - StructFields []*StructField - ModelType reflect.Type + PrimaryFields []*StructField + StructFields []*StructField + ModelType reflect.Type defaultTableName string - l sync.Mutex + l sync.Mutex } // TableName returns model's table name @@ -70,52 +70,52 @@ type StructField struct { } // TagSettingsSet Sets a tag in the tag settings map -func (s *StructField) TagSettingsSet(key, val string) { - s.tagSettingsLock.Lock() - defer s.tagSettingsLock.Unlock() - s.TagSettings[key] = val +func (sf *StructField) TagSettingsSet(key, val string) { + sf.tagSettingsLock.Lock() + defer sf.tagSettingsLock.Unlock() + sf.TagSettings[key] = val } // TagSettingsGet returns a tag from the tag settings -func (s *StructField) TagSettingsGet(key string) (string, bool) { - s.tagSettingsLock.RLock() - defer s.tagSettingsLock.RUnlock() - val, ok := s.TagSettings[key] +func (sf *StructField) TagSettingsGet(key string) (string, bool) { + sf.tagSettingsLock.RLock() + defer sf.tagSettingsLock.RUnlock() + val, ok := sf.TagSettings[key] return val, ok } // TagSettingsDelete deletes a tag -func (s *StructField) TagSettingsDelete(key string) { - s.tagSettingsLock.Lock() - defer s.tagSettingsLock.Unlock() - delete(s.TagSettings, key) +func (sf *StructField) TagSettingsDelete(key string) { + sf.tagSettingsLock.Lock() + defer sf.tagSettingsLock.Unlock() + delete(sf.TagSettings, key) } -func (structField *StructField) clone() *StructField { +func (sf *StructField) clone() *StructField { clone := &StructField{ - DBName: structField.DBName, - Name: structField.Name, - Names: structField.Names, - IsPrimaryKey: structField.IsPrimaryKey, - IsNormal: structField.IsNormal, - IsIgnored: structField.IsIgnored, - IsScanner: structField.IsScanner, - HasDefaultValue: structField.HasDefaultValue, - Tag: structField.Tag, + DBName: sf.DBName, + Name: sf.Name, + Names: sf.Names, + IsPrimaryKey: sf.IsPrimaryKey, + IsNormal: sf.IsNormal, + IsIgnored: sf.IsIgnored, + IsScanner: sf.IsScanner, + HasDefaultValue: sf.HasDefaultValue, + Tag: sf.Tag, TagSettings: map[string]string{}, - Struct: structField.Struct, - IsForeignKey: structField.IsForeignKey, + Struct: sf.Struct, + IsForeignKey: sf.IsForeignKey, } - if structField.Relationship != nil { - relationship := *structField.Relationship + if sf.Relationship != nil { + relationship := *sf.Relationship clone.Relationship = &relationship } // copy the struct field tagSettings, they should be read-locked while they are copied - structField.tagSettingsLock.Lock() - defer structField.tagSettingsLock.Unlock() - for key, value := range structField.TagSettings { + sf.tagSettingsLock.Lock() + defer sf.tagSettingsLock.Unlock() + for key, value := range sf.TagSettings { clone.TagSettings[key] = value } From a6382da48500a7adfe8a3f75eedc89a34644f54f Mon Sep 17 00:00:00 2001 From: Edgar Fournival Date: Wed, 2 Jan 2019 14:28:02 +0100 Subject: [PATCH 5/9] Do not set CreatedAt if blank during Save (#2207) --- callback_update.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/callback_update.go b/callback_update.go index f6ba0ffd..c52162c8 100644 --- a/callback_update.go +++ b/callback_update.go @@ -75,7 +75,7 @@ func updateCallback(scope *Scope) { } else { for _, field := range scope.Fields() { if scope.changeableField(field) { - if !field.IsPrimaryKey && field.IsNormal { + if !field.IsPrimaryKey && field.IsNormal && (field.Name != "CreatedAt" || !field.IsBlank) { if !field.IsForeignKey || !field.IsBlank || !field.HasDefaultValue { sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) } From 8316f94b72719208b2d939c70f3824287e62ea5d Mon Sep 17 00:00:00 2001 From: Brent Hughes Date: Wed, 2 Jan 2019 07:28:46 -0600 Subject: [PATCH 6/9] Fix Panic in test scenerio (#2131) I have found that there are times when testing that if I did not create the database through Open() it will not have the parent set and cause a panic when it hits this code path. --- model_struct.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model_struct.go b/model_struct.go index 08e741fe..9e93db63 100644 --- a/model_struct.go +++ b/model_struct.go @@ -40,7 +40,7 @@ func (s *ModelStruct) TableName(db *DB) string { s.defaultTableName = tabler.TableName() } else { tableName := ToTableName(s.ModelType.Name()) - if db == nil || !db.parent.singularTable { + if db == nil || (db.parent != nil && !db.parent.singularTable) { tableName = inflection.Plural(tableName) } s.defaultTableName = tableName From 9f1a7f53511168c0567b4b4b4f10ab7d21265174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=9C=BB=E8=9C=93=E7=89=B9=E6=B4=BE=E5=91=98?= Date: Wed, 2 Jan 2019 21:32:08 +0800 Subject: [PATCH 7/9] optimize getColumnAsArray (#2196) --- scope.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/scope.go b/scope.go index 806ccb7d..90e16782 100644 --- a/scope.go +++ b/scope.go @@ -1309,6 +1309,7 @@ func (scope *Scope) autoIndex() *Scope { } func (scope *Scope) getColumnAsArray(columns []string, values ...interface{}) (results [][]interface{}) { + resultMap := make(map[string][]interface{}) for _, value := range values { indirectValue := indirect(reflect.ValueOf(value)) @@ -1327,7 +1328,10 @@ func (scope *Scope) getColumnAsArray(columns []string, values ...interface{}) (r } if hasValue { - results = append(results, result) + h := fmt.Sprint(result...) + if _, exist := resultMap[h]; !exist { + resultMap[h] = result + } } } case reflect.Struct: @@ -1342,11 +1346,16 @@ func (scope *Scope) getColumnAsArray(columns []string, values ...interface{}) (r } if hasValue { - results = append(results, result) + h := fmt.Sprint(result...) + if _, exist := resultMap[h]; !exist { + resultMap[h] = result + } } } } - + for _, v := range resultMap { + results = append(results, v) + } return } From 8494ecdc9857e74477cd95965df2f0297fe6a461 Mon Sep 17 00:00:00 2001 From: aixiaoxiang Date: Sun, 10 Feb 2019 15:37:39 +0800 Subject: [PATCH 8/9] Better log output int8, int, int16, int32, int64, float32, float64, bool. (#2258) * Better log output int, int16, int32, int64, int8, float32, float64. * Better log output bool --- logger.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/logger.go b/logger.go index 4324a2e4..10a1b805 100644 --- a/logger.go +++ b/logger.go @@ -63,7 +63,13 @@ var LogFormatter = func(values ...interface{}) (messages []interface{}) { formattedValues = append(formattedValues, "NULL") } } else { - formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value)) + switch value.(type) { + case int8, int, int16, int32, int64, float32, float64, bool: + formattedValues = append(formattedValues, fmt.Sprintf("%v", value)) + break + default: + formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value)) + } } } else { formattedValues = append(formattedValues, "NULL") From 906799fef2f895116d915e1793314ab9053b400d Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 10 Feb 2019 15:39:40 +0800 Subject: [PATCH 9/9] Better log output for uint* --- logger.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/logger.go b/logger.go index 10a1b805..484bc022 100644 --- a/logger.go +++ b/logger.go @@ -64,9 +64,8 @@ var LogFormatter = func(values ...interface{}) (messages []interface{}) { } } else { switch value.(type) { - case int8, int, int16, int32, int64, float32, float64, bool: + case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool: formattedValues = append(formattedValues, fmt.Sprintf("%v", value)) - break default: formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value)) }