mirror of https://github.com/go-gorm/gorm.git
fixed wrong param substitution order
This commit is contained in:
parent
3c70f83833
commit
8d66eb4926
24
main_test.go
24
main_test.go
|
@ -631,6 +631,30 @@ func TestQueryBuilderSubselectInWhere(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestQueryBuilderRawQueryWithSubquery(t *testing.T) {
|
||||||
|
user := User{Name: "subquery_test_user1", Age: 10}
|
||||||
|
DB.Save(&user)
|
||||||
|
user = User{Name: "subquery_test_user2", Age: 11}
|
||||||
|
DB.Save(&user)
|
||||||
|
user = User{Name: "subquery_test_user2", Age: 12}
|
||||||
|
DB.Save(&user)
|
||||||
|
|
||||||
|
var count int
|
||||||
|
err := DB.Raw("select count(*) from (?) tmp",
|
||||||
|
DB.Table("users").
|
||||||
|
Select("name").
|
||||||
|
Where("age >= ? and name in (?)", 10, []string{"subquery_test_user1", "subquery_test_user2"}).
|
||||||
|
Group("name").
|
||||||
|
QueryExpr(),
|
||||||
|
).Count(&count).Error
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Expected to get no errors, but got %v", err)
|
||||||
|
}
|
||||||
|
if count != 2 {
|
||||||
|
t.Errorf("Row count must be 2, instead got %d", count)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestQueryBuilderSubselectInHaving(t *testing.T) {
|
func TestQueryBuilderSubselectInHaving(t *testing.T) {
|
||||||
user := User{Name: "query_expr_having_ruser1", Email: "root@user1.com", Age: 64}
|
user := User{Name: "query_expr_having_ruser1", Email: "root@user1.com", Age: 64}
|
||||||
DB.Save(&user)
|
DB.Save(&user)
|
||||||
|
|
54
scope.go
54
scope.go
|
@ -1,16 +1,16 @@
|
||||||
package gorm
|
package gorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"reflect"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Scope contain current operation's information when you perform any operation on the database
|
// Scope contain current operation's information when you perform any operation on the database
|
||||||
|
@ -555,6 +555,7 @@ func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str stri
|
||||||
return strings.Join(sqls, " AND ")
|
return strings.Join(sqls, " AND ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
replacements := []string{}
|
||||||
args := clause["args"].([]interface{})
|
args := clause["args"].([]interface{})
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
var err error
|
var err error
|
||||||
|
@ -562,29 +563,43 @@ func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str stri
|
||||||
case reflect.Slice: // For where("id in (?)", []int64{1,2})
|
case reflect.Slice: // For where("id in (?)", []int64{1,2})
|
||||||
if scanner, ok := interface{}(arg).(driver.Valuer); ok {
|
if scanner, ok := interface{}(arg).(driver.Valuer); ok {
|
||||||
arg, err = scanner.Value()
|
arg, err = scanner.Value()
|
||||||
str = strings.Replace(str, "?", scope.AddToVars(arg), 1)
|
replacements = append(replacements, scope.AddToVars(arg))
|
||||||
} else if bytes, ok := arg.([]byte); ok {
|
} else if b, ok := arg.([]byte); ok {
|
||||||
str = strings.Replace(str, "?", scope.AddToVars(bytes), 1)
|
replacements = append(replacements, scope.AddToVars(b))
|
||||||
} else if values := reflect.ValueOf(arg); values.Len() > 0 {
|
} else if values := reflect.ValueOf(arg); values.Len() > 0 {
|
||||||
var tempMarks []string
|
var tempMarks []string
|
||||||
for i := 0; i < values.Len(); i++ {
|
for i := 0; i < values.Len(); i++ {
|
||||||
tempMarks = append(tempMarks, scope.AddToVars(values.Index(i).Interface()))
|
tempMarks = append(tempMarks, scope.AddToVars(values.Index(i).Interface()))
|
||||||
}
|
}
|
||||||
str = strings.Replace(str, "?", strings.Join(tempMarks, ","), 1)
|
replacements = append(replacements, strings.Join(tempMarks, ","))
|
||||||
} else {
|
} else {
|
||||||
str = strings.Replace(str, "?", scope.AddToVars(Expr("NULL")), 1)
|
replacements = append(replacements, scope.AddToVars(Expr("NULL")))
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if valuer, ok := interface{}(arg).(driver.Valuer); ok {
|
if valuer, ok := interface{}(arg).(driver.Valuer); ok {
|
||||||
arg, err = valuer.Value()
|
arg, err = valuer.Value()
|
||||||
}
|
}
|
||||||
|
|
||||||
str = strings.Replace(str, "?", scope.AddToVars(arg), 1)
|
replacements = append(replacements, scope.AddToVars(arg))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buff := bytes.NewBuffer([]byte{})
|
||||||
|
i := 0
|
||||||
|
for pos := range str {
|
||||||
|
if str[pos] == '?' {
|
||||||
|
buff.WriteString(replacements[i])
|
||||||
|
i++
|
||||||
|
} else {
|
||||||
|
buff.WriteByte(str[pos])
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
scope.Err(err)
|
scope.Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
str = buff.String()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -642,8 +657,8 @@ func (scope *Scope) buildNotCondition(clause map[string]interface{}) (str string
|
||||||
if scanner, ok := interface{}(arg).(driver.Valuer); ok {
|
if scanner, ok := interface{}(arg).(driver.Valuer); ok {
|
||||||
arg, err = scanner.Value()
|
arg, err = scanner.Value()
|
||||||
str = strings.Replace(str, "?", scope.AddToVars(arg), 1)
|
str = strings.Replace(str, "?", scope.AddToVars(arg), 1)
|
||||||
} else if bytes, ok := arg.([]byte); ok {
|
} else if b, ok := arg.([]byte); ok {
|
||||||
str = strings.Replace(str, "?", scope.AddToVars(bytes), 1)
|
str = strings.Replace(str, "?", scope.AddToVars(b), 1)
|
||||||
} else if values := reflect.ValueOf(arg); values.Len() > 0 {
|
} else if values := reflect.ValueOf(arg); values.Len() > 0 {
|
||||||
var tempMarks []string
|
var tempMarks []string
|
||||||
for i := 0; i < values.Len(); i++ {
|
for i := 0; i < values.Len(); i++ {
|
||||||
|
@ -675,6 +690,7 @@ func (scope *Scope) buildSelectQuery(clause map[string]interface{}) (str string)
|
||||||
}
|
}
|
||||||
|
|
||||||
args := clause["args"].([]interface{})
|
args := clause["args"].([]interface{})
|
||||||
|
replacements := []string{}
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
switch reflect.ValueOf(arg).Kind() {
|
switch reflect.ValueOf(arg).Kind() {
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
|
@ -683,14 +699,28 @@ func (scope *Scope) buildSelectQuery(clause map[string]interface{}) (str string)
|
||||||
for i := 0; i < values.Len(); i++ {
|
for i := 0; i < values.Len(); i++ {
|
||||||
tempMarks = append(tempMarks, scope.AddToVars(values.Index(i).Interface()))
|
tempMarks = append(tempMarks, scope.AddToVars(values.Index(i).Interface()))
|
||||||
}
|
}
|
||||||
str = strings.Replace(str, "?", strings.Join(tempMarks, ","), 1)
|
replacements = append(replacements, strings.Join(tempMarks, ","))
|
||||||
default:
|
default:
|
||||||
if valuer, ok := interface{}(arg).(driver.Valuer); ok {
|
if valuer, ok := interface{}(arg).(driver.Valuer); ok {
|
||||||
arg, _ = valuer.Value()
|
arg, _ = valuer.Value()
|
||||||
}
|
}
|
||||||
str = strings.Replace(str, "?", scope.AddToVars(arg), 1)
|
replacements = append(replacements, scope.AddToVars(arg))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buff := bytes.NewBuffer([]byte{})
|
||||||
|
i := 0
|
||||||
|
for pos := range str {
|
||||||
|
if str[pos] == '?' {
|
||||||
|
buff.WriteString(replacements[i])
|
||||||
|
i++
|
||||||
|
} else {
|
||||||
|
buff.WriteByte(str[pos])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
str = buff.String()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue