Fix SubQuery for raw sql

This commit is contained in:
Jinzhu 2021-02-09 17:05:50 +08:00
parent 2ba612e805
commit df24821896
2 changed files with 16 additions and 1 deletions

View File

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

View File

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