Add "gorm:query_prefix"

This commit is contained in:
Tung Vu 2018-03-06 21:59:51 +07:00
parent 52c5c8127c
commit 9cad0154bb
6 changed files with 29 additions and 13 deletions

View File

@ -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),
))
}

View File

@ -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()
}
}

View File

@ -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 {

View File

@ -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...)

View File

@ -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()
}
}

View File

@ -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 ""
}