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",
|
"INSERT INTO %v %v%v%v",
|
||||||
quotedTableName,
|
quotedTableName,
|
||||||
scope.Dialect().DefaultValueStr(),
|
scope.Dialect().DefaultValueStr(),
|
||||||
addExtraSpaceIfExist(extraOption),
|
addExtraSpaceBeforeIfExist(extraOption),
|
||||||
addExtraSpaceIfExist(lastInsertIDReturningSuffix),
|
addExtraSpaceBeforeIfExist(lastInsertIDReturningSuffix),
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
scope.Raw(fmt.Sprintf(
|
scope.Raw(fmt.Sprintf(
|
||||||
@ -109,8 +109,8 @@ func createCallback(scope *Scope) {
|
|||||||
scope.QuotedTableName(),
|
scope.QuotedTableName(),
|
||||||
strings.Join(columns, ","),
|
strings.Join(columns, ","),
|
||||||
strings.Join(placeholders, ","),
|
strings.Join(placeholders, ","),
|
||||||
addExtraSpaceIfExist(extraOption),
|
addExtraSpaceBeforeIfExist(extraOption),
|
||||||
addExtraSpaceIfExist(lastInsertIDReturningSuffix),
|
addExtraSpaceBeforeIfExist(lastInsertIDReturningSuffix),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,15 +41,15 @@ func deleteCallback(scope *Scope) {
|
|||||||
scope.QuotedTableName(),
|
scope.QuotedTableName(),
|
||||||
scope.Quote(deletedAtField.DBName),
|
scope.Quote(deletedAtField.DBName),
|
||||||
scope.AddToVars(NowFunc()),
|
scope.AddToVars(NowFunc()),
|
||||||
addExtraSpaceIfExist(scope.CombinedConditionSql()),
|
addExtraSpaceBeforeIfExist(scope.CombinedConditionSql()),
|
||||||
addExtraSpaceIfExist(extraOption),
|
addExtraSpaceBeforeIfExist(extraOption),
|
||||||
)).Exec()
|
)).Exec()
|
||||||
} else {
|
} else {
|
||||||
scope.Raw(fmt.Sprintf(
|
scope.Raw(fmt.Sprintf(
|
||||||
"DELETE FROM %v%v%v",
|
"DELETE FROM %v%v%v",
|
||||||
scope.QuotedTableName(),
|
scope.QuotedTableName(),
|
||||||
addExtraSpaceIfExist(scope.CombinedConditionSql()),
|
addExtraSpaceBeforeIfExist(scope.CombinedConditionSql()),
|
||||||
addExtraSpaceIfExist(extraOption),
|
addExtraSpaceBeforeIfExist(extraOption),
|
||||||
)).Exec()
|
)).Exec()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,10 @@ func queryCallback(scope *Scope) {
|
|||||||
if !scope.HasError() {
|
if !scope.HasError() {
|
||||||
scope.db.RowsAffected = 0
|
scope.db.RowsAffected = 0
|
||||||
if str, ok := scope.Get("gorm:query_option"); ok {
|
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 {
|
if rows, err := scope.SQLDB().Query(scope.SQL, scope.SQLVars...); scope.Err(err) == nil {
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package gorm
|
package gorm
|
||||||
|
|
||||||
import "database/sql"
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
// Define callbacks for row query
|
// Define callbacks for row query
|
||||||
func init() {
|
func init() {
|
||||||
@ -20,6 +23,9 @@ type RowsQueryResult struct {
|
|||||||
func rowQueryCallback(scope *Scope) {
|
func rowQueryCallback(scope *Scope) {
|
||||||
if result, ok := scope.InstanceGet("row_query_result"); ok {
|
if result, ok := scope.InstanceGet("row_query_result"); ok {
|
||||||
scope.prepareQuerySQL()
|
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 {
|
if rowResult, ok := result.(*RowQueryResult); ok {
|
||||||
rowResult.Row = scope.SQLDB().QueryRow(scope.SQL, scope.SQLVars...)
|
rowResult.Row = scope.SQLDB().QueryRow(scope.SQL, scope.SQLVars...)
|
||||||
|
@ -99,8 +99,8 @@ func updateCallback(scope *Scope) {
|
|||||||
"UPDATE %v SET %v%v%v",
|
"UPDATE %v SET %v%v%v",
|
||||||
scope.QuotedTableName(),
|
scope.QuotedTableName(),
|
||||||
strings.Join(sqls, ", "),
|
strings.Join(sqls, ", "),
|
||||||
addExtraSpaceIfExist(scope.CombinedConditionSql()),
|
addExtraSpaceBeforeIfExist(scope.CombinedConditionSql()),
|
||||||
addExtraSpaceIfExist(extraOption),
|
addExtraSpaceBeforeIfExist(extraOption),
|
||||||
)).Exec()
|
)).Exec()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
9
utils.go
9
utils.go
@ -277,9 +277,16 @@ func getValueFromFields(value reflect.Value, fieldNames []string) (results []int
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func addExtraSpaceIfExist(str string) string {
|
func addExtraSpaceBeforeIfExist(str string) string {
|
||||||
if str != "" {
|
if str != "" {
|
||||||
return " " + str
|
return " " + str
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addExtraSpaceAfterIfExist(str string) string {
|
||||||
|
if str != "" {
|
||||||
|
return str + " "
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user