This commit is contained in:
demoManito 2022-09-26 19:41:29 +08:00
parent c20ca7a969
commit a6eb6723f3
2 changed files with 4 additions and 4 deletions

View File

@ -401,11 +401,11 @@ func Expr(expr string, args ...interface{}) clause.Expr {
// ExprToString clause.Expression Build to SQL String // ExprToString clause.Expression Build to SQL String
func (db *DB) ExprToString(expr clause.Expression) string { func (db *DB) ExprToString(expr clause.Expression) string {
stmt := &Statement{ stmt := &Statement{
DB: db, DB: db.Session(&Session{DryRun: true, SkipDefaultTransaction: true}),
Clauses: map[string]clause.Clause{}, Clauses: map[string]clause.Clause{},
} }
expr.Build(stmt) expr.Build(stmt)
return db.Dialector.Explain(stmt.SQL.String(), stmt.Vars...) return stmt.DB.Dialector.Explain(stmt.SQL.String(), stmt.Vars...)
} }
// SetupJoinTable setup join table schema // SetupJoinTable setup join table schema

View File

@ -365,7 +365,7 @@ func TestExprToString(t *testing.T) {
wantSQL := "((`age` > 10 AND `age` < 18) AND (`age` > 18 AND `age` < 21))" wantSQL := "((`age` > 10 AND `age` < 18) AND (`age` > 18 AND `age` < 21))"
gotSQL := DB.ExprToString(clause.And(exprs...)) gotSQL := DB.ExprToString(clause.And(exprs...))
if wantSQL != gotSQL { if wantSQL != gotSQL {
t.Fatalf("want: %s \n, got: %s", wantSQL, gotSQL) t.Errorf("want: %s \n, got: %s", wantSQL, gotSQL)
} }
sql := DB.ToSQL(func(tx *gorm.DB) *gorm.DB { sql := DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
@ -378,7 +378,7 @@ func TestExprToString(t *testing.T) {
}) })
wantSQL = "SELECT * FROM `users` WHERE start_time >= 1234 AND (((`age` > 10 AND `age` < 18) AND (`age` > 18 AND `age` < 21))) AND `users`.`deleted_at` IS NULL" wantSQL = "SELECT * FROM `users` WHERE start_time >= 1234 AND (((`age` > 10 AND `age` < 18) AND (`age` > 18 AND `age` < 21))) AND `users`.`deleted_at` IS NULL"
if wantSQL != sql { if wantSQL != sql {
t.Fatalf("want: %s \n, got: %s", wantSQL, sql) t.Errorf("want: %s \n, got: %s", wantSQL, sql)
} }
} }