forked from mirror/gorm
Fix SubQuery for raw sql
This commit is contained in:
parent
2ba612e805
commit
df24821896
|
@ -438,6 +438,12 @@ func (stmt *Statement) clone() *Statement {
|
|||
SkipHooks: stmt.SkipHooks,
|
||||
}
|
||||
|
||||
if stmt.SQL.Len() > 0 {
|
||||
newStmt.SQL.WriteString(stmt.SQL.String())
|
||||
newStmt.Vars = make([]interface{}, 0, len(stmt.Vars))
|
||||
newStmt.Vars = append(newStmt.Vars, stmt.Vars...)
|
||||
}
|
||||
|
||||
for k, c := range stmt.Clauses {
|
||||
newStmt.Clauses[k] = c
|
||||
}
|
||||
|
|
|
@ -991,7 +991,16 @@ func TestSubQueryWithRaw(t *testing.T) {
|
|||
DB.Create(&users)
|
||||
|
||||
var count int64
|
||||
err := DB.Raw("select count(*) from (?) tmp",
|
||||
err := DB.Raw("select count(*) from (?) tmp", DB.Raw("select name from users where age >= ? and name in (?)", 10, []string{"subquery_raw_1", "subquery_raw_3"})).Scan(&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 1, instead got %d", count)
|
||||
}
|
||||
|
||||
err = DB.Raw("select count(*) from (?) tmp",
|
||||
DB.Table("users").
|
||||
Select("name").
|
||||
Where("age >= ? and name in (?)", 20, []string{"subquery_raw_1", "subquery_raw_3"}).
|
||||
|
|
Loading…
Reference in New Issue