2020-02-13 19:09:44 +03:00
|
|
|
package clause_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2020-06-02 04:16:07 +03:00
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
"gorm.io/gorm/schema"
|
2020-06-02 05:34:50 +03:00
|
|
|
"gorm.io/gorm/utils/tests"
|
2020-02-13 19:09:44 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkSelect(b *testing.B) {
|
2020-02-26 14:06:42 +03:00
|
|
|
user, _ := schema.Parse(&tests.User{}, &sync.Map{}, db.NamingStrategy)
|
2020-02-13 19:09:44 +03:00
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
stmt := gorm.Statement{DB: db, Table: user.Table, Schema: user, Clauses: map[string]clause.Clause{}}
|
|
|
|
clauses := []clause.Interface{clause.Select{}, clause.From{}, clause.Where{Exprs: []clause.Expression{clause.Eq{Column: clause.PrimaryColumn, Value: "1"}, clause.Gt{Column: "age", Value: 18}, clause.Or(clause.Neq{Column: "name", Value: "jinzhu"})}}}
|
|
|
|
|
|
|
|
for _, clause := range clauses {
|
|
|
|
stmt.AddClause(clause)
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt.Build("SELECT", "FROM", "WHERE")
|
|
|
|
_ = stmt.SQL.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkComplexSelect(b *testing.B) {
|
2020-02-26 14:06:42 +03:00
|
|
|
user, _ := schema.Parse(&tests.User{}, &sync.Map{}, db.NamingStrategy)
|
2020-02-13 19:09:44 +03:00
|
|
|
|
2022-10-07 15:14:14 +03:00
|
|
|
limit10 := 10
|
2020-02-13 19:09:44 +03:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
stmt := gorm.Statement{DB: db, Table: user.Table, Schema: user, Clauses: map[string]clause.Clause{}}
|
|
|
|
clauses := []clause.Interface{
|
2022-01-06 10:02:53 +03:00
|
|
|
clause.Select{},
|
|
|
|
clause.From{},
|
2020-02-13 19:09:44 +03:00
|
|
|
clause.Where{Exprs: []clause.Expression{
|
|
|
|
clause.Eq{Column: clause.PrimaryColumn, Value: "1"},
|
|
|
|
clause.Gt{Column: "age", Value: 18},
|
|
|
|
clause.Or(clause.Neq{Column: "name", Value: "jinzhu"}),
|
|
|
|
}},
|
|
|
|
clause.Where{Exprs: []clause.Expression{
|
|
|
|
clause.Or(clause.Gt{Column: "score", Value: 100}, clause.Like{Column: "name", Value: "%linus%"}),
|
|
|
|
}},
|
2020-03-08 13:05:22 +03:00
|
|
|
clause.GroupBy{Columns: []clause.Column{{Name: "role"}}, Having: []clause.Expression{clause.Eq{"role", "admin"}}},
|
2022-10-07 15:14:14 +03:00
|
|
|
clause.Limit{Limit: &limit10, Offset: 20},
|
2020-02-13 19:09:44 +03:00
|
|
|
clause.OrderBy{Columns: []clause.OrderByColumn{{Column: clause.PrimaryColumn, Desc: true}}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, clause := range clauses {
|
|
|
|
stmt.AddClause(clause)
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt.Build("SELECT", "FROM", "WHERE", "GROUP BY", "LIMIT", "ORDER BY")
|
|
|
|
_ = stmt.SQL.String()
|
|
|
|
}
|
|
|
|
}
|