Improvements in scope_private.go

This commit is contained in:
Bojan Petrovic 2015-10-21 14:41:50 +02:00
parent d9faa4949c
commit e5d3836451

11
scope_private.go Normal file → Executable file
View File

@ -57,8 +57,13 @@ func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str stri
str = strings.Replace(str, "?", strings.Join(tempMarks, ","), 1) str = strings.Replace(str, "?", strings.Join(tempMarks, ","), 1)
default: default:
if valuer, ok := interface{}(arg).(driver.Valuer); ok { if valuer, ok := interface{}(arg).(driver.Valuer); ok {
tp := reflect.TypeOf(arg)
if tp.Kind() != reflect.Struct && reflect.ValueOf(arg).IsNil() || arg == nil || valuer == nil {
arg = nil
} else {
arg, _ = valuer.Value() arg, _ = valuer.Value()
} }
}
str = strings.Replace(str, "?", scope.AddToVars(arg), 1) str = strings.Replace(str, "?", scope.AddToVars(arg), 1)
} }
@ -413,7 +418,7 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
if relationship := fromField.Relationship; relationship != nil { if relationship := fromField.Relationship; relationship != nil {
if relationship.Kind == "many_to_many" { if relationship.Kind == "many_to_many" {
joinTableHandler := relationship.JoinTableHandler joinTableHandler := relationship.JoinTableHandler
scope.Err(joinTableHandler.JoinWith(toScope.db, scope.Value).Find(value).Error) scope.Err(joinTableHandler.JoinWith(joinTableHandler, toScope.db, scope.Value).Find(value).Error)
} else if relationship.Kind == "belongs_to" { } else if relationship.Kind == "belongs_to" {
sql := fmt.Sprintf("%v = ?", scope.Quote(toScope.PrimaryKey())) sql := fmt.Sprintf("%v = ?", scope.Quote(toScope.PrimaryKey()))
foreignKeyValue := fromFields[relationship.ForeignDBName].Field.Interface() foreignKeyValue := fromFields[relationship.ForeignDBName].Field.Interface()
@ -530,7 +535,7 @@ func (scope *Scope) addForeignKey(field string, dest string, onDelete string, on
var table = scope.TableName() var table = scope.TableName()
var keyName = fmt.Sprintf("%s_%s_foreign", table, field) var keyName = fmt.Sprintf("%s_%s_foreign", table, field)
var query = `ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s ON DELETE %s ON UPDATE %s;` var query = `ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s ON DELETE %s ON UPDATE %s;`
scope.Raw(fmt.Sprintf(query, scope.QuotedTableName(), keyName, field, dest, onDelete, onUpdate)).Exec() scope.Raw(fmt.Sprintf(query, scope.QuotedTableName(), scope.Quote(keyName), scope.Quote(field), scope.Quote(dest), onDelete, onUpdate)).Exec()
} }
func (scope *Scope) removeIndex(indexName string) { func (scope *Scope) removeIndex(indexName string) {
@ -548,7 +553,7 @@ func (scope *Scope) autoMigrate() *Scope {
if !scope.Dialect().HasColumn(scope, tableName, field.DBName) { if !scope.Dialect().HasColumn(scope, tableName, field.DBName) {
if field.IsNormal { if field.IsNormal {
sqlTag := scope.generateSqlTag(field) sqlTag := scope.generateSqlTag(field)
scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", quotedTableName, field.DBName, sqlTag)).Exec() scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", quotedTableName, scope.Quote(field.DBName), sqlTag)).Exec()
} }
} }
scope.createJoinTable(field) scope.createJoinTable(field)