diff --git a/callback_create.go b/callback_create.go index e7fe6f86..16dd9639 100644 --- a/callback_create.go +++ b/callback_create.go @@ -65,13 +65,13 @@ func createCallback(scope *Scope) { scope.InstanceSet("gorm:blank_columns_with_default_value", blankColumnsWithDefaultValue) } else if !field.IsPrimaryKey || !field.IsBlank { columns = append(columns, scope.Quote(field.DBName)) - placeholders = append(placeholders, scope.AddToVars(field.Field.Interface())) + placeholders = append(placeholders, scope.WrapPlaceholder(field, scope.AddToVars(field.Field.Interface()))) } } else if field.Relationship != nil && field.Relationship.Kind == "belongs_to" { for _, foreignKey := range field.Relationship.ForeignDBNames { if foreignField, ok := scope.FieldByName(foreignKey); ok && !scope.changeableField(foreignField) { columns = append(columns, scope.Quote(foreignField.DBName)) - placeholders = append(placeholders, scope.AddToVars(foreignField.Field.Interface())) + placeholders = append(placeholders, scope.WrapPlaceholder(foreignField, scope.AddToVars(foreignField.Field.Interface()))) } } } diff --git a/callback_update.go b/callback_update.go index 373bd726..d8c658c6 100644 --- a/callback_update.go +++ b/callback_update.go @@ -76,12 +76,12 @@ func updateCallback(scope *Scope) { for _, field := range scope.Fields() { if scope.changeableField(field) { if !field.IsPrimaryKey && field.IsNormal { - sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) + sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.WrapPlaceholder(field, scope.AddToVars(field.Field.Interface())))) } else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" { for _, foreignKey := range relationship.ForeignDBNames { if foreignField, ok := scope.FieldByName(foreignKey); ok && !scope.changeableField(foreignField) { sqls = append(sqls, - fmt.Sprintf("%v = %v", scope.Quote(foreignField.DBName), scope.AddToVars(foreignField.Field.Interface()))) + fmt.Sprintf("%v = %v", scope.Quote(foreignField.DBName), scope.WrapPlaceholder(foreignField, scope.AddToVars(foreignField.Field.Interface())))) } } } diff --git a/scope.go b/scope.go index 397ccf0b..4d4d3b7a 100644 --- a/scope.go +++ b/scope.go @@ -277,6 +277,16 @@ func (scope *Scope) AddToVars(value interface{}) string { return scope.Dialect().BindVar(len(scope.SQLVars)) } +// WrapPlaceholder returns a field's value placeholder wrapped into wrapper function +func (scope *Scope) WrapPlaceholder(field *Field, placeholder string) string { + if wr, exists := field.TagSettings["WRAPPER"]; !exists || len(wr) == 0 { + return placeholder + } else { + // Wrap a placeholder into wrapper + return strings.Replace(wr, "?", placeholder, -1) + } +} + // SelectAttrs return selected attributes func (scope *Scope) SelectAttrs() []string { if scope.selectAttrs == nil {