mirror of https://github.com/go-gorm/gorm.git
Fix gorm.Expr with SubQuery, fix #3857
This commit is contained in:
parent
0f00493c50
commit
6848ae872f
11
statement.go
11
statement.go
|
@ -165,16 +165,7 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) {
|
||||||
case Valuer:
|
case Valuer:
|
||||||
stmt.AddVar(writer, v.GormValue(stmt.Context, stmt.DB))
|
stmt.AddVar(writer, v.GormValue(stmt.Context, stmt.DB))
|
||||||
case clause.Expr:
|
case clause.Expr:
|
||||||
var varStr strings.Builder
|
v.Build(stmt)
|
||||||
var sql = v.SQL
|
|
||||||
for _, arg := range v.Vars {
|
|
||||||
stmt.Vars = append(stmt.Vars, arg)
|
|
||||||
stmt.DB.Dialector.BindVarTo(&varStr, stmt, arg)
|
|
||||||
sql = strings.Replace(sql, "?", varStr.String(), 1)
|
|
||||||
varStr.Reset()
|
|
||||||
}
|
|
||||||
|
|
||||||
writer.WriteString(sql)
|
|
||||||
case driver.Valuer:
|
case driver.Valuer:
|
||||||
stmt.Vars = append(stmt.Vars, v)
|
stmt.Vars = append(stmt.Vars, v)
|
||||||
stmt.DB.Dialector.BindVarTo(writer, stmt, v)
|
stmt.DB.Dialector.BindVarTo(writer, stmt, v)
|
||||||
|
|
|
@ -2,6 +2,7 @@ package tests_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -493,3 +494,26 @@ func TestFirstOrCreateWithPrimaryKey(t *testing.T) {
|
||||||
t.Errorf("invalid primary key after creating, got %v, %v", companies[0].ID, companies[1].ID)
|
t.Errorf("invalid primary key after creating, got %v, %v", companies[0].ID, companies[1].ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateFromSubQuery(t *testing.T) {
|
||||||
|
user := User{Name: "jinzhu"}
|
||||||
|
|
||||||
|
DB.Create(&user)
|
||||||
|
|
||||||
|
subQuery := DB.Table("users").Where("name=?", user.Name).Select("id")
|
||||||
|
|
||||||
|
result := DB.Session(&gorm.Session{DryRun: true}).Model(&Pet{}).Create([]map[string]interface{}{
|
||||||
|
{
|
||||||
|
"name": "cat",
|
||||||
|
"user_id": gorm.Expr("(?)", DB.Table("(?) as tmp", subQuery).Select("@uid:=id")),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "dog",
|
||||||
|
"user_id": gorm.Expr("@uid"),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if !regexp.MustCompile(`INSERT INTO .pets. \(.name.,.user_id.\) .*VALUES \(.+,\(SELECT @uid:=id FROM \(SELECT id FROM .users. WHERE name=.+\) as tmp\)\),\(.+,@uid\)`).MatchString(result.Statement.SQL.String()) {
|
||||||
|
t.Errorf("invalid insert SQL, got %v", result.Statement.SQL.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue