Removed unnecessary cloning.
`NewScope` clones `DB` no need to chain a call to clone with `NewScope`.
This commit is contained in:
		
							parent
							
								
									9acaa33324
								
							
						
					
					
						commit
						b388db9c64
					
				
							
								
								
									
										38
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								main.go
									
									
									
									
									
								
							@ -265,7 +265,7 @@ func (s *DB) Assign(attrs ...interface{}) *DB {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// First find first record that match given conditions, order by primary key
 | 
					// First find first record that match given conditions, order by primary key
 | 
				
			||||||
func (s *DB) First(out interface{}, where ...interface{}) *DB {
 | 
					func (s *DB) First(out interface{}, where ...interface{}) *DB {
 | 
				
			||||||
	newScope := s.clone().NewScope(out)
 | 
						newScope := s.NewScope(out)
 | 
				
			||||||
	newScope.Search.Limit(1)
 | 
						newScope.Search.Limit(1)
 | 
				
			||||||
	return newScope.Set("gorm:order_by_primary_key", "ASC").
 | 
						return newScope.Set("gorm:order_by_primary_key", "ASC").
 | 
				
			||||||
		inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
 | 
							inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
 | 
				
			||||||
@ -273,7 +273,7 @@ func (s *DB) First(out interface{}, where ...interface{}) *DB {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Last find last record that match given conditions, order by primary key
 | 
					// Last find last record that match given conditions, order by primary key
 | 
				
			||||||
func (s *DB) Last(out interface{}, where ...interface{}) *DB {
 | 
					func (s *DB) Last(out interface{}, where ...interface{}) *DB {
 | 
				
			||||||
	newScope := s.clone().NewScope(out)
 | 
						newScope := s.NewScope(out)
 | 
				
			||||||
	newScope.Search.Limit(1)
 | 
						newScope.Search.Limit(1)
 | 
				
			||||||
	return newScope.Set("gorm:order_by_primary_key", "DESC").
 | 
						return newScope.Set("gorm:order_by_primary_key", "DESC").
 | 
				
			||||||
		inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
 | 
							inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
 | 
				
			||||||
@ -281,12 +281,12 @@ func (s *DB) Last(out interface{}, where ...interface{}) *DB {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Find find records that match given conditions
 | 
					// Find find records that match given conditions
 | 
				
			||||||
func (s *DB) Find(out interface{}, where ...interface{}) *DB {
 | 
					func (s *DB) Find(out interface{}, where ...interface{}) *DB {
 | 
				
			||||||
	return s.clone().NewScope(out).inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
 | 
						return s.NewScope(out).inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Scan scan value to a struct
 | 
					// Scan scan value to a struct
 | 
				
			||||||
func (s *DB) Scan(dest interface{}) *DB {
 | 
					func (s *DB) Scan(dest interface{}) *DB {
 | 
				
			||||||
	return s.clone().NewScope(s.Value).Set("gorm:query_destination", dest).callCallbacks(s.parent.callbacks.queries).db
 | 
						return s.NewScope(s.Value).Set("gorm:query_destination", dest).callCallbacks(s.parent.callbacks.queries).db
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Row return `*sql.Row` with given conditions
 | 
					// Row return `*sql.Row` with given conditions
 | 
				
			||||||
@ -302,8 +302,8 @@ func (s *DB) Rows() (*sql.Rows, error) {
 | 
				
			|||||||
// ScanRows scan `*sql.Rows` to give struct
 | 
					// ScanRows scan `*sql.Rows` to give struct
 | 
				
			||||||
func (s *DB) ScanRows(rows *sql.Rows, result interface{}) error {
 | 
					func (s *DB) ScanRows(rows *sql.Rows, result interface{}) error {
 | 
				
			||||||
	var (
 | 
						var (
 | 
				
			||||||
		clone        = s.clone()
 | 
							scope        = s.NewScope(result)
 | 
				
			||||||
		scope        = clone.NewScope(result)
 | 
							clone        = scope.db
 | 
				
			||||||
		columns, err = rows.Columns()
 | 
							columns, err = rows.Columns()
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -328,7 +328,7 @@ func (s *DB) Count(value interface{}) *DB {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Related get related associations
 | 
					// Related get related associations
 | 
				
			||||||
func (s *DB) Related(value interface{}, foreignKeys ...string) *DB {
 | 
					func (s *DB) Related(value interface{}, foreignKeys ...string) *DB {
 | 
				
			||||||
	return s.clone().NewScope(s.Value).related(value, foreignKeys...).db
 | 
						return s.NewScope(s.Value).related(value, foreignKeys...).db
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// FirstOrInit find first matched record or initialize a new one with given conditions (only works with struct, map conditions)
 | 
					// FirstOrInit find first matched record or initialize a new one with given conditions (only works with struct, map conditions)
 | 
				
			||||||
@ -368,7 +368,7 @@ func (s *DB) Update(attrs ...interface{}) *DB {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Updates update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
 | 
					// Updates update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
 | 
				
			||||||
func (s *DB) Updates(values interface{}, ignoreProtectedAttrs ...bool) *DB {
 | 
					func (s *DB) Updates(values interface{}, ignoreProtectedAttrs ...bool) *DB {
 | 
				
			||||||
	return s.clone().NewScope(s.Value).
 | 
						return s.NewScope(s.Value).
 | 
				
			||||||
		Set("gorm:ignore_protected_attrs", len(ignoreProtectedAttrs) > 0).
 | 
							Set("gorm:ignore_protected_attrs", len(ignoreProtectedAttrs) > 0).
 | 
				
			||||||
		InstanceSet("gorm:update_interface", values).
 | 
							InstanceSet("gorm:update_interface", values).
 | 
				
			||||||
		callCallbacks(s.parent.callbacks.updates).db
 | 
							callCallbacks(s.parent.callbacks.updates).db
 | 
				
			||||||
@ -381,7 +381,7 @@ func (s *DB) UpdateColumn(attrs ...interface{}) *DB {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// UpdateColumns update attributes without callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
 | 
					// UpdateColumns update attributes without callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
 | 
				
			||||||
func (s *DB) UpdateColumns(values interface{}) *DB {
 | 
					func (s *DB) UpdateColumns(values interface{}) *DB {
 | 
				
			||||||
	return s.clone().NewScope(s.Value).
 | 
						return s.NewScope(s.Value).
 | 
				
			||||||
		Set("gorm:update_column", true).
 | 
							Set("gorm:update_column", true).
 | 
				
			||||||
		Set("gorm:save_associations", false).
 | 
							Set("gorm:save_associations", false).
 | 
				
			||||||
		InstanceSet("gorm:update_interface", values).
 | 
							InstanceSet("gorm:update_interface", values).
 | 
				
			||||||
@ -390,7 +390,7 @@ func (s *DB) UpdateColumns(values interface{}) *DB {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Save update value in database, if the value doesn't have primary key, will insert it
 | 
					// Save update value in database, if the value doesn't have primary key, will insert it
 | 
				
			||||||
func (s *DB) Save(value interface{}) *DB {
 | 
					func (s *DB) Save(value interface{}) *DB {
 | 
				
			||||||
	scope := s.clone().NewScope(value)
 | 
						scope := s.NewScope(value)
 | 
				
			||||||
	if !scope.PrimaryKeyZero() {
 | 
						if !scope.PrimaryKeyZero() {
 | 
				
			||||||
		newDB := scope.callCallbacks(s.parent.callbacks.updates).db
 | 
							newDB := scope.callCallbacks(s.parent.callbacks.updates).db
 | 
				
			||||||
		if newDB.Error == nil && newDB.RowsAffected == 0 {
 | 
							if newDB.Error == nil && newDB.RowsAffected == 0 {
 | 
				
			||||||
@ -403,13 +403,13 @@ func (s *DB) Save(value interface{}) *DB {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Create insert the value into database
 | 
					// Create insert the value into database
 | 
				
			||||||
func (s *DB) Create(value interface{}) *DB {
 | 
					func (s *DB) Create(value interface{}) *DB {
 | 
				
			||||||
	scope := s.clone().NewScope(value)
 | 
						scope := s.NewScope(value)
 | 
				
			||||||
	return scope.callCallbacks(s.parent.callbacks.creates).db
 | 
						return scope.callCallbacks(s.parent.callbacks.creates).db
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Delete delete value match given conditions, if the value has primary key, then will including the primary key as condition
 | 
					// Delete delete value match given conditions, if the value has primary key, then will including the primary key as condition
 | 
				
			||||||
func (s *DB) Delete(value interface{}, where ...interface{}) *DB {
 | 
					func (s *DB) Delete(value interface{}, where ...interface{}) *DB {
 | 
				
			||||||
	return s.clone().NewScope(value).inlineCondition(where...).callCallbacks(s.parent.callbacks.deletes).db
 | 
						return s.NewScope(value).inlineCondition(where...).callCallbacks(s.parent.callbacks.deletes).db
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Raw use raw sql as conditions, won't run it unless invoked by other methods
 | 
					// Raw use raw sql as conditions, won't run it unless invoked by other methods
 | 
				
			||||||
@ -420,7 +420,7 @@ func (s *DB) Raw(sql string, values ...interface{}) *DB {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Exec execute raw sql
 | 
					// Exec execute raw sql
 | 
				
			||||||
func (s *DB) Exec(sql string, values ...interface{}) *DB {
 | 
					func (s *DB) Exec(sql string, values ...interface{}) *DB {
 | 
				
			||||||
	scope := s.clone().NewScope(nil)
 | 
						scope := s.NewScope(nil)
 | 
				
			||||||
	generatedSQL := scope.buildWhereCondition(map[string]interface{}{"query": sql, "args": values})
 | 
						generatedSQL := scope.buildWhereCondition(map[string]interface{}{"query": sql, "args": values})
 | 
				
			||||||
	generatedSQL = strings.TrimSuffix(strings.TrimPrefix(generatedSQL, "("), ")")
 | 
						generatedSQL = strings.TrimSuffix(strings.TrimPrefix(generatedSQL, "("), ")")
 | 
				
			||||||
	scope.Raw(generatedSQL)
 | 
						scope.Raw(generatedSQL)
 | 
				
			||||||
@ -486,7 +486,7 @@ func (s *DB) Rollback() *DB {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// NewRecord check if value's primary key is blank
 | 
					// NewRecord check if value's primary key is blank
 | 
				
			||||||
func (s *DB) NewRecord(value interface{}) bool {
 | 
					func (s *DB) NewRecord(value interface{}) bool {
 | 
				
			||||||
	return s.clone().NewScope(value).PrimaryKeyZero()
 | 
						return s.NewScope(value).PrimaryKeyZero()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// RecordNotFound check if returning ErrRecordNotFound error
 | 
					// RecordNotFound check if returning ErrRecordNotFound error
 | 
				
			||||||
@ -535,7 +535,7 @@ func (s *DB) DropTableIfExists(values ...interface{}) *DB {
 | 
				
			|||||||
// HasTable check has table or not
 | 
					// HasTable check has table or not
 | 
				
			||||||
func (s *DB) HasTable(value interface{}) bool {
 | 
					func (s *DB) HasTable(value interface{}) bool {
 | 
				
			||||||
	var (
 | 
						var (
 | 
				
			||||||
		scope     = s.clone().NewScope(value)
 | 
							scope     = s.NewScope(value)
 | 
				
			||||||
		tableName string
 | 
							tableName string
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -561,14 +561,14 @@ func (s *DB) AutoMigrate(values ...interface{}) *DB {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// ModifyColumn modify column to type
 | 
					// ModifyColumn modify column to type
 | 
				
			||||||
func (s *DB) ModifyColumn(column string, typ string) *DB {
 | 
					func (s *DB) ModifyColumn(column string, typ string) *DB {
 | 
				
			||||||
	scope := s.clone().NewScope(s.Value)
 | 
						scope := s.NewScope(s.Value)
 | 
				
			||||||
	scope.modifyColumn(column, typ)
 | 
						scope.modifyColumn(column, typ)
 | 
				
			||||||
	return scope.db
 | 
						return scope.db
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// DropColumn drop a column
 | 
					// DropColumn drop a column
 | 
				
			||||||
func (s *DB) DropColumn(column string) *DB {
 | 
					func (s *DB) DropColumn(column string) *DB {
 | 
				
			||||||
	scope := s.clone().NewScope(s.Value)
 | 
						scope := s.NewScope(s.Value)
 | 
				
			||||||
	scope.dropColumn(column)
 | 
						scope.dropColumn(column)
 | 
				
			||||||
	return scope.db
 | 
						return scope.db
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -589,7 +589,7 @@ func (s *DB) AddUniqueIndex(indexName string, columns ...string) *DB {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// RemoveIndex remove index with name
 | 
					// RemoveIndex remove index with name
 | 
				
			||||||
func (s *DB) RemoveIndex(indexName string) *DB {
 | 
					func (s *DB) RemoveIndex(indexName string) *DB {
 | 
				
			||||||
	scope := s.clone().NewScope(s.Value)
 | 
						scope := s.NewScope(s.Value)
 | 
				
			||||||
	scope.removeIndex(indexName)
 | 
						scope.removeIndex(indexName)
 | 
				
			||||||
	return scope.db
 | 
						return scope.db
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -597,7 +597,7 @@ func (s *DB) RemoveIndex(indexName string) *DB {
 | 
				
			|||||||
// AddForeignKey Add foreign key to the given scope, e.g:
 | 
					// AddForeignKey Add foreign key to the given scope, e.g:
 | 
				
			||||||
//     db.Model(&User{}).AddForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT")
 | 
					//     db.Model(&User{}).AddForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT")
 | 
				
			||||||
func (s *DB) AddForeignKey(field string, dest string, onDelete string, onUpdate string) *DB {
 | 
					func (s *DB) AddForeignKey(field string, dest string, onDelete string, onUpdate string) *DB {
 | 
				
			||||||
	scope := s.clone().NewScope(s.Value)
 | 
						scope := s.NewScope(s.Value)
 | 
				
			||||||
	scope.addForeignKey(field, dest, onDelete, onUpdate)
 | 
						scope.addForeignKey(field, dest, onDelete, onUpdate)
 | 
				
			||||||
	return scope.db
 | 
						return scope.db
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user