diff --git a/callback_create.go b/callback_create.go index e7fe6f86..dd4d8365 100644 --- a/callback_create.go +++ b/callback_create.go @@ -100,8 +100,8 @@ func createCallback(scope *Scope) { "INSERT INTO %v %v%v%v", quotedTableName, scope.Dialect().DefaultValueStr(), - addExtraSpaceIfExist(extraOption), - addExtraSpaceIfExist(lastInsertIDReturningSuffix), + addExtraSpaceBeforeIfExist(extraOption), + addExtraSpaceBeforeIfExist(lastInsertIDReturningSuffix), )) } else { scope.Raw(fmt.Sprintf( @@ -109,8 +109,8 @@ func createCallback(scope *Scope) { scope.QuotedTableName(), strings.Join(columns, ","), strings.Join(placeholders, ","), - addExtraSpaceIfExist(extraOption), - addExtraSpaceIfExist(lastInsertIDReturningSuffix), + addExtraSpaceBeforeIfExist(extraOption), + addExtraSpaceBeforeIfExist(lastInsertIDReturningSuffix), )) } diff --git a/callback_delete.go b/callback_delete.go index 73d90880..1b073136 100644 --- a/callback_delete.go +++ b/callback_delete.go @@ -41,15 +41,15 @@ func deleteCallback(scope *Scope) { scope.QuotedTableName(), scope.Quote(deletedAtField.DBName), scope.AddToVars(NowFunc()), - addExtraSpaceIfExist(scope.CombinedConditionSql()), - addExtraSpaceIfExist(extraOption), + addExtraSpaceBeforeIfExist(scope.CombinedConditionSql()), + addExtraSpaceBeforeIfExist(extraOption), )).Exec() } else { scope.Raw(fmt.Sprintf( "DELETE FROM %v%v%v", scope.QuotedTableName(), - addExtraSpaceIfExist(scope.CombinedConditionSql()), - addExtraSpaceIfExist(extraOption), + addExtraSpaceBeforeIfExist(scope.CombinedConditionSql()), + addExtraSpaceBeforeIfExist(extraOption), )).Exec() } } diff --git a/callback_query.go b/callback_query.go index ba10cc7d..468f26b4 100644 --- a/callback_query.go +++ b/callback_query.go @@ -56,7 +56,10 @@ func queryCallback(scope *Scope) { if !scope.HasError() { scope.db.RowsAffected = 0 if str, ok := scope.Get("gorm:query_option"); ok { - scope.SQL += addExtraSpaceIfExist(fmt.Sprint(str)) + scope.SQL += addExtraSpaceBeforeIfExist(fmt.Sprint(str)) + } + if str, ok := scope.Get("gorm:query_prefix"); ok { + scope.SQL = addExtraSpaceAfterIfExist(fmt.Sprint(str)) + scope.SQL } if rows, err := scope.SQLDB().Query(scope.SQL, scope.SQLVars...); scope.Err(err) == nil { diff --git a/callback_row_query.go b/callback_row_query.go index c2ff4a08..055cd4ab 100644 --- a/callback_row_query.go +++ b/callback_row_query.go @@ -1,6 +1,9 @@ package gorm -import "database/sql" +import ( + "database/sql" + "fmt" +) // Define callbacks for row query func init() { @@ -20,6 +23,9 @@ type RowsQueryResult struct { func rowQueryCallback(scope *Scope) { if result, ok := scope.InstanceGet("row_query_result"); ok { scope.prepareQuerySQL() + if str, ok := scope.Get("gorm:query_prefix"); ok { + scope.SQL = addExtraSpaceAfterIfExist(fmt.Sprint(str)) + scope.SQL + } if rowResult, ok := result.(*RowQueryResult); ok { rowResult.Row = scope.SQLDB().QueryRow(scope.SQL, scope.SQLVars...) diff --git a/callback_update.go b/callback_update.go index 373bd726..d36599ab 100644 --- a/callback_update.go +++ b/callback_update.go @@ -99,8 +99,8 @@ func updateCallback(scope *Scope) { "UPDATE %v SET %v%v%v", scope.QuotedTableName(), strings.Join(sqls, ", "), - addExtraSpaceIfExist(scope.CombinedConditionSql()), - addExtraSpaceIfExist(extraOption), + addExtraSpaceBeforeIfExist(scope.CombinedConditionSql()), + addExtraSpaceBeforeIfExist(extraOption), )).Exec() } } diff --git a/utils.go b/utils.go index dfaae939..6f8f6099 100644 --- a/utils.go +++ b/utils.go @@ -277,9 +277,16 @@ func getValueFromFields(value reflect.Value, fieldNames []string) (results []int return } -func addExtraSpaceIfExist(str string) string { +func addExtraSpaceBeforeIfExist(str string) string { if str != "" { return " " + str } return "" } + +func addExtraSpaceAfterIfExist(str string) string { + if str != "" { + return str + " " + } + return "" +}