mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
fix(database/gdb): support OrderRandom feature in different databases (#3794)
This commit is contained in:
@ -178,6 +178,7 @@ type DB interface {
|
||||
ConvertValueForLocal(ctx context.Context, fieldType string, fieldValue interface{}) (interface{}, error) // See Core.ConvertValueForLocal
|
||||
CheckLocalTypeForField(ctx context.Context, fieldType string, fieldValue interface{}) (LocalType, error) // See Core.CheckLocalTypeForField
|
||||
FormatUpsert(columns []string, list List, option DoInsertOption) (string, error) // See Core.DoFormatUpsert
|
||||
OrderRandomFunction() string // See Core.OrderRandomFunction
|
||||
}
|
||||
|
||||
// TX defines the interfaces for ORM transaction operations.
|
||||
|
||||
@ -455,6 +455,11 @@ func (c *Core) RowsToResult(ctx context.Context, rows *sql.Rows) (Result, error)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// OrderRandomFunction returns the SQL function for random ordering.
|
||||
func (c *Core) OrderRandomFunction() string {
|
||||
return "RAND()"
|
||||
}
|
||||
|
||||
func (c *Core) columnValueToLocalValue(ctx context.Context, value interface{}, columnType *sql.ColumnType) (interface{}, error) {
|
||||
var scanType = columnType.ScanType()
|
||||
if scanType != nil {
|
||||
|
||||
@ -59,7 +59,7 @@ func (m *Model) OrderDesc(column string) *Model {
|
||||
// OrderRandom sets the "ORDER BY RANDOM()" statement for the model.
|
||||
func (m *Model) OrderRandom() *Model {
|
||||
model := m.getModel()
|
||||
model.orderBy = "RAND()"
|
||||
model.orderBy = m.db.OrderRandomFunction()
|
||||
return model
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user