Skip to content

Commit

Permalink
respect gorm:query_option in SubQuery() & QueryExpr() methods (jinzhu#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
zenovich authored Sep 21, 2020
1 parent 6bc389d commit 466b344
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
4 changes: 0 additions & 4 deletions callback_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ func queryCallback(scope *Scope) {
scope.SQL = fmt.Sprint(str) + scope.SQL
}

if str, ok := scope.Get("gorm:query_option"); ok {
scope.SQL += addExtraSpaceIfExist(fmt.Sprint(str))
}

if rows, err := scope.SQLDB().Query(scope.SQL, scope.SQLVars...); scope.Err(err) == nil {
defer rows.Close()

Expand Down
4 changes: 0 additions & 4 deletions callback_row_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ func rowQueryCallback(scope *Scope) {
scope.SQL = fmt.Sprint(str) + scope.SQL
}

if str, ok := scope.Get("gorm:query_option"); ok {
scope.SQL += addExtraSpaceIfExist(fmt.Sprint(str))
}

if rowResult, ok := result.(*RowQueryResult); ok {
rowResult.Row = scope.SQLDB().QueryRow(scope.SQL, scope.SQLVars...)
} else if rowsResult, ok := result.(*RowsQueryResult); ok {
Expand Down
29 changes: 29 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"path/filepath"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -1339,6 +1340,34 @@ func TestCountWithQueryOption(t *testing.T) {
}
}

func TestSubQueryWithQueryOption(t *testing.T) {
db := DB.New()

subQuery := db.Model(User{}).Select("users.id").
Set("gorm:query_option", "WHERE users.name='user2'").
SubQuery()

matched, _ := regexp.MatchString(
`^&{.+\s+WHERE users\.name='user2'.*\s\[]}$`, fmt.Sprint(subQuery))
if !matched {
t.Error("Unexpected result of SubQuery with query_option")
}
}

func TestQueryExprWithQueryOption(t *testing.T) {
db := DB.New()

queryExpr := db.Model(User{}).Select("users.id").
Set("gorm:query_option", "WHERE users.name='user2'").
QueryExpr()

matched, _ := regexp.MatchString(
`^&{.+\s+WHERE users\.name='user2'.*\s\[]}$`, fmt.Sprint(queryExpr))
if !matched {
t.Error("Unexpected result of QueryExpr with query_option")
}
}

func TestQueryHint1(t *testing.T) {
db := DB.New()

Expand Down
10 changes: 7 additions & 3 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,12 +841,16 @@ func (scope *Scope) joinsSQL() string {
}

func (scope *Scope) prepareQuerySQL() {
var sql string
if scope.Search.raw {
scope.Raw(scope.CombinedConditionSql())
sql = scope.CombinedConditionSql()
} else {
scope.Raw(fmt.Sprintf("SELECT %v FROM %v %v", scope.selectSQL(), scope.QuotedTableName(), scope.CombinedConditionSql()))
sql = fmt.Sprintf("SELECT %v FROM %v %v", scope.selectSQL(), scope.QuotedTableName(), scope.CombinedConditionSql())
}
return
if str, ok := scope.Get("gorm:query_option"); ok {
sql += addExtraSpaceIfExist(fmt.Sprint(str))
}
scope.Raw(sql)
}

func (scope *Scope) inlineCondition(values ...interface{}) *Scope {
Expand Down

0 comments on commit 466b344

Please sign in to comment.