Add "gorm:query_prefix"
This commit is contained in:
parent
52c5c8127c
commit
9cad0154bb
@ -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),
|
||||
))
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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...)
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
9
utils.go
9
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 ""
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user