mirror of
https://github.com/ent/ent.git
synced 2026-04-30 22:50:54 +03:00
dialect/sql: add OrderByRand option (#3518)
This commit is contained in:
@@ -7,6 +7,8 @@ package sql
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"entgo.io/ent/dialect"
|
||||
)
|
||||
|
||||
// The following helpers exist to simplify the way raw predicates
|
||||
@@ -312,6 +314,20 @@ func orderByAgg(fn, field string, opts ...OrderTermOption) *OrderExprTerm {
|
||||
}
|
||||
}
|
||||
|
||||
// OrderByRand returns a term to natively order by a random value.
|
||||
func OrderByRand() func(*Selector) {
|
||||
return func(s *Selector) {
|
||||
s.OrderExprFunc(func(b *Builder) {
|
||||
switch s.Dialect() {
|
||||
case dialect.MySQL:
|
||||
b.WriteString("RAND()")
|
||||
default:
|
||||
b.WriteString("RANDOM()")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ToFunc returns a function that sets the ordering on the given selector.
|
||||
// This is used by the generated code.
|
||||
func (f *OrderFieldTerm) ToFunc() func(*Selector) {
|
||||
|
||||
Reference in New Issue
Block a user