fix(database/gdb): support OrderRandom feature in different databases (#3794)

This commit is contained in:
oldme
2024-09-24 11:58:34 +08:00
committed by GitHub
parent 9af8393758
commit c13004e230
9 changed files with 76 additions and 1 deletions

View File

@ -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.

View File

@ -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 {

View File

@ -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
}