1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 11:18:50 +08:00

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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 76 additions and 1 deletions

View File

@ -0,0 +1,12 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package oracle
// OrderRandomFunction returns the SQL function for random ordering.
func (d *Driver) OrderRandomFunction() string {
return "DBMS_RANDOM.VALUE()"
}

View File

@ -1177,6 +1177,17 @@ func Test_Model_Replace(t *testing.T) {
})
}
func Test_OrderRandom(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
result, err := db.Model(table).OrderRandom().All()
t.AssertNil(err)
t.Assert(len(result), TableSize)
})
}
/* not support the "AS"
func Test_Model_Raw(t *testing.T) {
table := createInitTable()

View File

@ -0,0 +1,12 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package pgsql
// OrderRandomFunction returns the SQL function for random ordering.
func (d *Driver) OrderRandomFunction() string {
return "RANDOM()"
}

View File

@ -587,3 +587,14 @@ func Test_Model_OnDuplicateEx(t *testing.T) {
t.Assert(one["nickname"], "name_1")
})
}
func Test_OrderRandom(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
result, err := db.Model(table).OrderRandom().All()
t.AssertNil(err)
t.Assert(len(result), TableSize)
})
}

View File

@ -0,0 +1,12 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package sqlite
// OrderRandomFunction returns the SQL function for random ordering.
func (d *Driver) OrderRandomFunction() string {
return "RANDOM()"
}

View File

@ -4299,3 +4299,14 @@ func TestResult_Structs1(t *testing.T) {
t.Assert(array[1].Name, "smith")
})
}
func Test_OrderRandom(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
result, err := db.Model(table).OrderRandom().All()
t.AssertNil(err)
t.Assert(len(result), TableSize)
})
}

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
}